#include<bits/stdc++.h>
using namespace std;
struct node{
int w,ne;
};
vector<node> g[1000];
int i,j,k,m,n,w,t,a,b,c,p[1000],d[1000],fl,f;
void dfs(int x){
if (p[x]==1) {
fl=1; return;
}
p[x]=1;
for (int i=0;i<g[x].size();i++) {
if (fl) return;
if (g[x][i].w+d[x]<d[g[x][i].ne]){
d[g[x][i].ne]=g[x][i].w+d[x];
dfs(g[x][i].ne);
if (fl){
p[x]=0;
return;
}
}
}
p[x]=0;
}
int main(){
scanf("%d",&t);
while (t--){
scanf("%d%d%d",&w,&n,&m);
for (i=1;i<=w;i++) g[i].clear();
for (i=1;i<=n;i++) {
scanf("%d%d%d",&a,&b,&c);
node tmp;
tmp.w=c; tmp.ne=b; g[a].push_back(tmp);
tmp.w=c; tmp.ne=a; g[b].push_back(tmp);
}
for (i=1;i<=m;i++) {
scanf("%d%d%d",&a,&b,&c);
node tmp;
tmp.w=-c; tmp.ne=b; g[a].push_back(tmp);
}
f=0;
memset(d,0,sizeof(d));
for (i=1;i<=w;i++){
fl=0;
dfs(i);
//puts("OK");
if (fl){
printf("YES\n");
f++;
}
if (f) break;
}
if (!f) printf("NO\n");
}
return 0;
}
之前是用Floyd做的,然而只有12(方法完全不对好吗?!)
然后用SPFA,结果又莫名WA掉。
改用vector,终于对了。。。