(最大流) poj3436 ACM Computer Factory

ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5123 Accepted: 1755 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 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

第一天接触网络流,这道题是标准的最大流问题。
将机器看做节点,建立一个超级源和超级汇,求超级源到超级汇的最大流即可。
由于容量信息存储在节点上,我采取了拆点的方法,每个机器拆成输入端和输出端两个点,这两点连一条容量为机器工作容量的边。接下来,将输入端为初始状态的节点和超级源点之间连一条容量无穷大的边,将输出端为目标状态的节点和超级汇点之间连一条容量无穷大的边,再将每一台机器的输出端节点和符合匹配的输入端输入端节点之间连一条容量无穷大的边,图便构造完毕。
求最大流采用了EK算法,最后记录路径时利用残余网络求解。
代码如下:

Memory 220K Time 16MS

#include<stdio.h>
#include<queue>
using namespace std;

const int inf=999999999;
int map[105][105];
int pre[105];
int n,p;
queue<int>q;

struct mac{
	int cap;
	int input[15];
	int output[15];
}m[55];

bool ok(int a,int b){
	for (int i=0;i<p;i++)
		if (m[a].output[i]+m[b].input[i]==1)
			return false;

	return true;
}

bool bfs(int src,int des){
	memset(pre,-1,sizeof(pre));
	while(!q.empty())
		q.pop();
	pre[src]=0;
	int index;
	q.push(src);
	while(!q.empty()){
		index=q.front();
		q.pop();
		for(int i=0;i<=2*n+1;i++){
			if (pre[i]==-1 && map[index][i]>0){
				pre[i]=index;
				if (i==des)
					return true;
				q.push(i);
			}
		}
	}
	return false;
}

int maxflow(int src,int des){
	int mflow=0;
	while(bfs(src,des)){
		int minflow=inf;
		for (int i=des;i!=src;i=pre[i]){
			minflow=min(minflow,map[pre[i]][i]);
		}
		for (int i=des;i!=src;i=pre[i]){
			map[pre[i]][i]-=minflow;
			map[i][pre[i]]+=minflow;
		}
		mflow+=minflow;
	}
	return mflow;
}

int main(){
	int src,des;
	
	scanf("%d%d",&p,&n);
	memset(map,0,sizeof(map));

	for (int i=1;i<=n;i++){
		scanf("%d",&m[i].cap);
		for (int j=0;j<p;j++)
			scanf("%d",&m[i].input[j]);
		for (int j=0;j<p;j++)
			scanf("%d",&m[i].output[j]);
		map[2*i-1][2*i]=m[i].cap;
	}

	src=0;
	des=2*n+1;

	for (int i=1;i<=n;i++){
		bool flag=true;
		for (int j=0;j<p;j++)
			if (m[i].input[j]==1){
				flag=false;
				break;
			}
		if (flag){
			map[src][2*i-1]=inf;
		}
	}

	for (int i=1;i<=n;i++){
		bool flag=true;
		for (int j=0;j<p;j++)
			if (m[i].output[j]==0){
				flag=false;
				break;
			}
		if (flag){
			map[2*i][des]=inf;
		}
	}

	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++){
			if (i!=j && ok(i,j))
				map[2*i][2*j-1]=inf;
		}
	
	int result=maxflow(src,des);
	int counter=0;

	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (i!=j && map[2*i][2*j-1]<inf && map[2*i][2*j-1]>0)
				counter++;
	printf("%d %d\n",result,counter);
	for (int i=1;i<=n;i++)
		for (int j=1;j<=n;j++)
			if (i!=j && map[2*i][2*j-1]<inf && map[2*i][2*j-1]>0)
				printf("%d %d %d\n",i,j,inf-map[2*i][2*j-1]);

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值