60分求调
查看原帖
60分求调
1045301
Hamburger999楼主2024/1/23 15:32
#include<bits/stdc++.h>
using namespace std;

struct Edge{
	int v,w,nxt;
};
struct A{
	int c,u;
};
int head[20001],d[20001],N,M,ans[20001],cnt,k,out[20001];
Edge m[20001];
A f[20001];

void addedge(int u,int v,int w){
	m[k]={v,w,head[u]};
	++d[v];
	head[u]=k++;
}

void topsort(){
	stack<int>q;
	cnt=0;
	for(int i=1;i<=N;i++){
		if(d[i]==0){
			q.push(i);
			ans[++cnt]=i;
		} 
	}
	while(!q.empty()){
		int a=q.top();
		q.pop();
		for(int i=head[a];i!=-1;i=m[i].nxt){
			if(!(--d[m[i].v])){
				q.push(m[i].v);
				ans[++cnt]=m[i].v;
			}
		}
	}
}

int main(){
	memset(head,-1,sizeof(head));
	cin>>N>>M; 
	for(int i=1;i<=N;i++){
		cin>>f[i].c>>f[i].u;
	}
	int u,v,w;
	for(int i=1;i<=M;i++){
		cin>>u>>v>>w;
		addedge(u,v,w);
	}
	for(int i=1;i<=N;i++){
		if(head[i]==-1){
			out[++cnt]=i;
		}
	}
	topsort();
	for(int i=1;i<=N;i++){
		f[ans[i]].c-=f[ans[i]].u;
		if(f[ans[i]].c>0){
			for(int j=head[ans[i]];j!=-1;j=m[j].nxt){
				f[m[j].v].c+=m[j].w*f[ans[i]].c;
			}
		}
	}
	k=0;
	for(int i=1;i<=cnt;i++){ 
		if(f[out[i]].c>0){
			k++;
			cout<<out[i]<<' '<<f[out[i]].c<<endl;
		}
	}
	if(k==0)cout<<"NULL";
	return 0;
}
2024/1/23 15:32
加载中...