POJ 3436 ACM Computer Factory 网络流 ʕ •ᴥ•ʔ

ACM Computer Factory

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4328   Accepted: 1449   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,1Si,2...Si,PDi,1Di,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

题意:有N台组装电脑的机器。电脑的组成部分共有P部分。

每台机器有P个输入输出规格。还有一个容量Q;

其中输入规格有三种情况:0,1,2

0:该部分不能存在

1:该部分必须保留

2:该部分可有可无

输出规格有2种情况:0,1

0:该部分不存在

1:该部分存在

要求的是生产电脑最大的台数,就是求网络中的最大流。

思路: 将输入规格 数值 都不为1的存入 map[s][i]=产量, 将输出规格 都为1的存入 map[i][e] =产量

然后 再找 当某机器的输入规格 == 某机器的输出规格 存入map[i][j]= 他们产量的最小值

再 用模板跑一边找到网络流的最大值 同时再开一个数组 记录它所走的路径 就OK了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#define N 0x3f3f3f3f
#define ll long long
using namespace std;
int n,m;
struct ac
{
	int in[20];
	int out[20];
	int w;
}r[510];
int s,e;
int map[55][55];
int mapp[55][55];
int dis[55];
int bfs()
{
	memset(dis,-1,sizeof(dis));
	queue<int>q;
	dis[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=0;i<=e;i++)
		{
			if(dis[i]==-1&&map[u][i])
			{
				dis[i]=dis[u]+1;
				q.push(i);
			}
		}
	}
	if(dis[e]>0)
	return 1;
	return 0;
}
int dfs(int u,int h)
{
	if(u==e||h==0)
	return h;
	int ans,he=0;
	for(int i=0;i<=e;i++)
	{
		if(dis[i]==dis[u]+1&&map[u][i]>0&&(ans=dfs(i,min(h,map[u][i]))))
		{
			map[u][i]-=ans;
			map[i][u]+=ans;
			mapp[u][i]+=ans;
			mapp[i][u]-=ans;
			h-=ans;
			he+=ans;
			if(h==0)
			break;
		}
	}
	return he;
}
int main()
{
	cin>>m>>n;
	memset(map,0,sizeof(map));
	for(int i=1;i<=n;i++)
	{
		cin>>r[i].w;
		for(int j=1;j<=m;j++)
		cin>>r[i].in[j];
		for(int j=1;j<=m;j++)
		cin>>r[i].out[j];
	}
	s=0,e=n+1;
	int flag;
	for(int i=1;i<=n;i++)
	{
		flag=0;
		for(int j=1;j<=m;j++)
		{
			if(r[i].in[j]==1)
			{
				flag=1;
				break;
			}
		}
		if(!flag)
		{
			map[s][i]=r[i].w;
		}
		flag=0;
		for(int j=1;j<=m;j++)
		{
			if(r[i].out[j]!=1)
			{
				flag=1;
				break;
			}
		}
		if(!flag)
		{
			map[i][e]=r[i].w;
		}
	} 
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i!=j)
			{
				flag=0;
				for(int k=1;k<=m;k++)
				{
					if(r[i].out[k]!=r[j].in[k]&&r[j].in[k]!=2)
					{	
						flag=1;
						break;
					}
				} 
				if(!flag)
				{
					map[i][j]=min(r[i].w,r[j].w);
				}
			}
		}
	}
	int sum=0;
	memset(mapp,0,sizeof(mapp));//存图 
	while(bfs())
	{
		sum+=dfs(s,N);
	}
	int num=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(mapp[i][j]>0)//走了这条路 
			num++;
		}
	}
	cout<<sum<<" "<<num<<endl;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(mapp[i][j]>0)
			cout<<i<<" "<<j<<" "<<mapp[i][j]<<endl;
		}
	} 
	return 0;	
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81939524