拓扑排序,WA#2#3#5,70pts求调
查看原帖
拓扑排序,WA#2#3#5,70pts求调
658620
Youth_Glory楼主2024/8/17 16:33
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define debug cout<<"weifeng's debug\n";
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const ll MAXN=1e5+5;

ll n,m,c,in[MAXN],cnt[MAXN],u,v,x;
vector<ll>G[MAXN];
map<ll,map<ll,ll> >memory;
queue<ll>q;

void bfs(){
	for(ll i=1;i<=n;i++)if(in[i]==0)q.push(i);
	while(!q.empty()){
		ll now=q.front();
		q.pop();
		for(ll i=0;i<G[now].size();i++){
			ll nxt=G[now][i];
			if(--in[nxt]==0)q.push(nxt);
			cnt[nxt]=max(cnt[nxt],cnt[now]+memory[now][nxt]);
		}
	}
	for(ll i=1;i<=n;i++)cout<<cnt[i]<<"\n";
}

signed main()
{
	IOS;
	cin>>n>>m>>c;
	for(ll i=1;i<=n;i++)cin>>cnt[i];
	for(ll i=1;i<=c;i++){
		cin>>u>>v>>x;
		memory[u][v]=x;
		G[u].push_back(v);
		in[v]++;
	}
	bfs();
	return 0;
}

2024/8/17 16:33
加载中...