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 |
好棒,好棒,过了100道,而且是网络流的第一题。//1459 Accepted 196K 329MS C++ 2160B 2012-08-29 20:33:59 #include<stdio.h> #include<string.h> #define inf 100000000 int matrix[110][110]; int dui[110],vist[110],n,s,t,opt[110]; int bfs() { //printf("linger3 "); int i,x; memset(vist,0,sizeof(vist)); memset(opt,0,sizeof(opt)); int front=0,rear=0; dui[rear]=s; opt[s]=0; vist[s]=1; front++; while(rear!=front) { x=dui[rear]; rear++; for(i=0;i<=n+1;i++) { if(matrix[x][i]>0&&vist[i]==0) { vist[i]=1; dui[front]=i; opt[i]=opt[x]+1; front++; } } } if(vist[t]==1) { return 1;//广搜搜到了汇点,表示可以进行深搜 } else { return 0; } } int lujing[110],min,flow; void dfs(int x,int index) { // printf("linger2 "); int i; lujing[index]=x; if(x==t) { min=inf; for(i=0;i<index;i++) { if(matrix[lujing[i]][lujing[i+1]]<min) { min=matrix[lujing[i]][lujing[i+1]]; } } for(i=0;i<index;i++) { matrix[lujing[i]][lujing[i+1]]=matrix[lujing[i]][lujing[i+1]]-min; matrix[lujing[i+1]][lujing[i]]=matrix[lujing[i+1]][lujing[i]]-(-min); } flow=flow+min; return ; } for(i=0;i<=n+1;i++) { if(matrix[x][i]>0&&opt[x]==opt[i]-1) { dfs(i,index+1); } } return ; } int dicnic() { //printf("linger1\n"); int f=0,zong; zong=0; while(bfs()) { //printf("linger4"); flow=0; min=inf; dfs(s,0); zong=zong+flow; } return zong; } int main() { int m,np,nc,d1,d2,d3,i,j; for(;scanf("%d%d%d%d ",&n,&np,&nc,&m)!=EOF;) { memset(matrix,0,sizeof(matrix)); s=n; t=n+1; for(i=0;i<m;i++) { scanf("(%d,%d)%d ",&d1,&d2,&d3); matrix[d1][d2]=d3; } for(i=0;i<np;i++) { scanf("(%d)%d ",&d1,&d2); matrix[s][d1]=d2; } for(i=0;i<nc;i++) { if(i<nc-1) { scanf("(%d)%d ",&d1,&d2); } else { scanf("(%d)%d",&d1,&d2); } matrix[d1][t]=d2; } /*for(i=0;i<=n+1;i++) { for(j=0;j<=n+1;j++) { printf("%d ",matrix[i][j]); } printf("\n"); }*/ printf("%d\n",dicnic()); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator