(最大流+拆点+无源点汇点)ACM Computer Factory

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
Hint
Bold texts appearing in the sample sections are informative and do not form part of the actual data.
题目大意是ACM竞赛用的电脑在生产时需要安装多个零件,而工厂有多个安装零件的加工机器,每个机器在加工前对这个电脑有一定要求,可能需要这个电脑已经安装好某个零件,可能需要这个电脑没安装这个零件,也可能装不装这个零件都没有影响。每个机器会说明加工完后的电脑的零件配备情况,当所有零件都安装完毕时,电脑就能投入使用。而每个加工机器都有他的工作性能(以计算机每小时为单位),题目求一个工厂的最大整体性能
首先题目没有设置源点和汇点,所以需要自行设置超级源点和超级汇点。
将加工机器视为一个点,则这些点带有权值,因此需要拆点。
将超级源点连接零件要求只有0或2的点,将超级汇点连接电脑加工完成的点(即产出全为1的点),然后跑最大流

#include<iostream>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<cstdio>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=10000+5;
const int maxm=100000+5;
int head[maxn],dis[maxn],ne,n,m,S,T,ans,a1[maxn],b1[maxn],c1[maxn];
struct node
{
	int in[11],out[11];
	int in1,out1;
	int flow;
}a[maxn];
void init()
{
	ans=0;
	ne=0;
	memset(head,-1,sizeof(head));
}
struct edge{int v,w,nxt,ws,us;}G[maxm<<1];
void add(int u,int v,int w)
{
	G[ne]=(edge){v,w,head[u],w,u};
	head[u]=ne++;
	G[ne]=(edge){u,0,head[v],0,v};
	head[v]=ne++;
}
int bfs()
{
	memset(dis,-1,sizeof(dis));
	queue<int> q;
	q.push(S);
	dis[S]=0;
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=G[i].nxt)
		{
			int v=G[i].v;
			if(dis[v]==-1&&G[i].w>0)
			{
				dis[v]=dis[u]+1;
				q.push(v);
			}
		}
	}
	return dis[T]!=-1;
}
int dfs(int u,int exp)
{
	if(u==T) return exp;
	int flow=0,tmp=0;
	for(int i=head[u];i!=-1;i=G[i].nxt)
	{
		int v=G[i].v;
		if(dis[v]==dis[u]+1&&G[i].w>0)
		{
			tmp=dfs(v,min(G[i].w,exp));
			if(!tmp) continue;
			exp-=tmp;
			flow+=tmp;
			G[i].w-=tmp;
			G[i^1].w+=tmp;
			if(!exp) break;
		}
	}
	return flow;
}
int main()
{
	
	while(~scanf("%d%d",&n,&m))
	{
		init();
		T=m*2+1;
		S=0;
		for(int i=1;i<=m;i++)
		{
			scanf("%d",&a[i].flow);
			add(i,i+m,a[i].flow);
			a[i].in1=0;
			for(int j=0;j<n;j++)
			{
				scanf("%d",&a[i].in[j]);
				if(a[i].in[j]==1) a[i].in1++;
			}
			a[i].out1=0;
			for(int j=0;j<n;j++)
			{
				scanf("%d",&a[i].out[j]);
				if(a[i].out[j]==0) a[i].out1++;
			}
		}
		for(int i=1;i<=m;i++)
		{
			if(a[i].in1==0) add(0,i,inf);
			if(a[i].out1==0) add(i+m,T,inf);
			for(int j=1;j<i;j++)
			{
				int flag=0;
				for(int k=0;k<n;k++)
				{
					if(a[j].out[k]+a[i].in[k]==1)
					{
						flag=1;
						break;
					}
				}
				if(!flag) add(j+m,i,inf);
				flag=0;
				for(int k=0;k<n;k++)
				{
					if(a[j].in[k]+a[i].out[k]==1)
					{
						flag=1;
						break;
					}
				}
				if(!flag) add(i+m,j,inf);
			}
		}
		while(bfs())
		{
			ans+=dfs(S,inf);
		}
		int x=0;
		for(int i=m*2;i<ne;i++)
		{
			if(G[i].ws>G[i].w&&G[i].us-m>0&&G[i].v!=2*m+1)
			{
				a1[x]=G[i].us-m;
				b1[x]=G[i].v;
				c1[x]=G[i].ws-G[i].w;
				x++;
			}
		}
		printf("%d %d\n",ans,x);
		for(int i=0;i<x;i++)
		{
			printf("%d %d %d\n",a1[i],b1[i],c1[i]);
		}
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值