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 |
spfa。。。#include<iostream> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<queue> using namespace std; int f,n,m,w,map[510][510],a,b,c,dis[510],e[510],cnt[510],fail; queue<int> que; void spfa() { while(!que.empty()&&!fail) { int v=que.front(); que.pop(); e[v]=0; // if (dis[v]>10000||dis[v]<-10000) // { // fail=1; // break; // } for (int i=1;i<=n;i++) if (map[v][i]) if (dis[i]>map[v][i]+dis[v]) { dis[i]=map[v][i]+dis[v]; if (!e[i]) { e[i]=1; que.push(i); cnt[i]++; if (cnt[i]>=n) { fail=1; break; } } } } } int main() { ios::sync_with_stdio(false); cin>>f; for (int l=1;l<=f;l++) { memset(map,0,sizeof(map)); memset(e,0,sizeof(e)); memset(cnt,0,sizeof(cnt)); fail=0; while (!que.empty()) que.pop(); // que.clear(); cin>>n>>m>>w; for (int i=1;i<=n;i++) dis[i]=10000000; for (int i=1;i<=m;i++) { cin>>a>>b>>c; if (map[a][b]) map[a][b]=min(map[a][b],c); else map[a][b]=c; if (map[b][a]) map[b][a]=min(map[b][a],c); else map[b][a]=c; } for (int i=1;i<=w;i++) { cin>>a>>b>>c; if (map[a][b]) map[a][b]=min(map[a][b],-c); else map[a][b]=-c; } que.push(1);e[1]=1;dis[1]=0;cnt[1]=1; spfa(); if (fail) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator