标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1145 Accepted Submission(s): 476
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<cstdlib> #include<algorithm> #include<queue> #include<stack> #include<vector> #include<set> using namespace std; int n,m,in[1010],dp[1010]; vector<int> e[1010],w[1010]; int main() { int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { int ans=0; for(int i=0;i<n;i++) e[i].clear(),in[i]=0,dp[i]=0,w[i].clear(); for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); e[a].push_back(b); w[a].push_back(c); in[b]++; } queue<int> q; for(int i=0;i<n;i++) { if(in[i]==0) q.push(i),dp[i]=1; else dp[i]=0; } while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0;i<e[x].size();i++) { int v=e[x][i]; dp[v]=max(dp[v],dp[x]+w[x][i]); if(--in[v]==0) { q.push(v); } } } for(int i=0;i<n;i++) { if(ans<dp[i]) ans=dp[i]; } printf("%d\n",ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4401899.html