// ShellDawn
// POJ1459
// No.23

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#define MM(x) memset(x,0,sizeof(x))
#define INF 0x3f3f3f3f
using namespace std;

#define maxn 105
int E[maxn][maxn];
int P[maxn][maxn][2];
int C[maxn];
int V[maxn];
int N;

bool BFS(int s){
    MM(V);MM(C);
    queue<int> q;
    q.push(s);
    V[s] = 1;
    while(!q.empty()){
        int now = q.front();
        q.pop();
        for(int i=1;i<=N+1;i++){
            if(V[i] == 0 && E[now][i] > 0){
                V[i] = V[now] + 1;
                q.push(i);
                int c = C[now];
                P[now][c][0] = i;
                P[now][c][1] = E[now][i];
                C[now]++;
                if(i==N+1) return true;
            }
        }
    }
    return false;
}

int DFS(int now,int minflow){
    if(now == N+1) return minflow;
    int flow = 0;
    for(int i=0;i<C[now];i++){
        int next = P[now][i][0];
        int v = P[now][i][1];
        int f = DFS(next,min(minflow,v));
        flow += f;
        minflow -= f;
        E[now][next] -= f;
        E[next][now] += f;
    }
    return flow;
}

int main(){
    //freopen("A.txt","r",stdin);
    int n,np,nc,m;
    while(~scanf("%d%d%d%d",&n,&np,&nc,&m)){
        MM(E);N=n;
        for(int i=0;i<m;i++){
            int a,b,z;
            scanf(" (%d,%d)%d",&a,&b,&z);
            //cout<<"a b z : "<<a<<" "<<b<<" "<<z<<endl;
            E[a+1][b+1] = z;
        }
        for(int i=0;i<np;i++){
            int a,z;
            scanf(" (%d)%d",&a,&z);
            E[0][a+1] = z;
        }
        for(int i=0;i<nc;i++){
            int a,z;
            scanf(" (%d)%d",&a,&z);
            E[a+1][n+1]  = z;
        }
        int ans = 0;
        while(BFS(0)) ans+=DFS(0,INF);
        printf("%d\n",ans);
    }
    return 0;
}
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐