POJ 3436 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 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.


题目大意:

这道题其实并没有看明白啥意思,瞻仰了大神们的博客才明白的。。。大概就是说,有一些生产计算机的机器,每台机器有一个输入规格和输出规格,只有符合输入规格的计算机才可以进入此机器进行深加工,每台机器还有一个生产速度,要求连接这些机器形成一条生产线,求出最大生产速度,并输出生产线的组成(相连的两台机器序号+这条线的生产速度),由于该题是Special Judge所以不必考虑输出的顺序。


解题思路:

这是训练计划上一道典型的最大流问题,看网上好多大神的做法都是拆点。。。感觉貌似不必拆点,也看到了几个大神的做法也没有拆点,直接引入超级源点和超级汇点,再根据机器之间的对应关系建图跑最大流就可以了,具体建图规则:

(1)如果某机器i的输入规格全部部件都不为1,说明该机器不需要计算机拥有必要部件,那么建立一条超级源点到i的边,权值(容量)为i机器的生产速度。

(2)如果某机器i的输出规格全部部件都为1,说明通过该机器的计算机为成品,那么建立一条i到超级汇点的边,权值(容量)为i机器的生产速度。

(3)判断机器i与机器j是否可以建立生产线,如果机器i与机器j对应的部件加和全不为1(列出全部情况就可以看出来了),那么可以建立一条i到j的边,权值为min(i生产速度,j生产速度)。


根据以上规则建好图,跑一遍最大流,然后将最大链输出,方法是遍历边集合,因为这个边集合包含了超级源点和超级汇点,所以包含这两点的边要忽略,如果某条边的容量和流量都大于0,那么输出该边以及该边流量,需要注意的是此题要先输出边的条数再输出边信息,所以先临时存下来,再输出。


#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <map>
#include <set>
#include <bitset>
#include <ctime>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <ctime>

#define STD_REOPEN() freopen("../in.in","r",stdin)
#define STREAM_REOPEN fstream cin("../in.in")
#define INF 0x3f3f3f3f
#define _INF 63
#define eps 1e-4
#define MAX_V 100010
#define MAX_P 110
#define MAX_E 4001000
#define MAX 32000
#define MOD_P 3221225473
#define MOD 9901

using namespace std;

struct edge
{
	int from,to,cap,flow;
	edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct EdmondsKarp	//最大流增广路模板
{
	int n,m;
	vector<edge> edges;
	vector<int> g[MAX_P];
	int a[MAX_P];
	int p[MAX_P];
	void init(int n)
	{
		this->n=n;
		for(int i=0;i<=n;i++)
			g[i].clear();
		edges.clear();
	}
	void AddEdge(int from,int to,int cap)
	{
		edges.push_back(edge(from,to,cap,0));
		edges.push_back(edge(to,from,0,0));
		m=edges.size();
		g[from].push_back(m-2);
		g[to].push_back(m-1);
	}
	int MaxFlow(int s,int t)
	{
		int flow=0;
		while(1)
		{
			memset(a,0,sizeof(a));
			queue<int> q;
			q.push(s);
			a[s]=INF;
			while(!q.empty())
			{
				int x=q.front();
				q.pop();
				int len=g[x].size();
				for(int i=0;i<len;i++)
				{
					edge &e=edges[g[x][i]];
					if(!a[e.to]&&e.cap>e.flow)
					{
						p[e.to]=g[x][i];
						a[e.to]=min(a[x],e.cap-e.flow);
						q.push(e.to);
					}
				}
				if(a[t])
					break;
			}
			if(!a[t])
				break;
			for(int u=t;u!=s;u=edges[p[u]].from)
			{
				edges[p[u]].flow+=a[t];
				edges[p[u]^1].flow-=a[t];
			}
			flow+=a[t];
		}
		return flow;
	}
}fl;

int s[55][15];
int opt[55][15];

int main()
{
	//STD_REOPEN();
	int n,p;
	while(~scanf("%d %d",&p,&n))
	{
		int ss=0,tt=n+1;
		fl.init(n+2);
		for(int i=1;i<=n;i++)
			for(int j=0;j<2*p+1;j++)
				scanf("%d",&s[i][j]);	//s[i][0]表示i机器的生产速度,其余位置与输入匹配
		for(int i=1;i<=n;i++)
		{
			bool fs,ft;
			fs=ft=true;
			for(int j=1;j<=p;j++)
			{
				if(s[i][j]==1)	//建图规则1
					fs=false;
				if(s[i][j+p]==0)		//建图规则2
					ft=false;
			}
			if(fs)		//超级源点与生产线起点
				fl.AddEdge(ss,i,s[i][0]);
			if(ft)		//生产线终点与超级汇点
				fl.AddEdge(i,tt,s[i][0]);
			for(int j=1;j<=n;j++)
			{
				if(i==j)
					continue;
				bool f=true;
				for(int k=1;k<=p;k++)
					if(s[i][k+p]+s[j][k]==1)
						f=false;
				if(f)		//建图规则3
					fl.AddEdge(i,j,min(s[i][0],s[j][0]));
			}
		}
		int ans=fl.MaxFlow(ss,tt);	//取得最大流
		int cnt=0;
		int len=fl.edges.size();
		//cout<<"len = "<<len<<endl;
		for(int i=0;i<len;i++)
		{
			edge t=fl.edges[i];
			if(t.cap>0&&t.flow>0)	//枚举边集里符合输出条件的边
			{
				if(t.from==ss||t.to==tt)
					continue;
				opt[cnt][0]=t.from;
				opt[cnt][1]=t.to;
				opt[cnt][2]=t.flow;
				cnt++;
			}
		}
		printf("%d %d\n",ans,cnt);
		for(int i=0;i<cnt;i++)
			printf("%d %d %d\n",opt[i][0],opt[i][1],opt[i][2]);
	}

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值