POJ 3436:ACM Computer Factory 网络流

来源:互联网 发布:mac 不同设备 照片分类 编辑:程序博客网 时间:2024/05/20 23:57

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 6247 Accepted: 2178 Special Judge

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 jDi,k— output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 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 13 415  0 0 0  0 1 010  0 0 0  0 1 130  0 1 2  1 1 13   0 2 1  1 1 1Sample input 23 55   0 0 0  0 1 0100 0 1 0  1 0 13   0 1 0  1 1 01   1 0 1  1 1 0300 1 1 2  1 1 1Sample input 32 2100  0 0  1 0200  0 1  1 1

Sample Output

Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 0

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

这个题的题意是现在有m台机器,一台机器有n个输入限制(0代表该部件必须没有,1代表该部件必须有,2代表该部件可有可无),n个输出条件。满足输入限制的这台机器才能工作,然后每一台机器都有自己的速度,问这些台机器最多能组成多快的机器,然后每个部分流是多少。

网络流的好题啊。建一个超级源点(输出全部为0,这样才能够与输入全部为0的那些机器接上),一个超级汇点(输入全部为1,这样才能与输出全部为1的机器接上)。然后将每一对能满足输入输出条件的两个点连接起来,容量是两个速度的最小值。求整个网络的最大流。

代码:

#include <iostream>#include <algorithm>#include <cmath>#include <vector>#include <string>#include <queue>#include <cstring>#pragma warning(disable:4996)using namespace std;const int sum = 201;const int INF = 99999999;int cap[sum][sum], flow[sum][sum], in[sum][sum], a[sum], p[sum];int p2, n;void Edmonds_Karp(){int u, t, result = 0;queue <int> s;while (s.size())s.pop();while (1){memset(a, 0, sizeof(a));memset(p, 0, sizeof(p));a[0] = INF;s.push(0);while (s.size()){u = s.front();s.pop();for (t = 0; t <= n + 1; t++){if (!a[t] && flow[u][t]<cap[u][t]){s.push(t);p[t] = u;a[t] = min(a[u], cap[u][t] - flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量}}}if (a[n + 1] == 0)break;result += a[n + 1];for (u = n + 1; u != 0; u = p[u]){flow[p[u]][u] += a[n + 1];flow[u][p[u]] -= a[n + 1];}}cout << result << " ";}int main(){//freopen("i.txt", "r", stdin);  //freopen("o.txt", "w", stdout); int i, j, k, u, v, value;while (scanf("%d%d", &p2, &n) == 2){memset(cap, 0, sizeof(cap));memset(flow, 0, sizeof(flow));for (j = 0; j <= 2 * p2 + 1; j++){in[0][j] = 0;in[n + 1][j] = 1;}for (i = 1; i <= n; i++){for (j = 0; j<2 * p2 + 1; j++){scanf("%d", &in[i][j]);}}for (i = 0; i <= n + 1; i++){for (j = 0; j <= n + 1; j++){if (i == j)continue;bool f = true;for (k = 1; k <= p2; k++){if (!(in[j][k] == 2 || in[i][k + p2] == in[j][k]))f = false;}if (f&&i == 0){cap[i][j] = in[j][0];}else if (f&&j == n + 1){cap[i][j] = in[i][0];}else if (f){cap[i][j] += min(in[i][0], in[j][0]);}}}Edmonds_Karp();int cnt = 0;for (i = 1; i <= n; i++){for (j = 1; j <= n; j++){if (flow[i][j]>0)cnt++;}}cout << cnt << endl;for (i = 1; i <= n; i++){for (j = 1; j <= n; j++){if (flow[i][j]>0){cout << i << " " << j << " " << flow[i][j] << endl;}}}}return 0;}


0 0
原创粉丝点击