Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
一次A过,留下代码做纪念~(bellman-ford)#include<iostream> #include<cstring> using namespace std; int dis[6000]; int mix=10005; int n,m,w; struct edge { int u; int v; int t; }; edge a[6000]; void bellman() { memset(dis,mix,sizeof(dis)); dis[1]=0; for(int i=1;i<n-1;i++) for(int j=1;j<=2*m+w;j++) { if(dis[a[j].u]+a[j].t<dis[a[j].v]) dis[a[j].v]=dis[a[j].u]+a[j].t; } int flag=0; for(int j=1;j<=2*m+w;j++) { if(dis[a[j].u]+a[j].t<dis[a[j].v]) { flag=1; break; } } if(flag==1) cout<<"YES"<<endl; else cout<<"NO"<<endl; return; } int main() { int f; cin>>f; while(f--) { cin>>n>>m>>w; int i; for(i=1;i<=2*m-1;i=i+2) { cin>>a[i].u>>a[i].v>>a[i].t; a[i+1].u=a[i].v; a[i+1].v=a[i].u; a[i+1].t=a[i].t; } for(i=2*m+1;i<=2*m+w;i++) { cin>>a[i].u>>a[i].v>>a[i].t; a[i].t=-a[i].t; } bellman(); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator