网络流

来源:互联网 发布:linux发行版排行 2017 编辑:程序博客网 时间:2024/06/06 07:42

Dinic:

vart,cap,next:array[0..400] of longint;headlist:array[0..200] of longint;vis:array[0..200] of boolean;cur,d:array[0..200] of longint;queue:array[0..200] of longint;ans,front,finish,num,i,j,k,m,n,x,y,z:longint;procedure init;beginreadln(m,n);for i:=1 to n doheadlist[i]:=-1;num:=-1;for i:=1 to m dobeginreadln(x,y,z);inc(num);next[num]:=headlist[x];headlist[x]:=num;t[num]:=y;cap[num]:=z;inc(num);next[num]:=headlist[y];headlist[y]:=num;t[num]:=x;cap[num]:=0;end;end;function BFS:boolean;var i:longint;beginfillchar(vis,sizeof(vis),false);queue[1]:=1; front:=1; finish:=1;vis[1]:=true;while (finish>=front) dobegini:=headlist[queue[front]];while i<>-1 dobeginif (not(vis[t[i]]) and (cap[i]>0)) then beginvis[t[i]]:=true;d[t[i]]:=d[queue[front]]+1;inc(finish);queue[finish]:=t[i];end;i:=next[i];end;inc(front);end;exit(vis[n]);end;function min(a,b:longint):longint;beginif a<b then exit(a);exit(b);end;function DFS(now,a:longint):longint;var flow,f,i:longint;beginif (now=n) or (a=0) then exit(a);flow:=0;i:=cur[now];while i<>-1 dobeginif d[now]+1=d[t[i]] then beginf:=dfs(t[i],min(a,cap[i]));if f>0 then begincap[i]:=cap[i]-f;cap[i xor 1]:=cap[i xor 1]+f;flow:=flow+f;a:=a-f;if (a=0) then break;end;end;i:=next[i];cur[now]:=i;end;if flow=0 then d[now]:=-1;exit(flow);end;procedure Dinic;beginans:=0;while BFS dobeginfor i:=1 to n docur[i]:=headlist[i];ans:=ans+dfs(1,maxlongint);end;end;begininit;Dinic;writeln(ans);end.
ISAP:

const shuru='NetFlow.in';  shuchu='NetFlow.out';type pppp=recordfrom,too,sap:longint;  end;varedge:array[0..401] of pppp;g:Array[0..200,0..200] of longint;queue,sum,p,cur,d:array[0..201] of longint;vis:array[0..201] of boolean;ans,front,finish,now,x,y,z,num,i,j,k,n,m:longint;ok:boolean;procedure init;beginreadln(m,n);num:=-1;for i:=1 to m dobeginreadln(X,y,z);inc(g[x,0]);inc(num);g[x,g[x,0]]:=num;edge[num].from:=x;edge[num].too:=y;edge[num].sap:=z;inc(g[y,0]);inc(num);g[y,g[y,0]]:=num;edge[num].from:=y;edge[num].too:=x;edge[num].sap:=0;end;end;procedure BFS;var i:longint;step:pppp;beginfillchar(vis,sizeof(vis),false);queue[1]:=n; front:=1; finish:=1; vis[n]:=true;while finish>=front dobeginfor i:=1 to g[queue[front],0] dobeginstep:=edge[g[queue[front],i]];if not(vis[step.too]) then begininc(Finish);queue[finish]:=step.too;vis[step.too]:=true;d[step.too]:=d[step.from]+1;end;end;inc(front);end;end;function min(a,b:longint):longint;beginif a<b then exit(A);exit(b);end;function Augment:longint;var x,a:longint;beginx:=n; a:=maxlongint;while x<>1 dobegina:=min(a,edge[p[x]].sap);x:=edge[p[x]].from;end;x:=n;while x<>1 dobeginedge[p[x]].sap:=edge[p[x]].sap-a;edge[p[x] xor 1].sap:=edge[p[x] xor 1].sap+a;x:=edge[p[x]].from;end;exit(A);end;procedure ISAP;beginBFS;for i:=1 to n do inc(sum[d[i]]);x:=1; ans:=0;for i:=1 to n do cur[i]:=1;while (d[1]<n) dobeginif (x=n) then beginans:=ans+Augment;x:=1;  end;OK:=false;for i:=cur[x] to g[x,0] doif (d[x]=d[edge[g[x,i]].too]+1) and (edge[g[x,i]].sap>0) then beginok:=true;p[edge[g[x,i]].too]:=g[x,i];cur[x]:=i;x:=edge[g[x,i]].too;break;  end;if not(ok) then beginm:=n-1;for i:=1 to g[x,0] doif edge[g[x,i]].sap>0 then m:=min(m,d[edge[g[x,i]].too]);dec(sum[d[x]]);if sum[d[x]]=0 then break;d[x]:=m+1;inc(sum[d[x]]);cur[x]:=1;if (x<>1) then x:=edge[p[x]].from;end;end;end;begininit;ISAP;writeln(Ans);end.



0 0
原创粉丝点击