POJ 3436 ACM Computer Factory 最大流



ACM Computer Factory
Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 PN, then N descriptions of the machines. The description of ith machine is represented as by 2 P + 1 integersQiSi,1Si,2...Si,PDi,1Di,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 ABW, 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

Hint

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


题意:有n个机器,每个机器有2*p+1个参数值,第一个表示这个机器的最大容量,

就是最大可加工零件的数量,然后的p个参数表示,输入条件控制,再之后的p个

参数表示输出情况。在输入部分有三种数字,0,1,2,表示对能够送入当前第i

个机器加工的零件的P个部分的要求,0表示这个部分一定不能有,1表示这个部

分一定要有,2表示这个部分可有可无。对于输出部分,只有两种状态就是0和1。

0表示这个位置输出时没有部件,1表示这个位置输出时有部件。

最后输出时,只有当输出部分p个位置都是1,才算完成。

那么想要形成一个流水线,即产品从一个机器出来,再从另一个机器进去,要满

足的条件是,前一个机器的输出必须满足后一个机器的输入。那么怎么才算满足呢,

由于2比较特别可以在必要时候看成0或者1,所以可以不管它。那么对于0和1,

00 ,11的时候肯定是满足的,然而对于01或者10就不能满足了。于是,只要前

一个机器的后P位中任何一位和后一个机器的前P位中对应位置相加等于1,那么

这两个机器就无法连边。让所有能连接起来的机器连成一张图,题目要求一次

最多能加工多少产品并且输出最大流的路径。

考虑到每台机器都有容量,所以把一台机器分成两个点,中间建一条容量的边。

同时如果一台机器的输出符合另一台机器的输入,则建一条容量无穷大的边。

同时要增加源点和汇点。输入没有1的连接源点,输出全部是1的连接汇点。

代码如下:

//因为是多源点汇点,所以加入一个源点(0)与汇点(n+1),ek 算法
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define N 120
#define inf 0x3f3f3f3f
int pre[N], perf[N], data[N][N], n; //data[][]:满足条件的机器则连边建图 
int cap[N][N];//最大容量  
int flow[N][N];//实际流量
int ek()
{
	memset(flow, 0, sizeof(flow));
	int i, j, k, max_flow = 0;
	while (1)
	{
		memset(pre, -1, sizeof(pre));//边的前驱
		memset(perf, 0, sizeof(perf));
		queue<int >q;
		q.push(0);
		perf[0] = inf;
		while (!q.empty())
		{
			int s = q.front();  q.pop();
			for (i = 1; i <= n; i++){
				if (cap[s][i] > flow[s][i] && !perf[i]){
				   pre[i] = s;
				   perf[i] = min(perf[s], cap[s][i] - flow[s][i]);
				   q.push(i);
				}
			}
			if (perf[n])   break;//到汇点就先退出
		}
		if (!perf[n])  break;//到不了汇点退出
		for (i = n; i != 0; i = pre[i]){//更新流量
			flow[pre[i]][i] += perf[n];//正加(注意:反了)
			flow[i][pre[i]] -= perf[n];//反减(注意:反了)
		}
		max_flow += perf[n];//最大流
	}
	return max_flow;
}
struct l
{
	int s, t, f;
}lines[N];
int main()
{
#ifdef OFFLINE
	freopen("t.txt", "r", stdin);
#endif
	int s, t, p, i, j, k;
	while (~scanf("%d%d", &p, &n))
	{
		s = 0, t = n + 1;
		for (i = 1; i < 2 * p + 1; i++)//初始化源点和汇点
		{
			data[s][i] = 0;//源点出发
			data[t][i] = 1;//汇点出发
		}
		for (i = 1; i <= n; i++){
			for (j = 0; j < 2 * p + 1; j++){
				scanf("%d", &data[i][j]);//拆点
			}
		}
		n += 1;//共有 n 个点
		memset(cap, 0, sizeof(cap));
		for (i = 0; i <= n; i++){
			for (j = 0; j <= n; j++){
				if (i == j)   continue;
				int flag = 0;
				for (k = 1; k <= p; k++){
					if (data[i][k + p] + data[j][k] == 1){
                                   //机器 i 输出和机器 j 输入的和等于1则不符合
						flag = 1;   break;
					}
				}
				if (flag)  continue;
				if (i == 0)   cap[i][j] = data[j][0];//源点
				else if (j == n)  cap[i][j] = data[i][0];//汇点
				else   cap[i][j] += min(data[i][0], data[j][0]);
				//注意一定是多条流的和(中间点)(直接赋值也可以)
			}
		}
		printf("%d ", ek());
		int tol = 0;
		for (i = 1; i < n; i++){
			for (j = 1; j < n; j++){
				if (flow[i][j]>0){
					lines[tol].s = i;
					lines[tol].t = j;
					lines[tol].f = flow[i][j];
					tol++;
				}
			}
		}
		printf("%d\n", tol);
		for (i = 0; i < tol; i++)
			printf("%d %d %d\n", lines[i].s, lines[i].t, lines[i].f);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值