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 |
距离标号最短增广路WA了16次了,不知道哪里错了啊,救命啊~~这几天饭都吃不进去,麻烦哪位大哥指点我一下,这是改动后的最后一次代码…… #include <stdio.h> #include <memory.h> #define MAXINT 1000000000 #define S 201 int na,nb,n,s,t,l[S],p[S][S],f[S][S],c[S][S],pre[S],m[S],a[S],b[S],d[S]; int min(int x,int y) { return (x<y)?x:y; } bool bfs() { int i,j,k; memset(pre,0,sizeof(pre)); memset(d,0,sizeof(d)); memset(a,0,sizeof(a)); pre[t]=t; for (a[1]=t,na=1;!pre[s];) { nb=0; for (k=1;k<=na;k++) { i=a[k]; for (j=1;j<=l[i];j++) if (!pre[p[i][j]]) { b[++nb]=p[i][j]; pre[b[nb]]=i; d[b[nb]]=d[i]+1; } } if (!nb) return false; memset(b,0,sizeof(a)); for (na=nb,i=1;i<=na;i++) a[i]=b[i]; memset(b,0,sizeof(b)); } return true; } int shortest_augument() { int i,j,k,temp,res=0; bool have; if (!bfs()) return 0; memset(pre,0,sizeof(pre)); memset(m,0,sizeof(m)); m[s]=MAXINT; pre[s]=s; for (i=s;d[s]<n;) { have=false; for (k=1;k<=l[i];k++) { j=p[i][k]; if (d[i]==d[j]+1 && !pre[j] && c[i][j]-f[i][j]>0) { pre[j]=i; m[j]=min(m[i],c[i][j]-f[i][j]); i=j; if (pre[t]) { for (i=t;i!=s;i=pre[i]) { f[pre[i]][i]+=m[t]; f[i][pre[i]]=-f[pre[i]][i]; } memset(pre,0,sizeof(pre)); memset(m,0,sizeof(m)); m[s]=MAXINT; pre[s]=s; } have=true; break; } } if (!have) { temp=MAXINT; for (k=1;k<=l[i];k++) { j=p[i][k]; if (!pre[j] && c[i][j]-f[i][j]>0) { have=true; if (d[j]<temp) temp=d[j]; } } if (have) d[i]=temp+1; else d[i]=n,i=pre[i]; } } for (i=1;i<=l[s];i++) res+=f[s][p[s][i]]; return res; } main() { int i,m,u,v,x; for (;scanf("%d%d",&m,&n)!=EOF;printf("%d\n",shortest_augument())) { s=1; t=n; memset(l,0,sizeof(l)); memset(p,0,sizeof(p)); memset(c,0,sizeof(c)); memset(f,0,sizeof(f)); for (i=1;i<=m;i++) { scanf("%d%d%d",&u,&v,&x); if (c[u][v] || c[v][u]) c[u][v]+=x; else if (x) { p[u][++l[u]]=v; p[v][++l[v]]=u; c[u][v]=x; } } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator