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 |
Re:这题WA了我16次,问题居然是我把N,M,W跟F一起读了,导致所有N,M,W都一样,郁闷死了,附邻接矩阵+邻接表Bellman_fordIn Reply To:这题WA了我16次,问题居然是我把N,M,W跟F一起读了,导致所有N,M,W都一样,郁闷死了,附邻接矩阵+邻接表Bellman_ford Posted by:Belldandy at 2011-10-26 17:16:23 邻接表: Source Code Problem: 3259 User: Belldandy Memory: 184K Time: 79MS Language: C Result: Accepted Source Code #include<stdio.h> #define INF 999999 struct NODE { int x,y,length; }queue[30000]; int length=0; int bellman(int n) { int i=0,j=0,k=0,check[600],flag=0,u,v; for(i=1;i<=n;i++) { check[i]=INF; } check[1]=0; for(i=0;i<=n;i++) { flag=0; for(j=0;j<length;j++) { u=queue[j].x; v=queue[j].y; if(check[v]>check[u]+queue[j].length) { check[v]=check[u]+queue[j].length; flag=1; } } if(!flag) { return 0; } } for(j=0;j<length;j++) { u=queue[j].x; v=queue[j].y; if(check[v]>check[u]+queue[j].length) { return 1; } } return 0; } int main() { int f=0,n=0,m=0,w=0; int i=0,s=0,e=0,t=0,j=0; scanf("%d",&f); while(f--) { scanf("%d%d%d",&n,&m,&w); length=0; for(i=0;i<m;i++) { scanf("%d%d%d",&s,&e,&t); queue[length].x=s; queue[length].y=e; queue[length++].length=t; queue[length].x=e; queue[length].y=s; queue[length++].length=t; } for(i=0;i<w;i++) { scanf("%d%d%d",&s,&e,&t); queue[length].x=s; queue[length].y=e; queue[length++].length=-t; } if(bellman(n)) { printf("YES\n"); } else { printf("NO\n"); } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator