POJ-3436-ACM Computer Factory(最大流+输出路径,矩阵的遍历)

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

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.

题目大意:其实我也不太懂。。但是从样例推,加上题上一点点的理解,大概是:

首先两个整数p(机器接口数),n(机器数);

之后是n个机器,每个机器首先输入一个Qi意思是能够提供的运算量,之后是p个数,意思是输入的端口,在之后是p个数,意思是输出的端口

输入的端口有三种数:0(该位置不能有输入),1(该位置必须有输入),2(该位置无所谓);

输出的端口有两种数:0(同理),1(同理);

我们要找从输入全是0 - 输出全是 1 之间的能使机器的效率最大 的方案,随便输出一种最大的方案即可;

根据题意,画出样例1的草图:

网络流,看完样例,按照网络流的板子画了画图:很明显,端口之间的联通流量使无限的,有限的使机器(节点)的计算量(容纳量),所以将节看成两个端口之间的限制的流,然后给样例1写成  u v w  的形式:

0 2  15

0 3   10

2/3  7 30

1/3  7  3

很容易就按照样例画出一个网络流的图:

大概就是这样了。。邻接矩阵存图就行,存好图就是板子了,直接敲板子最大流,最后跑完最大流后,按照跑完的图进行路径输出即可,随便怎么输出都行(题目没有顺序要求):

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
//#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

int mp[205][205];//图
int pre[205],a[205][20];//分层,数据
bool vis[205];
int n,p;

bool bfs(int s,int t)
{
	clean(vis,0);
	clean(pre,0);
	queue<int> que;
	que.push(s);
	vis[s]=1;
	while(que.size())
	{
		int u=que.front();
		que.pop();
		if(u==t)
			return 1;
		for(int i=0;i<=t;++i)
		{
			if(vis[i]==0&&mp[u][i])
			{
				vis[i]=1;
				pre[i]=u;
				que.push(i);
			}
		}
	}
	return 0;
}

void Find(int s,int t)
{
	int ans=0;
	while(bfs(s,t))//分层
	{
		int res=INF;
		for(int i=t;i!=s;i=pre[i])
			res=min(res,mp[pre[i]][i]);
		for(int i=t;i!=s;i=pre[i])
		{
			mp[pre[i]][i]-=res;
			mp[i][pre[i]]+=res;
		}
		ans=ans+res;
	}
	cout<<ans<<" ";
}

int main()
{
	while(cin>>p>>n)
	{
		clean(mp,0);
		for(int i=1;i<=n;++i)
		{
			cin>>mp[i][i+n];
			for(int j=1;j<=p;++j)//输入 
				cin>>a[i][j];
			for(int j=1;j<=p;++j)//输出 
				cin>>a[i+n][j];
		}
		for(int i=1;i<=n;++i)//源 和 汇 
		{
			bool f1=1,f2=1;
			for(int j=1;j<=p;++j)
			{
				if(a[i][j]==1)
					f1=0;
				if(a[i+n][j]!=1)
					f2=0;
			}
			if(f1)
				mp[0][i]=INF;
			if(f2)
				mp[i+n][2*n+1]=INF;
		}
		
		for(int i=1;i<=n;++i)//点 - 点 
		{
			for(int j=1;j<=n;++j)
			{
				bool f=1;
				for(int k=1;k<=p;++k)
				{
					if(a[i+n][k]+a[j][k]==1)
					{
						f=0;
						break;
					}
				}
				if(f)
					mp[i+n][j]=INF;
			}
		}
//		for(int i=0;i<=2*n+1;++i)//Debug
//		{
//			for(int j=0;j<=2*n+1;++j)
//				cout<<mp[i][j]<<" ";
//			cout<<endl;
//		}
		Find(0,2*n+1);//找到最大流
		int cnt=0,a1[205],a2[205],a3[205];
		for(int i=1;i<=n;++i)//路径
		{
			for(int j=1;j<=n;++j)
			{
				if(i!=j&&mp[j][i+n]>0)
				{
					++cnt;
					a1[cnt]=i;
					a2[cnt]=j;
					a3[cnt]=mp[j][i+n];
				}
			}
		}
		cout<<cnt<<endl;
		for(int i=1;i<=cnt;++i)
			cout<<a1[i]<<" "<<a2[i]<<" "<<a3[i]<<endl;
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81838492