POJ3436 ACM Computer Factory 拆点+网络最大流

这道题目的意思我没看懂。
看了解释后,大概明白这题的意思是有一个工厂,它里面有多个装配电脑的线路,每个线路有一个最大的电脑容量数,然后有2p个值,分别对应输入和输出。
0表示不能有,1表示必须有,2表示无所谓有和没有。最终要我们求出最多的组装数,和线路的连接数并输出方案。 只有输出全是1的才算组装好一台电脑。
 
这里我们要拆点,把一个线路拆成两个点i和i+n,并且在两点间连一条弧,容量为相应电脑最大容纳量,然后如果输入没有1则i和源点相连容量INF,然后如果输出全是1,则i+n和汇点相连容量为INF。
然后如果i的输出和j的输入能够对应满足要求,则将i+n与j相连,容量为INF。
最后求一个最大流即可。然后在对剩余网络进行搜索即可
ACM Computer Factory
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4073 Accepted: 1363 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 ofP 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 ofith machine is represented as by 2 P + 1 integers Qi Si,1Si,2...Si,P Di,1Di,2...Di,P, whereQi specifies performance, Si,j — input specification for partj, Di,k — output specification for partk.

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, thenM descriptions of the connections. Each connection between machines A and B must be described by three positive numbers A B W, whereW 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
#include<iostream>
#include<cstdio>

using namespace std;

#define MAXN 200 
#define INF 0xFFFFFF

struct edge
{
	int to,c,next;
}e[99999];

struct edge2
{
	int u,v,f;
}ee[99999];
int pi[15][MAXN],po[15][MAXN];
int que[1000], dis[MAXN],pre[MAXN];
int head[MAXN],head2[MAXN];
int st,ed,maxflow;
int en;
int n,p;

void add(int a,int b,int c)
{
	e[en].to=b;
	e[en].c=c;
	e[en].next=head[a];
	head[a]=en++;
	e[en].to=a;
	e[en].c=0;
	e[en].next=head[b];
	head[b]=en++;	
} 

bool bfs() 
{
	memset(dis,-1,sizeof(dis));
	que[0]=st,dis[st]=1;
	int t=1,f=0;
	while(f<t)
	{
		int j=que[f++];
		for(int k=head[j];k!=-1;k=e[k].next)
		{	
			int i=e[k].to;
			if(dis[i]==-1 && e[k].c)
			{
				que[t++]=i;
				dis[i]=dis[j]+1;
				if(i==ed) return true;
			}
		}
	}
	return false; 
} 

int update()  
{  
	int p,flow=INF;  
    for (int i=pre[ed];i!=-1;i=pre[i])
		if(e[head2[i]].c<flow) p=i,flow=e[head2[i]].c;    
    for (int i=pre[ed];i!=-1;i=pre[i]) 
		e[head2[i]].c-=flow,e[head2[i]^1].c+=flow;   
    maxflow+=flow; 
    return p;
}  

void dfs()
{  
	memset(pre,-1,sizeof(pre));
	memcpy(head2,head,sizeof(head2));  
    for(int i=st,j;i!=-1;)  
    {  
        int flag=false;  
        for(int k=head[i];k!=-1;k=e[k].next)    
          if(e[k].c && (dis[j=e[k].to]==dis[i]+1) )  
          {  
                pre[j]=i; 
				head2[i]=k;  
				i=j; 
				flag=true;  
                if(i==ed) 
					i=update();  
                if(flag) 
					break;  
          }  
        if (!flag) dis[i]=-1,i=pre[i];   
    }  
} 

void solve()
{
	bool flag=true;
	int temp;
	st=0,ed=n*2+1;
	en=0;
	maxflow=0;
	memset(head,-1,sizeof(head));
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&temp);
		add(i,i+n,temp);
		flag=true;
		for(int j=1;j<=p;j++)
		{
			scanf("%d",&pi[i][j]);
			if(pi[i][j]==1)
				flag=false;
		}
		if(flag)
			add(st,i,INF);
		flag=true;
		for(int j=1;j<=p;j++)
		{
			scanf("%d",&po[i][j]);
			if(!po[i][j])
				flag=false;
		}
		if(flag)
			add(i+n,ed,INF);
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			flag=true;
			if(i==j) continue;
			for(int k=1;k<=p;k++)
			{
				if(po[i][k]==1 && pi[j][k]==0)
					flag=false;
				if(po[i][k]==0 && pi[j][k]==1)
					flag=false;
				if(!flag) break;
			}
			if(flag)
				add(i+n,j,INF);	
		}
	}
	while(bfs())
		dfs();
	int counts=0;
	for(int i=1;i<=n;i++)
	{
		for(int j=head[i+n];j!=-1;j=e[j].next)
		{
			if(e[j].to==i || e[j].to==ed)	continue;
			
			if(e[j^1].c)
			{
				ee[counts].u=i;
				ee[counts].v=e[j].to;
				ee[counts++].f=e[j^1].c;
			}
		}
	}
	printf("%d %d\n",maxflow,counts);
	for(int i=0;i<counts;i++)
		printf("%d %d %d\n",ee[i].u,ee[i].v,ee[i].f);	
}

int main()
{
	while(scanf("%d%d",&p,&n)!=EOF) 
		solve();
	return 0;
} 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值