POJ3436——建图和最大流路径记录

题目链接:http://poj.org/problem?id=3436

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.

 

题目翻译

为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产线来生产这样的电脑,随着比赛规模的越来越大,生产线的生产能力不能满足需要,所以说ACM董事会想要重新建造一条生产线。 生产线是全自动化的,所以需要机器来组成生产线,给定有多少中种机器,标准ACM用电脑有多少部份,每种机器将什么样的ACM电脑半成品处理成什么样的电脑半成品(对于输入的电脑半成品,每部分有0,1,2三种状态:代表着 0、这部分必须没有我才能处理,1、这部分必须有我才能处理,2、这部分有没有我都能处理。对于输出的电脑半成品有0,1两种状态:代表着0,处理完后的电脑半成品里没有这部分,1、处理完的电脑半成品有这部分),每一个机器每小时可以处理Q个半成品(输入数据中的Qi)。 求组装好的成产线的最大工作效率(每小时最多生成多少成品,成品的定义就是所有部分的状态都是“1”) 第一行输入两个数:一个P代表有P个零件, 一个 N代表有N台机器。 接下来N行,每行第一个数字有Qi,代表 第i个零件每小时能加工的半成品零件个数。然后是2*P个数字,前P个数字是加工前半成品需要满足的条件,后P个数表示加工后的半成品的数量。

 

题意有点难理解

其实就是给你一些机器,机器加工前需要的零件条件,加工后的零件条件,以及加工的速度。

让你求出把000加工到111的最大零件个数,以及用到的机器种类和路径,

很容易知道000就是最大流的源点,111就是最大流的汇点。如果一个机器加工后的半成品零件满足另一个机器加工前的条件,那么这两个机器建边,边的权值为两个机器的最小速度,最后求出最大流并且输出最大流的路径就行了。

//这个题采用的是邻接矩阵建图和EK算法跑最大流 
#include <iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int INF=0x3f3f3f3f;
struct Point{
	int a[13];
	int b[14];
	int w;
}p[53];
int G[53][53],pre[53],n,P,vis[53],map[53][53];
int EK(){
	queue<int> q;
	int ans=0;
	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()){
			int now = q.front();
			q.pop();
			if(now==n+1) break;
			for(int i = 0;i<=n+1;++i){
				if(!vis[i]&&G[now][i]>0){
					pre[i]=now;
					vis[i]=1;
					q.push(i);
				}
			}
		}
		if(!vis[n+1]) break;
		int minn=INF;
		for(int i = n+1;i!=0;i=pre[i])
			minn=min(G[pre[i]][i],minn);
		ans+=minn;
		for(int i = n+1;i!=0;i=pre[i]){
			G[pre[i]][i]-=minn;
			G[i][pre[i]]+=minn;
		}
	}
	return ans;
}
int main(int argc, char** argv) {
	while(~scanf("%d%d",&P,&n)){
		for(int i = 1;i<=n;++i){
			scanf("%d",&p[i].w);
			for(int j = 1;j<=P;++j){
				scanf("%d",&p[i].a[j]);
			}
			for(int j = 1;j<=P;++j){
				scanf("%d",&p[i].b[j]);
			}
		}
		memset(G,0,sizeof(G));
		for(int i = 1;i<=n;++i){
			int f1=1,f2=1;
			for(int j = 1;j<=P;++j){
				if(p[i].a[j]==1) f1=0;
				if(p[i].b[j]==0) f2=0;
			}
			if(f1) G[0][i]=p[i].w;
			if(f2) G[i][n+1]=p[i].w;//处理汇点和源点的情况 
			for(int j = 1;j<=n;++j){//枚举除了自身的其他机器,
			//如果当前机器加工后的半成品是另一个机器加工前的半成品。则建边
			//权值为这两个机器生产零件数的最小值 
				if(i!=j){
					int flag=1;
					for(int k = 1;k<=P;++k){
						if(p[i].b[k]+p[j].a[k]==1){ 
							flag=0;
							break;
						}
					}
					if(flag)
						G[i][j]=min(p[i].w,p[j].w);
				} 
			}
		}
		memcpy(map,G,sizeof(G));//存储初始图的邻接矩阵 
		int maxflow=EK();//求出最大流(EK可以做) 
		int a[107][3];
		int cnt=0;
		for(int i = 1;i<=n;++i){
			for(int j = 1;j<=n;++j){
				if(map[i][j]>G[i][j]){//如果跑完最大流后,有一条边的流减小,表示最大流经过这条边。 
					a[cnt][0]=i;
					a[cnt][1]=j;
					a[cnt++][2]=map[i][j]-G[i][j];
				}
			}
		}
		printf("%d %d\n",maxflow,cnt);
		for(int i = 0;i<cnt;++i)
			printf("%d %d %d\n",a[i][0],a[i][1],a[i][2]);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值