Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## Bellman Ford 判断负环AC （附代码）

Posted by MadCreeper at 2017-05-18 22:11:06 on Problem 3259
```//ACCEPTED! Bellman Ford ·
#include<iostream>
#include<cstdio>
#include<cstring>
#define INF 999999
using namespace std;
int F,N,M,W;
int s,e,t;
int cnt;
int dis[520];
struct Edge
{
int u,v;
int cost;
// u go to v costs 'cost'
}edge[5040]={0};
{
edge[cnt].u=ss;
edge[cnt].v=ee;
edge[cnt].cost=tt;
cnt++;//count of edges
}

int Bellman()
{
for(int i=1;i<=N;i++)
dis[i]=INF;
dis[1]=0;
for(int i=1;i<N;i++)
for(int j=0;j<cnt;j++)
dis[edge[j].v]=min(dis[edge[j].v],(dis[edge[j].u]+edge[j].cost));

for(int j=0;j<cnt;j++)
if(dis[edge[j].v]>dis[edge[j].u]+edge[j].cost)
return 0;
return 1;
}
int main()
{
cin>>F;

for(int k=1;k<=F;k++)
{
cin>>N>>M>>W;
cnt=0;

for(int i=1;i<=M;i++)
{
cin>>s>>e>>t;
}
for(int i=1;i<=W;i++)
{
cin>>s>>e>>t;
}
if(Bellman()) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}

return 0;
}
```

Followed by: