ACM Computer Factory

题目

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 Pnumbers 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,PDi,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.

样例

这是道网络最大流首先拆点,因为点上有容量限制,在拆出的两点间连一条边,容量为产量;如果输入规格中没有1,则与超级源点相连,容量为无穷大;如果输出规格中没有0,则与超级汇点相连,容量为无穷大;判断和其他机器的输入输出是否匹配,能匹配上则建边,容量为无穷大;在该图上跑一遍最大流,容量大于零的边集即为方案。
 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
#define maxn 55
struct node{
	int in[12], out[12], flow;
}G[55];
int map1[maxn][maxn], pre[maxn], vis[maxn], n;

int EK()
{
	int i, ans = 0, now, min1;
	queue <int> q;
	while (1)  {
		memset(pre, -1, sizeof(pre));
		memset(vis, 0, sizeof(vis));
		while (!q.empty()){
			q.pop();
		}
		q.push(0);
		vis[0] = 1;
		while (!q.empty()){
			now = q.front();
			q.pop();
			if (now == n + 1) {
				break;
			}
			for (i = 0; i <= n + 1; i++){
				if (!vis[i] && map1[now][i] > 0){
					pre[i] = now; 
					vis[i] = 1;  
					q.push(i);  
				}
			}
		}
		if (!vis[n + 1]){
			break; 
		}
		min1 = INF;
		for (i = n + 1; i != 0; i = pre[i]){
			if (map1[pre[i]][i] < min1)  {
				min1 = map1[pre[i]][i];
			}
		}
		ans += min1;
		for (i = n + 1; i != 0; i = pre[i]){
			map1[pre[i]][i] -= min1;
			map1[i][pre[i]] += min1;
		}
	}
	return ans;
}
int main(){
	int p, map2[maxn][maxn], flat, an, i, j, k;
	while (scanf("%d%d", &p, &n) != EOF){
		int cut = 0, ji[maxn][3];
		for (i = 1; i <= n; i++){
			scanf("%d", &G[i].flow);
			for (j = 0; j < p; j++){
				scanf("%d", &G[i].in[j]);
			}
			for (j = 0; j < p; j++){
				scanf("%d", &G[i].out[j]);
			}
		}
		memset(map1, 0, sizeof(map1));
		for (i = 1; i <= n; i++)
		{
			int fs = 1, ft = 1;
			for (j = 0; j < p; j++)
			{
				if (G[i].in[j] == 1)  {
					fs = 0;
				}
				if (G[i].out[j] == 0)  {
					ft = 0;
				}
			}
			if (fs){
				map1[0][i] = G[i].flow;
			}
			if (ft){
				map1[i][n + 1] = G[i].flow;
			}
			for (j = 1; j <= n; j++){
				if (i != j){
					flat = 1;
					for (k = 0; k < p; k++){
						if (G[i].out[k] + G[j].in[k] == 1){
							flat = 0;
							break;
						}
					}
					if (flat){
						map1[i][j] = min(G[i].flow, G[j].flow);
					}
				}
			}
		}
		memcpy(map2, map1, sizeof(map1));  
		an = EK();
		for (i = 1; i <= n; i++)  {
			for (j = 1; j <= n; j++){
				if (map2[i][j] > map1[i][j]){
					ji[cut][0] = i;
					ji[cut][1] = j;
					ji[cut][2] = map2[i][j] - map1[i][j];
					cut++;
				}
			}
		}
		printf("%d %d\n", an, cut);//输出
		for (i = 0; i < cut; i++){
			printf("%d %d %d\n", ji[i][0], ji[i][1], ji[i][2]);
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值