20-1-31-最大流-POJ3436-SAP

POJ3436

ACM Computer Factory

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 11575Accepted: 4353Special 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 j, Di,k — output specification for part k.

Constraints

1 ≤ 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

Hint

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

Source

Northeastern Europe 2005, Far-Eastern Subregion

  • 最大流
  • 参考
  • 主要是建图, 图建好了使用最大流的板子就能过.j
    • 图G[i][j]初始化为0
    • 有n台机器编号为0-n-1, 每一台机器分为两个节点, 一个为i, 一个为i+n.
    • 节点i指向i+n的边, 权值为机器每小时能生产的零件的数量
    • 设立一个源点 2*n, 汇点2*n+1
    • 源点指向所有生产零件条件全为0或者2的点, 边的权值初始化为INF
    • 所有产出零件条件为1的点 , 指向源点, 边的权值初始化为INF
    • 调用最大流的SAP算法, 时间复杂度为O(V^2*E)
    • 四种网络流算法的时间复杂度分析
#pragma warning(disable:4996)
#include <cstring>
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
const int maxn = 55;
const int maxp =	15;
const int INF = 0x3f3f3f3f;
/*
* SAP 算法(矩阵形式)
* 结点编号从0 开始
*/
const int MAXN = 55;		//节点数目

int maze[MAXN][MAXN];		//图
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int sap(int start, int end, int nodenum/*节点的数量*/) {
	memset(cur, 0, sizeof(cur));
	memset(dis, 0, sizeof(dis));
	memset(gap, 0, sizeof(gap));
	int u = pre[start] = start;
	int maxflow = 0;		//最大流的结果
	int aug = -1;
	gap[0] = nodenum;
	while (dis[start] < nodenum) {
	loop:
		for (int v = cur[u]; v < nodenum; v++)
			if (maze[u][v] && dis[u] == dis[v] + 1) {
				if (aug == -1 || aug > maze[u][v])aug = maze[u][v];
				pre[v] = u;
				u = cur[u] = v;
				if (v == end) {
					maxflow += aug;
					for (u = pre[u]; v != start; v = u, u = pre[u]) {
						maze[u][v] -= aug;
						maze[v][u] += aug;
					}
					aug = -1;
				}
				goto loop;
			}
		int mindis = nodenum - 1;
		for (int v = 0; v < nodenum; v++)
			if (maze[u][v] && mindis > dis[v]) {
				cur[u] = v;
				mindis = dis[v];
			}
		if ((--gap[dis[u]]) == 0)break;
		gap[dis[u] = mindis + 1]++;
		u = pre[u];
	}
	return maxflow;
}

int cost[maxn];
int g[maxn * 2][maxn * 2];
int G[maxn * 2][maxn * 2];

struct Node {
	int x, y, cost;
};

int main() {
	int p, n;
	while (cin >> p >> n && (p || n)) {
		for (int i = 0; i < n; i++) {
			scanf("%d", &cost[i]);		//机器i每小时生产的零件的数量
			for (int j = 0; j < p; j++) {
				scanf("%d", &g[i][j]);
			}
			for (int j = 0; j < p; j++) {
				scanf("%d", &g[i + n][j]);
			}
		}
		memset(G, 0, sizeof(G));
		//将每一台机器拆分为两个顶点, 顶点i和顶点i+n
			//由顶点i指向顶点i+n构造一条边
			//边的权值为cost[i]
		for (int i = 0; i < n; i++) {
			G[i][i + n] = cost[i];
		}
		//源点的标号设置为2*n和2*n+1.
			//源点和所有的对部件要求为0的顶点或者
			//存在2的顶点之间连上边
		for (int i = 0; i < n; i++) {
			 bool flag = true;
			for (int j = 0; j < p; j++) {
				if (!(g[i][j] == 0 || g[i][j] == 2)) {
					flag = false;
					break;
				}
			}
			if (flag) {
				G[2 * n][i] = INF;
			}
		}
		//汇点和所有的对部件要求为1的顶点连上边
			//边的权值为INF
		for (int i = n; i < 2 * n; i++) {
			int flag = true;
			for (int j = 0; j < p; j++) {
				if (g[i][j] != 1) {
					flag = false;
					break;
				}
			}
			if (flag) {
				G[i][2 * n + 1] = INF;
			}
		}
		//如果一个机器的产出和另一个机器的产入相接,
			//那么将这两个机器相连,权值为0, 否则为INF
		for (int i = n; i < 2 * n; i++) {//产出
			for (int j = 0; j < n; j++) {
				bool flag = true;
				for (int k = 0; k < p; k++) {
					if (!(g[i][k] == g[j][k] || g[j][k] == 2)) {
						flag = false;
						break;
					}
				}
				if (flag) {
					G[i][j] = INF;
				}	
			}
		}
		for (int i = 0; i < 2 * n + 2; i++) {
			for (int j = 0; j < 2 * n + 2; j++) {
				maze[i][j] = G[i][j];
			}
		}
		/*for (int i = 0; i < 2 * n + 2; i++) {
			for (int j = 0; j < 2 * n + 2; j++) {
				if(maze[i][j]!=INF)
				cout << maze[i][j] << "\t";
				else {
					cout << "INF" << "\t";
				}
			}
			cout << "\n";
		}*/
		//调用SAP算法模板求最大流
		int res = sap(2 * n, 2 * n + 1, 2 * n + 2);
		cout << res << " ";
		int cnt = 0;
		vector<Node>q;
		for (int i = n; i < 2 * n; i++) {//出点
			for (int j = 0; j < n; j++) {//入点
				if (i - n != j && G[i][j] != maze[i][j]) {
					cnt++;
					Node tmp;
					tmp.x = i - n + 1;
					tmp.y = j + 1;
					tmp.cost = G[i][j] - maze[i][j];
					q.push_back(tmp);
				}
			}
		}
		cout << cnt << "\n";
		for (int i = 0; i < q.size(); i++) {
			cout << q[i].x << " " << q[i].y << " " << q[i].cost << "\n";
		}
	}
	return 0;
}
/*
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
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值