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;
}


發佈了104 篇原創文章 · 獲贊 28 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章