POJ 3436 ACM Computer Factory(最大流)

                                                                             ACM Computer Factory

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9169 Accepted: 3373 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,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.

题目大意就不说了,我也是看题解的翻译才懂得,只可意会不可言传...

拆点,把每一个机器拆成两个点,边权设为点权,对于输出与输入满足条件的两台电脑,将拆的点与另一台电脑的原点连上一条边,对于输入都为0的点,都连上源点,输出都为1的点,连上汇点,跑最大流。

另一个麻烦的地方就是记录路径,因为我用的不是EK,存储路径并不是用邻接矩阵,所以用一个数组标记一下两台不同的电脑相连的边,最后输出即可。输出路径参考了https://blog.csdn.net/gauss_acm/article/details/43991995

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=110;
const int maxm=10010;
const int inf=0x3f3f3f3f;
struct Node
{
	int to;
	int capa;
	int next;
}edge[maxm];
struct Flow
{
	int flow;
	int in[15];
	int out[15];
}com[maxn];
struct LDJ
{
	int u;
	int v;
	int flow;
}ans[maxn];
int cnt;
int source,sink;
int head[maxn];
int dep[maxn];
bool vis[maxn][maxn];
void init()
{
	memset(head,-1,sizeof(head));
	memset(dep,-1,sizeof(dep));
	memset(vis,false,sizeof(vis));
	cnt=0;
	return;
}
void add(int u,int v,int capa)
{
	edge[cnt].to=v;
	edge[cnt].capa=capa;
	edge[cnt].next=head[u];
	head[u]=cnt++;
	edge[cnt].to=u;
	edge[cnt].capa=0;
	edge[cnt].next=head[v];
	head[v]=cnt++;
	return;
}
bool bfs()
{
	queue<int> que;
	que.push(source);
	memset(dep,-1,sizeof(dep));
	dep[source]=0;
	while(!que.empty())
	{
		int node=que.front();
		que.pop();
		for(int i=head[node];~i;i=edge[i].next)
		{
			int v=edge[i].to;
			if(edge[i].capa>0&&dep[v]==-1)
			{
				dep[v]=dep[node]+1;
				if(v==sink) return true;
				que.push(v);
			}
		}
	}
	return dep[sink]!=-1;
}
int dfs(int node,int minn)
{
	if(node==sink||minn==0)
	{
		return minn;
	}
	int r=0;
	for(int i=head[node];~i;i=edge[i].next)
	{
		int v=edge[i].to;
		if(edge[i].capa>0&&dep[v]==dep[node]+1)
		{
			int tmp=dfs(v,min(edge[i].capa,minn));
			if(tmp>0)
			{
				edge[i].capa-=tmp;
				edge[i^1].capa+=tmp;
				r+=tmp;
				minn-=tmp;
				if(!minn) break;
			}
		}
	}
	if(!r) dep[node]=-1;
	return r;
}
int dinic()
{
	int maxflow=0;
	while(bfs())
	{
		maxflow+=dfs(source,inf);
	}
	return maxflow;
}
int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int p,n;
	while(~scanf("%d%d",&p,&n))
	{
		init();
		source=0;
		sink=2*n+1;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&com[i].flow);
			for(int j=0;j<p;j++)
			{
				scanf("%d",&com[i].in[j]);
			}
			for(int j=0;j<p;j++)
			{
				scanf("%d",&com[i].out[j]);
			}
			add(i,i+n,com[i].flow);
		}
		for(int i=1;i<=n;i++)
		{
			bool flag_s=true;
			bool flag_t=true;
			for(int j=0;j<p;j++)
			{
				if(com[i].in[j]==1) flag_s=false;
				if(com[i].out[j]==0) flag_t=false;
			}
			if(flag_s)
			{
				add(source,i,inf);
			}
			if(flag_t)
			{
				add(i+n,sink,inf);
			}
			for(int j=1;j<=n;j++)
			{
				if(i==j) continue;
				else
				{
					bool flag=true;
					for(int k=0;k<p;k++)
					{
						if(((com[i].out[k]==0&&com[j].in[k]==1)||(com[i].out[k]==1&&com[j].in[k]==0)))
						{
							flag=false;
							break;
						}
					}
					if(flag)
					{
						add(i+n,j,min(com[i].flow,com[j].flow));
						vis[i+n][j]=true;
					}
				}
			}
		}
		int res=dinic();
		int path_cnt=0;
		for(int node=1;node<=n;node++)
		{
			for(int j=head[node+n];~j;j=edge[j].next)
			{
				int v=edge[j].to;
				if(vis[node+n][v]&&edge[j].capa<min(com[node].flow,com[v].flow))
				{
					ans[path_cnt].u=node;
					ans[path_cnt].v=v;
					ans[path_cnt++].flow=min(com[node].flow,com[v].flow)-edge[j].capa;
				}
			}
		}
		printf("%d %d\n",res,path_cnt);
		for(int i=0;i<path_cnt;i++)
		{
			printf("%d %d %d\n",ans[i].u,ans[i].v,ans[i].flow);
		}
	}
	return 0;
}

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值