80分求调
查看原帖
80分求调
768587
mc360楼主2024/6/5 13:43

3号点不过,笔算也是同一个答案

#include<bits/stdc++.h>
using namespace std;
struct edge
{
	long long to,nxt,data;
}e[20001];
int head[101];
long long n,m,cnt,cn,u,w,x,y,z,ans[101],f[101],rd[101],cd[101],c[101];
queue<int> q;
void add(int x,int y,int v)
{
	e[++cnt].nxt=head[x];
	e[cnt].data=v;
	head[x]=cnt;
	e[cnt].to=y;
}
void topsort()
{
	for(int i=1;i <= n;i++)
		if(!rd[i])
			q.push(i);
	while(q.size())
	{
		u=q.front();
		q.pop();
		for(int i=head[u];i != 0;i=e[i].nxt)
		{
			x=e[i].to;
			w=e[i].data;
			c[x]+=w*c[u];
			rd[x]--;
			if(!rd[x])
			{
				c[x]-=f[x];
				q.push(x);
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	for(int i=1;i <=n;i++)
	{
		cin >> c[i] >> f[i];
	}
	for(int i=1;i <= m;i++)
	{
		cin >> x >> y >> z;
		rd[y]++;
		cd[x]++;
		add(x,y,z);
	}
	topsort();
	for(int i=1;i <= n;i++)
	{
		if(cd[i]==0&&c[i]>0)
		{
			ans[++cn]=i;
		}
	}
	if(!cn)
		cout << "NULL\n";
	else
	{
		sort(ans+1,ans+1+cn);
		for(int i=1;i <= cn;i++)
			cout << ans[i] << ' ' << c[ans[i]] << endl;
	}
	return 0;
}
/*
6 8
1 0
1 0
1 100
0 1
0 -2
0 0
1 4 1
2 4 0
3 4 -1
1 5 1
2 5 -1
3 5 -1
4 6 1
5 6 1
*/ 
// 6 1
2024/6/5 13:43
加载中...