poj1459 Power Network --- 最大流 EK/dinic

來源:互聯網
上載者:User

標籤:blog   os   io   for   2014   ar   amp   size   

求從電站->調度站->消費者的最大流,給出一些邊上的容量,和電站和消費者可以輸入和輸出的最大量。

添加一個超級源點和匯點,建邊跑模板就可以了。兩個模板逗可以。


#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#define inf 0x3f3f3f3f#define eps 1e-6#define ll __int64const int maxn=110;using namespace std;int n,m,np,nc,s,t;int level[maxn],c[maxn][maxn];bool makelevel(){    memset(level,0,sizeof level);    level[s]=1;    int q[maxn];    int fro=0,iq=0;    q[iq++]=s;    int i,v;    while(fro!=iq)    {        v=q[fro++];        for(i=1;i<=n+2;i++)//注意點的編號        {            if(!level[i]&&c[v][i])            {                level[i]=level[v]+1;                q[iq++]=i;            }        }    }    if(!level[t]) return 0;    return 1;}int dfs(int now,int maxf){    if(now==t) return maxf;    int ret=0;    for(int i=1;maxf&&i<=n+2;i++)//注意點的編號    {        if(c[now][i]&&level[now]+1==level[i])        {            int tmp=dfs(i,min(maxf,c[now][i]));            c[now][i]-=tmp;            c[i][now]+=tmp;            ret+=tmp;            maxf-=tmp;        }    }    return ret;}int dinic(){    int ans=0;    while(makelevel()) ans+=dfs(s,inf);    return ans;}/*int c[maxn][maxn],p[maxn];bool bfs(){    queue<int> q;    bool vis[maxn];    memset(vis,0,sizeof vis);    memset(p,-1,sizeof p);    q.push(s);    vis[s]=1;    while(!q.empty())    {        int e=q.front();        if(e==t) return 1;        q.pop();        for(int i=1;i<=n+2;i++)//注意點的範圍        {            if(c[e][i]&&!vis[i])            {                vis[i]=1;                p[i]=e;                q.push(i);            }        }    }    return 0;}int ek(){    int u,ans=0,mn;    while(bfs())    {        mn=inf;        u=t;        while(p[u]!=-1)        {            mn=min(mn,c[p[u]][u]);            u=p[u];        }        ans+=mn;        u=t;        while(p[u]!=-1)        {            c[p[u]][u]-=mn;            c[u][p[u]]+=mn;            u=p[u];        }    }    return ans;}*/int main(){    int i,a,b,cc;    while(~scanf("%d%d%d%d",&n,&np,&nc,&m))    {        s=n+1,t=n+2;        memset(c,0,sizeof c);        for(i=0;i<m;i++)        {            while(getchar()!='(');            scanf("%d,%d)%d",&a,&b,&cc);            c[a+1][b+1]=cc;        }        for(i=0;i<np;i++)        {            while(getchar()!='(');            scanf("%d)%d",&a,&b);            c[s][a+1]=b;        }        for(i=0;i<nc;i++)        {            while(getchar()!='(');            scanf("%d)%d",&a,&b);            c[a+1][t]=b;        }        printf("%d\n",dinic());    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.