标签:
#include<iostream> #include<cstdio> #include<queue> #include<vector> #include<cstring> #include<algorithm> using namespace std; const int INF=10e7; const int MAXN=510; int f,n,m,w,s,e,t; int lc[MAXN],cntNode[MAXN]; bool vis[MAXN]; struct edge { int v,cost; }E; vector<edge> Edge[MAXN]; bool SPFA() { queue<int> que; int u,v,c,len; for(int i=1;i<=n;i++) lc[i]=INF,vis[i]=0,cntNode[i]=0; lc[1]=0;vis[1]=1;cntNode[1]=1; que.push(1); while(!que.empty()) { u=que.front();que.pop(); vis[u]=0;len=Edge[u].size(); for(int i=0;i<len;i++) { v=Edge[u][i].v; c=Edge[u][i].cost; if(lc[v]>lc[u]+c) { lc[v]=lc[u]+c; if(!vis[v]) { vis[v]=1; ++cntNode[v]; que.push(v); if(cntNode[v]>n) return true; } } } } return false; } int main() { scanf("%d",&f); while(f--) { scanf("%d%d%d",&n,&m,&w); for(int i=1;i<=n;i++) Edge[i].clear(); while(m--) { scanf("%d%d%d",&s,&e,&t); E.v=e;E.cost=t; Edge[s].push_back(E); E.v=s;E.cost=t; Edge[e].push_back(E); } while(w--) { scanf("%d%d%d",&s,&e,&t); E.v=e;E.cost=-t; Edge[s].push_back(E); } if(SPFA()) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/atmacmer/p/5196914.html