POJ 3436 -- ACM Computer Factory(最大流,建图)

题目链接

Description

As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That is why all these computers are historically produced at the same factory.Every ACM computer consists of P parts. When all these parts are present, the computer is ready and can be shipped to one of the numerous ACM contests.Computer manufacturing is fully automated by using N various machines. Each machine removes some parts from a half-finished computer and adds some new parts (removing of parts is sometimes necessary as the parts cannot be added to a computer in arbitrary order). Each machine is described by its performance (measured in computers per hour), input and output specification.Input specification describes which parts must be present in a half-finished computer for the machine to be able to operate on it. The specification is a set of P numbers 0, 1 or 2 (one number for each part), where 0 means that corresponding part must not be present, 1 — the part is required, 2 — presence of the part doesn’t matter.Output specification describes the result of the operation, and is a set of P numbers 0 or 1, where 0 means that the part is absent, 1 — the part is present.The machines are connected by very fast production lines so that delivery time is negligibly small compared to production time.After many years of operation the overall performance of the ACM Computer Factory became insufficient for satisfying the growing contest needs. That is why ACM directorate decided to upgrade the factory.As different machines were installed in different time periods, they were often not optimally connected to the existing factory machines. It was noted that the easiest way to upgrade the factory is to rearrange production lines. ACM directorate decided to entrust you with solving this problem.

Input

Input file contains integers P N, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integers Qi Si,1 Si,2…Si,P Di,1 Di,2…Di,P, where Qi specifies performance, Si,j — input specification for part j, Di,k — output specification for part k.Constraints1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Qi ≤ 10000

Output

Output the maximum possible overall performance, then M — number of connections that must be made, then M descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, where W is the number of computers delivered from A to B per hour.If several solutions exist, output any of them.

Sample Input

Sample input 1
3 4
15  0 0 0  0 1 0
10  0 0 0  0 1 1
30  0 1 2  1 1 1
3   0 2 1  1 1 1
Sample input 2
3 5
5   0 0 0  0 1 0
100 0 1 0  1 0 1
3   0 1 0  1 1 0
1   1 0 1  1 1 0
300 1 1 2  1 1 1
Sample input 3
2 2
100  0 0  1 0
200  0 1  1 1

Sample Output

Sample output 1
25 2
1 3 15
2 3 10
Sample output 2
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
Sample output 3
0 0

AC

  • 两个版本拆点和不拆点
  • 网上很多都是拆点,我这里是不拆点的(如果WA,就多交几次。。。玄学!!!)
  • 建图:
  • 如果一个机器的输入端没有1,那么这个机器就是开始的机器,让这个机器和源点相连
  • 如果一个机器的输出端全是1,那么这个机器就是结束的机器,让这个机器和汇点相连
  • 然后遍历每个机器,如果一个机器的输出端符合另一台的输入端,那么就可以建边,权值为输出端的最大产量
  • 最后从源点到汇点跑一个最大流
// 不拆点
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 300005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;

int a[105][105], a1[105][105], g[105][55], pre[105];
bool vis[105], viss[105];
int inf = 0x3f3f3f3f;
int n, m;
struct ac{
    int x, y, c;
};
// EK最大流 
bool bfs(int s, int e) {
    memset(pre, -1, sizeof(pre));
    memset(vis, false, sizeof(vis));
    vis[s] = true;
    pre[s] = s;
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = 0; i <= m + 1; ++i) {
            if (vis[i] || a[t][i] == 0) continue;
            vis[i] = true;
            pre[i] = t;
            if (i == e) return true;
            que.push(i);
        }
    }
    return false;
}
ll solve(int s, int e) {
    ll sum = 0;
    while (bfs(s, e)) {
        int MIN = inf;
        for (int i = e; i != s; i = pre[i]) {
            MIN = min(MIN, a[pre[i]][i]);
        }
        for (int i = e; i != s; i = pre[i]) {
            a[pre[i]][i] -= MIN;
            a[i][pre[i]] += MIN;
        }
        sum += MIN;
    }
    return sum;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(g, 0, sizeof(g));
        memset(a, 0, sizeof(a));
        memset(viss, false, sizeof(viss));
        for (int i = 1; i <= m; ++i) {
            for (int j = 0; j <= 2 * n; ++j) {
                scanf("%d", &g[i][j]);
            }
        }
        for (int i = 1; i <= m; ++i) {
            // 判断输入端是否全是0 
            int flag1 = 0;
            for (int j = 1; j <= n; ++j) {
                if (g[i][j] == 1)   flag1 = 1;
            }
            if (!flag1) a[0][i] = g[i][0], viss[i] = true;
            // 判断输出端是否全唯1 
            int flag0 = 0;
            for (int j = n + 1; j <= 2 * n; ++j) {
                if (g[i][j] == 0)   flag0 = 1;
            }
            if (!flag0) {
                a[i][m + 1] = g[i][0];
                continue;
            } 
            // 将满足输出与输入关系对应的点建边 
            for (int j = 1; j <= m; ++j) {
                // 如果要匹配的点是起点,直接跳过(加快速度) 
                if (i == j || viss[j])  continue;
                int flag = 1;
                for (int k = 1; k <= n; ++k) {
                    if (g[i][k + n] + g[j][k] == 1)
                        flag = 0;
                }
                if (flag)   a[i][j] = g[i][0];
            } 
        }
        // 将图备份,在最后选择出最大流经过的边 
        for (int i = 0; i <= m + 1; ++i) {
            for (int j = 0; j <= m + 1; ++j) {
                a1[i][j] = a[i][j];
            }
        }
        // 最大流 
        ll ans = solve(0, m + 1);
        // 存最大流 每条边的情况 
        vector<ac> v;
        for (int i = 1; i <= m; ++i) {
            for (int j = 1; j <= m; ++j) {
                // a1是备份图,如果跑完最大流之后,边的权值小于原来的权值,那么这个边就是最大流的一部分 
                if (a1[i][j] > a[i][j]) {
                    v.push_back((ac){i, j, a1[i][j] - a[i][j]});
                }
            }
        } 
        int len = v.size();
        printf("%lld %d\n", ans, len);
        for (int i = 0; i < len; ++i) {
            printf("%d %d", v[i].x, v[i].y);
            printf(" %d\n", v[i].c);
        }
    }
    return 0;
} 
  • 将点与自己连一个产量
  • 源点和点的全为零的输入端,汇点和全为1的输出端设为正无穷
// 拆点
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#define N 300005
#include <cstring>
#define ll long long
#define P pair<int, int>
#define mk make_pair
using namespace std;

int a[205][205], a1[205][205], g[205][105], pre[205];
bool vis[205], viss[205];
int inf = 0x3f3f3f3f;
int n, m;
struct ac{
    int x, y, c;
};
// EK最大流 
bool bfs(int s, int e) {
    memset(pre, -1, sizeof(pre));
    memset(vis, false, sizeof(vis));
    vis[s] = true;
    pre[s] = s;
    queue<int> que;
    que.push(s);
    while (!que.empty()) {
        int t = que.front();
        que.pop();
        for (int i = 0; i <= 2 * m + 1; ++i) {
            if (vis[i] || a[t][i] == 0) continue;
            vis[i] = true;
            pre[i] = t;
            if (i == e) return true;
            que.push(i);
        }
    }
    return false;
}
ll solve(int s, int e) {
    ll sum = 0;
    while (bfs(s, e)) {
        int MIN = inf;
        for (int i = e; i != s; i = pre[i]) {
            MIN = min(MIN, a[pre[i]][i]);
        }
        for (int i = e; i != s; i = pre[i]) {
            a[pre[i]][i] -= MIN;
            a[i][pre[i]] += MIN;
        }
        sum += MIN;
    }
    return sum;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(g, 0, sizeof(g));
        memset(a, 0, sizeof(a));
        memset(viss, false, sizeof(viss));
        for (int i = 1; i <= m; ++i) {
            for (int j = 0; j <= 2 * n; ++j) {
                scanf("%d", &g[i][j]);
            }
        }
        // 拆点建边 
        for (int i = 1; i <= m; ++i) {
            a[2 * i - 1][2 * i] = g[i][0];
        } 
        for (int i = 1; i <= m; ++i) {
            // 判断输入端是否全是0 
            int flag1 = 0;
            for (int j = 1; j <= n; ++j) {
                if (g[i][j] == 1)   flag1 = 1;
            }
            if (!flag1) a[0][i * 2 - 1] = inf, viss[i] = true;
            // 判断输出端是否全唯1 
            int flag0 = 0;
            for (int j = n + 1; j <= 2 * n; ++j) {
                if (g[i][j] == 0)   flag0 = 1;
            }
            if (!flag0) {
                a[i * 2][2 * m + 1] = inf;
                continue;
            } 
            // 将满足输出与输入关系对应的点建边 
            for (int j = 1; j <= m; ++j) {
                // 如果要匹配的点是起点,直接跳过(加快速度) 
                if (i == j || viss[j])  continue;
                int flag = 1;
                for (int k = 1; k <= n; ++k) {
                    if (g[i][k + n] + g[j][k] == 1)
                        flag = 0;
                }
                if (flag)   a[i * 2][j * 2 - 1] = min(g[i][0], g[j][0]);
            } 
        }
        // 将图备份,在最后选择出最大流经过的边 
        for (int i = 0; i <= 2 * m + 1; ++i) {
            for (int j = 0; j <= 2 * m + 1; ++j) {
                a1[i][j] = a[i][j];
            }
        }
        // 最大流 
        ll ans = solve(0, 2 * m + 1);
        // 存最大流 每条边的情况 
        vector<ac> v;
        for (int i = 1; i <= 2 * m; ++i) {
            for (int j = 1; j <= 2 * m; ++j) {
                if ((i + 1) / 2 == (j + 1) / 2) continue;
                // a1是备份图,如果跑完最大流之后,边的权值小于原来的权值,那么这个边就是最大流的一部分 
                if (a1[i][j] > a[i][j]) {
                    v.push_back((ac){(i + 1) / 2, (j + 1) / 2, a1[i][j] - a[i][j]});
                }
            }
        } 
        int len = v.size();
        printf("%lld %d\n", ans, len);
        for (int i = 0; i < len; ++i) {
            printf("%d %d", v[i].x, v[i].y);
            printf(" %d\n", v[i].c);
        }
    }
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/81783851