ACM/ICPC 之 ACM计算机工厂-EK算法(POJ3436)

  题意有点难读懂

 

//网络流-EK算法-ACM计算机工厂-构图重点
//Time:0Ms  Memory:208K
#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;

#define MAXN 55
#define INF 0x3f3f3f3f

int p,n;
int s,t;
int comp[MAXN][22];
int bd[MAXN][MAXN];
int res[MAXN][MAXN];
int pre[MAXN];

bool bfs()
{
    memset(pre,-1,sizeof(pre));
    queue<int> q;
    q.push(s); pre[s] = 0;
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 1; i <= t; i++)
        {
            if(pre[i] == -1 && res[cur][i])
            {
                pre[i] = cur;
                if(i == t) return true;
                q.push(i);
            }
        }
    }
    return false;
}

int EK(int &total)
{
    int maxFlow = 0;
    while(bfs()){
        int mind = INF;
        for(int i = t; i != s; i = pre[i])
            mind = min(mind, res[pre[i]][i]);
        for(int i = t; i != s; i = pre[i])
        {
            if(i != t && pre[i] != s && res[pre[i]][i] == bd[pre[i]][i]) total++;
            res[pre[i]][i] -= mind;
            res[i][pre[i]] += mind;
            if(i != t && pre[i] != s && res[i][pre[i]] == bd[i][pre[i]]) total--;
        }
        maxFlow += mind;
    }
    return maxFlow;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    scanf("%d%d",&p,&n);
    s = 0; t = n + 1;
    //输入+构图
    for(int i = 1; i <= n; i++)
    {
        bool in = true, out = true;
        for(int j = 0; j <= p*2; j++)
        {
            scanf("%d", &comp[i][j]);
            if(j >= 1 && j <= p && comp[i][j] == 1) in = false;
            if(j > p && !comp[i][j]) out = false;   //未生产完
        }
        if(in) bd[s][i] = comp[i][0];
        if(out) bd[i][t] = comp[i][0];
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            bool con = true;
            if(i == j) continue;
            for(int k = 1; k <= p; k++)
            {
                if(comp[j][k] != 2 && comp[i][k + p] != comp[j][k])    //判断是否可传输此线
                {
                    con = false;
                    break;
                }
            }
            if(con) bd[i][j] = comp[i][0];  //connection
        }

    memcpy(res, bd, sizeof(bd));
    int total = 0;
    int maxFlow = EK(total);
    printf("%d %d\n", maxFlow, total);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            if(bd[i][j] && bd[i][j] > res[i][j])
                printf("%d %d %d\n", i, j, bd[i][j] - res[i][j]);
        }
    return 0;
}

 

    原文作者:Inkblots
    原文地址: https://www.cnblogs.com/Inkblots/p/5714735.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞