// // main.cpp // poj3259 // // Created by Fangpin on 15/5/28. // Copyright (c) 2015年 FangPin. All rights reserved. // #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; int g[600][600]; struct Edge{ int from,to,dist; }; int n,d[1000]; vector<Edge> vec; bool bf(){ for(int i=0;i<1000;++i) d[i]=10000000; d[1]=0; for(int i=0;i<n-1;++i){ bool flag=false; for(int j=0;j<vec.size();++j){ Edge &e=vec[j]; if(d[e.to]>d[e.from]+e.dist){ d[e.to]=d[e.from]+e.dist; flag=true; } } if(!flag) break; } for(int i=0;i<vec.size();++i){ Edge &e=vec[i]; if(d[e.to]>d[e.from]+e.dist) return false; } return true; } int main(int argc, const char * argv[]) { int t; cin>>t; while(t--){ int m,k; vec.clear(); cin>>n>>m>>k; memset(g,0,sizeof(g)); for(int i=1;i<=m;++i){ int a,b,c; scanf("%d%d%d",&a,&b,&c); Edge e; e.from=a; e.to=b; e.dist=c; vec.push_back(e); swap(e.from,e.to); vec.push_back(e); } for(int i=1;i<=k;++i){ int a,b,c; scanf("%d%d%d",&a,&b,&c); c=-c; Edge e; e.from=a; e.to=b; e.dist=c; vec.push_back(e); } if(bf()) cout<<"NO\n"; else cout<<"YES\n"; } return 0; }
poj 3259 bellman-ford判断是否存在负权回路
原文地址:http://blog.csdn.net/fangpinlei/article/details/46129693