POJ3436(ACM Computer Factory)

ACM Computer Factory

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

思路

这道题很容易看出来是个最大流,难点在于建图,由于每个机器是将一定的半成品加工成半成品或者成品,所以每台机器既可以作为接受其他机器半成品的终点,同时也可以作为发送加工半成品的起点。还需要借助超级源点和超级汇点。将所有成品机器汇聚到同一个点m+1,所有起点机器汇聚到同一个点0。然后从跑Dinic或者EK算法。最后判断一下哪条边的容量变了就说明这太机器参与了运作记录即可。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 205;
struct info{
	int a[15];			
	int b[15];	
	int w;		
}s[maxn];
int p[maxn][maxn];
int g[maxn][maxn];
struct edge{
	int from,to,f;
	edge(){}
	edge(int a,int b,int c){
		from = a;to = b;f = c;
	}
};
vector<edge>v;
int dep[maxn];
int bfs(int s,int t)
{
	memset(dep,0,sizeof(dep));
	queue<int>q;
	q.push(s);dep[s] = 1;
	while(!q.empty()){
		int x = q.front();
		q.pop();
		for(int i = 0;i <= t;i++){
			if(p[x][i] && !dep[i]){
				dep[i] = dep[x] + 1;
				q.push(i);
			}
		}
	}
	return dep[t];
}
int dfs(int s,int flow,int t)
{
	if(s == t)		return flow;
	int sum = 0;
	for(int i = 0;i <= t;i++){
		if(p[s][i] && dep[i] == dep[s] + 1){
			int k = dfs(i,min(flow-sum,p[s][i]),t);
			if(k){
				p[s][i] -= k;
				p[i][s] += k;
				sum += k;
				if(sum == flow)		break;
			}
		}
	}
	if(!sum)	dep[s] = -1;
	return sum;
}
int Dinic(int s,int t)
{
	int sum = 0;
	while(bfs(s,t)){
		sum += dfs(s,inf,t);
	}
	return sum;
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		memset(s,0,sizeof(s));
		memset(g,0,sizeof(g));
		memset(p,0,sizeof(p));
		v.clear();
		for(int i = 1;i <= m;i++){
			scanf("%d",&s[i].w);
			for(int j = 1;j <= n;j++){		//收到的材料样子
				scanf("%d",&s[i].a[j]);
			}
			for(int j = 1;j <= n;j++){		//做成的材料样子
				scanf("%d",&s[i].b[j]);
			}
		}
		for(int i = 1;i <= m;i++){
			int f1 = 1,f2 = 1;
			for(int j = 1;j <= n;j++){
				if(s[i].a[j] == 1)	f1 = 0;			//全是1,终点 
				if(s[i].b[j] == 0)	f2 = 0;			//全是0,起点 
			}
			if(f1)	p[0][i] += s[i].w;
			if(f2)	p[i][m+1] += s[i].w;
			for(int j = 1;j <= m;j++){
				if(i == j)	continue;
				bool f = true;
				for(int k = 1;k <= n;k++){
					if(s[i].b[k] + s[j].a[k] == 1){		//一个为1,一个为0说明不匹配。
						f = false;
						break;
					}
				}
				if(f)	p[i][j] = min(s[i].w,s[j].w);		//取最小
			} 
		}
		memcpy(g,p,sizeof(g));
		int ans = Dinic(0,m+1);
		for(int i = 1;i <= m;i++){
			for(int j = 1;j <= m;j++){
				if(g[i][j] > p[i][j]){
					v.push_back(edge(i,j,g[i][j]-p[i][j]));
				}
			}
		}
		printf("%d %d\n",ans,v.size());
		for(int i = 0;i < v.size();i++){
			printf("%d %d %d\n",v[i].from,v[i].to,v[i].f);
		}
	}
	return 0;
}

愿你走出半生,归来仍是少年~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值