F - Power Network POJ - 1459

#include   
#include   
#include   
using namespace std;  
const int N = 105;  
const int inf = 0x3f3f3f3f;  
int g[N][N];  
int flow[N][N];  
int p[N];  
int x[N];  
int s,e;  
int Edmonds_Karp()  
{  
    memset(flow,0,sizeof(flow));  
    queue Q;  
    int f = 0,v,u;  
    while(1)  
    {  
        memset(x,0,sizeof(x));  
        x[s] = inf;  
        Q.push(s);  
        while(!Q.empty())  
        {  
            u = Q.front();  
            Q.pop();  
            for(v=s;v<=e;v++)  
            {  
                if(!x[v] && g[u][v]>flow[u][v])  
                {  
                    p[v] = u;  
                    Q.push(v);  
                    x[v] = min(x[u],g[u][v]-flow[u][v]);  
                }  
            }  
        }  
        if(x[e]==0)  
        {  
            return f;  
        }  
        for(int i=e;i!=s;i=p[i])  
        {  
            flow[p[i]][i] += x[e];  
            flow[i][p[i]] -= x[e];  
        }  
        f += x[e];  
    }  
}  
int main()  
{  
    int n,a,b,m,i;  
    while(scanf("%d%d%d%d",&n,&a,&b,&m)!=EOF)  
    {  
        int u,v,w;  
        memset(g,0,sizeof(g));  
        for(i=1;i<=m;i++)  
        {  
            scanf(" (%d,%d)%d",&u,&v,&w);  
            g[++u][++v] = w;  
        }  
        for(i=1;i<=a;i++)  
        {  
            scanf(" (%d)%d",&u,&w);  
            g[0][++u] = w;  
        }  
        for(i=1;i<=b;i++)  
        {  
            scanf(" (%d)%d",&v,&w);  
            g[++v][n+1] = w;  
        }  
        n++;  
        s = 0; e = n;  
        printf("%d\n",Edmonds_Karp());  
    }  
    return 0;  
}  

 

你可能感兴趣的:(网络流,最大流)