SPFA求负环 模板题
记得每组处理之前clear vector
/* ***********************************************
Author :Sun Yuefeng
Created Time :2016/10/25 18:02:02
File Name :A.cpp
************************************************ */ #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<vector>
#include<queue>
#include<list>
#define M(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=2e3+;
const int mod=1e7+;
int dx[]= {,,,-,,-,,-};
int dy[]= {,-,,,-,,,-}; struct edge
{
int v,value;
edge(int _v=,int _value=):v(_v),value(_value){}
}; vector<edge>e[maxn];
int n,m,w;
bool vis[maxn];
int cnt[maxn];
int dist[maxn]; bool SPFA()
{
M(vis,false);
M(dist,inf);
M(cnt,);
vis[]=true;
dist[]=;
cnt[]=;
queue<int>q;
while(!q.empty()) q.pop();
q.push();
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
for(int i=;i<e[u].size();i++)
{
int v=e[u][i].v;
if(dist[v]>dist[u]+e[u][i].value)
{
dist[v]=dist[u]+e[u][i].value;
if(!vis[v])
{
vis[v]=true;
q.push(v);
cnt[v]++;
if(cnt[v]==n) return true;
}
}
}
}
return false;
} void addedge(int u,int v,int w)
{
e[u].push_back(edge(v,w));
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
for(int i=;i<maxn;i++)
e[i].clear();
int u,v,l;
scanf("%d%d%d",&n,&m,&w);
while(m--)
{
scanf("%d%d%d",&u,&v,&l);
addedge(u,v,l);
addedge(v,u,l);
}
while(w--)
{
scanf("%d%d%d",&u,&v,&l);
addedge(u,v,-l);
}
if(SPFA()) printf("YES\n");
else printf("NO\n");
}
return ;
}