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 |
为什么dinic 就会错 好心人看一下吧,或者提供些数据,这个是我第一次做网络流,千万不要弄的像泰坦尼克一样。多谢!#include <iostream> using namespace std; #define MAX 201 int n,m,flow; int c[MAX][MAX],lc[MAX][MAX],mark[MAX];//mark[]用来记录层次 lc[][]是处于相邻层次结点所在的边,c[][]就是初始图 int next[MAX];//在dfs中用来记录路径的,next顾名思义 int mini,miniold;//min是记录当前增广路中目前最小的流,miniold是当本次搜索失败后,给还原使用 int bfs(){ int head=0,tail=1,seq[MAX]; int i; memset(lc,0,sizeof(lc)); memset(seq,0,sizeof(seq)); memset(mark,255,sizeof(mark));//m[] set to -1 seq[1]=1; mark[1]=1; while(head<tail){ head++; for(i=1;i<=m;i++) if(c[seq[head]][i]>0) if(mark[i]==-1 || mark[i]==mark[seq[head]]+1){ if(mark[i]==-1){tail++; seq[tail]=i;} mark[i]=mark[seq[head]]+1; lc[seq[head]][i]=c[seq[head]][i]; } } return 0; } int dfs(int start,int end){ if(start==0) return 0; int i; if(start==end){ for(i=1;i!=m;i=next[i]){ lc[i][next[i]]-=mini; lc[next[i]][i]+=mini; c[i][next[i]]-=mini; c[next[i]][i]+=mini; } flow+=mini; mini=2000000000; return 1; } for(i=1;i<=m;i++) if(mark[i]==mark[start]+1 && lc[start][i]>0){ next[start]=i; miniold=mini; if(mini>lc[start][i]) mini=lc[start][i]; if(dfs(i,end)) return 1; mini=miniold; next[start]=0; } return 0; } int main(){ //freopen("ditch.in","r",stdin); //freopen("ditch.out","w",stdout); int i,j,k,temp; while(1){ memset(c,0,sizeof(c)); if(cin>>n>>m){ flow=0; for(i=1;i<=n;i++){ cin>>j>>k>>temp; c[j][k]+=temp; } while(1){ bfs(); if(mark[m]==-1) break; mini=2000000000; memset(next,0,sizeof(next)); i=1; while(dfs(i,m)){ for(i=1;lc[i][next[i]]>0;i++){} i--; } } cout<<flow<<endl; } else break; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator