【最大流+输出路径】POJ-3436 ACM Computer Factory

34 篇文章 0 订阅
30 篇文章 0 订阅
ACM Computer Factory
Time Limit: 1000MS Memory Limit: 64M
    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 Pnumbers 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,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 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

Hint

Bold texts appearing in the sample sections are informative and do not form part of the actual data.
————————————————————失望的分割线————————————————————
前言:之前建图没有考虑好数量关系,裸着来结果惨死。甚至在网上找到有人“不拆点”AC的代码,但是有一组数据可以hack掉:
2 4
10 0 0 0 1
10 0 0 0 0
10 0 1 1 1
10 0 1 1 1
思路:举例来说,X点从A点和B点接受了一些流量,即将流进Y点和Z点,那么问题来了:
A点/B点能发出多少流量?X点能接受/发出多少流量?Y点/Z点能接受多少流量?
A和B发出的流量可能很大,超过了X点的工作效率,X点流进Y点和Z点的流量和又不能超过X点的实际产量。
因此需要将X点拆为X和X'点,两点之间权值为X的效率。其余的顶点之间权值完全可以设置INF。
输入输出规格也就可以分配给X和X'这两个点。对于源点和汇点,源点输出全为0,汇点输入全为1。
还有一个难点,在于路径的记录。这就要理解Dinic模板了。因为每条反向弧初始容量设置为0,因此完成增广之后,反向弧的容量即为路径。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 55, M = 1e5;
int tot, head[N], cur[N], lev[N], q[N], s[N], S, T;
struct Node {
	int u, v, cap;
	int next;
}edge[M];
int P, n, table[N<<1][10] = {0};
int path[N][N] = {0}, line = 0;

void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
}

void add(int u, int v, int c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));
	lev[S] = 0;
	int fron = 0, rear = 0;
	q[rear++] = S;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap && lev[v] == -1) {
				lev[v] = lev[u] + 1;
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

void Path(int u, int v, int w)
{
	if(u <= n || v > n) return ;//不是应该记录的路径
	u -= n;//节约内存
	if(u != S && v != T) {
		if(path[u][v]) {
			path[u][v] = max(path[u][v], w);
		}
		else {
			path[u][v] = w;
			line++;
		}
	}
}

int Dinic()
{
	int ret = 0;
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int u = S, top = 0;
		while(1) {
			if(u == T) {
				int mini = INF, loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].cap) {
						mini = edge[s[i]].cap;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;//反向弧的扩充,即为要记录的路径
					Path(edge[s[i]].u, edge[s[i]].v, edge[s[i]^1].cap);
				}
				ret += mini;
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap && lev[v] == lev[u] + 1) break;
			}
			if(i != -1) {
				s[top++] = i;
				u = edge[i].v;//更新u为下一点
			}
			else {
				if(!top) break;
				lev[u] = -1;
				u = edge[s[--top]].u;
			}
		}
	}
	return ret;
}

int main()
{
#ifdef J_Sure
	//freopen("000.in", "r", stdin);
	//freopen("888.out", "w", stdout);
#endif
	scanf("%d%d", &P, &n);
	S = 0; T = n<<1|1;
	init();
	int c;
	for(int i = 1; i <= n; i++) {
		scanf("%d", &c);
		add(i, n+i, c); add(n+i, i, 0);
		for(int p = 0; p < P; p++) {
			scanf("%d", &table[i][p]);
		}
		for(int p = 0; p < P; p++) {
			scanf("%d", &table[n+i][p]);
		}
	}
	for(int i = 1; i <= n; i++) {
		bool flag_s = true, flag_t = true;
		for(int p = 0; p < P; p++) {
			if(table[i][p] == 1) flag_s = false;
			if(table[n+i][p] != 1) flag_t = false;
		}
		if(flag_s) {
			add(S, i, INF); add(i, S, 0);
		}
		if(flag_t) {
			add(n+i, T, INF); add(T, n+i, 0);
		}//满足与源点/汇点相连的条件
		for(int j = 1; j <= n; j++) if(i != j) {
			bool flag = true;
			for(int p = 0; p < P; p++) {
				if(table[n+i][p] + table[j][p] == 1) {
					flag = false; break;
				}//0与1不能搭配
			}
			if(flag) {
				add(n+i, j, INF); add(j, n+i, 0);
			}
		}
	}
	int ret = Dinic();//注意,尽量避免直接输出一个有返回值的函数
	printf("%d %d\n", ret, line);
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) if(path[i][j]) {
			printf("%d %d %d\n", i, j, path[i][j]);
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值