题意:给出n个点,m条正权的边,w条负权的边,问是否存在负环
因为Bellman_ford最多松弛n-1次, 因为从起点1终点n最多经过n-2个点,即最多松弛n-1次,如果第n次松弛还能成功的话,则说明存在有负环
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int maxn=;
int d[maxn];
int flag;
int nodenum,edgenum,ecnt; struct Edge{
int u,v,cost;
} e[maxn]; int Bellman_ford(){
for(int i=;i<=nodenum;i++) d[i]=INF;
d[]=; for(int i=;i<nodenum;i++){
for(int j=;j<=ecnt;j++){
if(d[e[j].v]>d[e[j].u]+e[j].cost)
d[e[j].v]=d[e[j].u]+e[j].cost;
}
} flag=;
for(int i=;i<=ecnt;i++){
if(d[e[i].v]>d[e[i].u]+e[i].cost){
flag=;
break;
}
}
return flag;
} void addedges(int a,int b,int c){
e[++ecnt].u=a;
e[ecnt].v=b;
e[ecnt].cost=c;
} int main(){
int m,w,ncase;
int a,b,c;
scanf("%d",&ncase);
while(ncase--){
ecnt=;
scanf("%d %d %d",&nodenum,&m,&w);
while(m--){
scanf("%d %d %d",&a,&b,&c);
addedges(a,b,c);
addedges(b,a,c);
}
while(w--){
scanf("%d %d %d",&a,&b,&c);
c=-c;
addedges(a,b,c);
}
if(Bellman_ford()) printf("NO\n");
else printf("YES\n");
}
return ;
}