codevs 1993草地排水 isap模板题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codevs 1993草地排水 isap模板题相关的知识,希望对你有一定的参考价值。
高天宇的isap模板,不用解释了吧。。。
#include<queue> #include<cstdio> #include<cctype> #include<cstring> #include<algorithm> using namespace std; const int inf=1E9; int N,M,point[203],nxt[403],v[403],c[403],cnt=0; int deep[203],num[203],cur[203],lastedge[203]; bool vis[203]; inline int getint(){char c;while (!isdigit(c=getchar()));int a=c-‘0‘;while (isdigit(c=getchar())) a=a*10+c-‘0‘;return a;} inline void insect(int x,int y,int z){nxt[cnt]=point[x];point[x]=cnt;v[cnt]=y;c[cnt]=z;cnt++;} inline void bfs(int t){ for (int i=1;i<=N;++i) deep[i]=N; memset(vis,0,sizeof(vis)); queue<int>q; vis[t]=1; q.push(t); deep[t]=0; while (!q.empty()){ int now=q.front(); q.pop(); for (int tmp=point[now];tmp!=-1;tmp=nxt[tmp]) if ((vis[v[tmp]]==0)&&(c[tmp^1]!=0)){ vis[v[tmp]]=1; q.push(v[tmp]); deep[v[tmp]]=deep[now]+1; } } } inline int addflow(int s,int t){ int ans=inf,now=t; while (now!=s){ ans=min(ans,c[lastedge[now]]); now=v[lastedge[now]^1]; }now=t; while (now!=s){ c[lastedge[now]]-=ans; c[lastedge[now]^1]+=ans; now=v[lastedge[now]^1]; }return ans; } inline int isap(int s,int t){ int ans=0,now=s; bfs(t); for (int i=1;i<=N;++i) num[deep[i]]++,cur[i]=point[i]; while (deep[s]<N){ if (now==t){ ans+=addflow(s,t); now=s; }bool has_find=0; for (int tmp=cur[now];tmp!=-1;tmp=nxt[tmp]){ int u=v[tmp]; if ((deep[u]+1==deep[now])&&(c[tmp]!=0)){ has_find=1; cur[now]=tmp; lastedge[u]=tmp; now=u; break; } } if (has_find==0){ int minn=N-1; for (int tmp=point[now];tmp!=-1;tmp=nxt[tmp]) if (c[tmp]!=0) minn=min(minn,deep[v[tmp]]); if (--num[deep[now]]==0) break; num[deep[now]=minn+1]++; cur[now]=point[now]; if (now!=s) now=v[lastedge[now]^1]; } }return ans; } int main(){ memset(point,-1,sizeof(point)); memset(nxt,-1,sizeof(nxt)); M=getint();N=getint(); for (int i=1;i<=M;++i){ int x=getint(),y=getint(),z=getint(); insect(x,y,z); insect(y,x,0); }printf("%d\n",isap(1,N));return 0; }
以上是关于codevs 1993草地排水 isap模板题的主要内容,如果未能解决你的问题,请参考以下文章