#include<stdio.h> const int INF = 10000000; const int MX = 2050; int m,n,d[MX]; struct Node { int u,v,w; }nodes[MX]; void initializeSingleSource(int s) { for(int i=0;i<n;i++) { d[i]=INF; d[s]=0; } } void relax(int u,int v ,int w) { if(d[v]>d[u]+w) { d[v]=d[u]+w; } } bool bellmanFord(int s) { initializeSingleSource(s); int i,j,k; for(i=0;i<n-1;i++) { for(j=0;j<m;j++) { relax(nodes[j].u,nodes[j].v,nodes[j].w); } } for(j=0;j<m;j++) { if (d[nodes[j].v] > d[nodes[j].u] + nodes[j].w) return false; } return true; } int main() { int test; scanf("%d",&test); while(test--) { scanf("%d %d",&n,&m); int i,j,k,l; for(i=0;i<m;i++) { scanf("%d %d %d",&nodes[i].u,&nodes[i].v,&nodes[i].w); } if(!bellmanFord(0))printf("possible\n"); else printf("not possible\n"); } return 0; }
Monday, August 25, 2014
uva: 558 - Wormholes
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment