首页 > 代码库 > poj1459网络流之多源点最大流

poj1459网络流之多源点最大流

这题想了好久,一直认为应该bfs更新后求最小值把发电站最大发电加进去,但是又发现这样求增广路的时候会导致用户更新出错,

加源点和汇点也考虑到了,没想到居然发电量就是超级源到源点的v,居然这么简单@。@

技术分享
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;

const double g=10.0,eps=1e-9;
const int N=100+10,maxn=16,inf=9999999;

int v[N][N],s,t,n;
int pre[N];
bool vis[N];
bool bfs()
{
    memset(vis,0,sizeof vis);
    memset(pre,0,sizeof pre);
    vis[s]=1;
    queue<int>q;
    q.push(s);
    while(!q.empty()){
        int x=q.front();
        if(x==t)return 1;
        q.pop();
        for(int i=0;i<=n+1;i++)
        {
            if(!vis[i]&&v[x][i])
            {
                vis[i]=1;
                q.push(i);
                pre[i]=x;
            }
        }
    }
    return 0;
}
int max_flow()
{
    int ans=0;
    while(1){
        if(!bfs())return ans;
        int minn=inf+1;
        for(int i=t;i!=s;i=pre[i])
            minn=min(minn,v[pre[i]][i]);
        for(int i=t;i!=s;i=pre[i])
        {
            v[pre[i]][i]-=minn;
            v[i][pre[i]]+=minn;
        }
        ans+=minn;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int np,nc,m;
    while(cin>>n>>np>>nc>>m){
        memset(v,0,sizeof v);
        int a,b,c;
        char ru;
        while(m--){
            cin>>ru;
            cin>>a;
            cin>>ru;
            cin>>b;
            cin>>ru;
            cin>>c;
            v[a][b]+=c;
        }
        while(np--){
            cin>>ru;
            cin>>a;
            cin>>ru;
            cin>>b;
            v[n][a]=b;
        }
        while(nc--){
            cin>>ru;
            cin>>a;
            cin>>ru;
            cin>>b;
            v[a][n+1]=b;
        }
        s=n,t=n+1;
        cout<<max_flow()<<endl;
    }
    return 0;
}
View Code

 

poj1459网络流之多源点最大流