网络流Dinic算法模板 POJ1273
生活随笔
收集整理的這篇文章主要介紹了
网络流Dinic算法模板 POJ1273
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這就是以后我的板子啦~~~
#include <queue> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define N 444 int tot,next[N],first[N],w[N],v[N],n,m,ch[N]; void add(int from,int to,int weight){v[tot]=to;w[tot]=weight;next[tot]=first[from];first[from]=tot++; } bool tell(){memset(ch,-1,sizeof(ch));queue<int>q;q.push(1);ch[1]=0;while(!q.empty()){int t=q.front();q.pop();for(int i=first[t];~i;i=next[i])if(w[i]&&ch[v[i]]==-1)q.push(v[i]),ch[v[i]]=ch[t]+1;}return ch[n]!=-1; } int zeng(int a,int b){if(a==n)return b;int r=0;for(int i=first[a];~i&&b>r;i=next[i])if(ch[a]+1==ch[v[i]]&&w[i]){int t=zeng(v[i],min(b-r,w[i]));w[i]-=t;w[i^1]+=t;r+=t;}if(!r)ch[a]=-1;return r; } int dinic(){int ans=0,jy;while(tell())while(jy=zeng(1,0x3fffffff))ans+=jy;return ans; } int main(){while(scanf("%d%d",&m,&n)!=EOF){memset(first,-1,sizeof(first));register int xx,yy,zz;tot=0;for(int i=1;i<=m;i++){scanf("%d%d%d",&xx,&yy,&zz);add(xx,yy,zz);add(yy,xx,0);}printf("%d\n",dinic());} }還有一中寫在結構體里面的:
(假設1為源點,n為匯點)
轉載于:https://www.cnblogs.com/SiriusRen/p/6532269.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的网络流Dinic算法模板 POJ1273的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [bash] printf使用范例
- 下一篇: VsCode官网快速下载技巧