3436 ACM Computer Factory

                                            ACM Computer Factory
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 2955Accepted: 988Special 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 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

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
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
2 2
100  0 0  1 0
200  0 1  1 1

Sample Output

25 2
1 3 15
2 3 10
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1
0 0

 

题意:有n个流水线,现在要生产由p个零件组成的电脑,每条流水线要工作有需要零件,每条流水线有产出零件以及每小时流水线的负载量,对于每条流水线,输入格式为

si  a[i][0].....a[i][p]    b[i][0]......b[i][p]

si为第i条流水线的负载量,a[i]代表第i条流水线的需要零件,b[i]代表第i条流水线的产出零件。

现在要你重新安排流水线的调度,使得n条流水线能生产的电脑的台数做多

分析:(1)建图,网络流,对于第i条和第j条流水线,如果第i条流水线的输出能作为第j条流水线的输入那么则把第i条和第j条流水线连接,其容量为第i条流水线的负载量。如果第i条流水线不需要零件作为输入那么从源点连一条边到i,容量为inf,如果第i条流水线的产出零件包含p个零件,则从第i条流水线向终点连一条容量为si的边。这种做法其实是错的。看下面的这个例子:

点旁边的数字代表改流水线的负载量,上图中负载俩都为2,由上面的建图方式那么图中所有边得容量都为2,也就是从第3条流水线处理了4个任务,但是3的负载量为2。所以这种建图方式是错的。

(2)正解,拆点,将i拆成i和i',对于第i,j条流水线如果i的产出能作为j的输入,那么从j'像i连边,容量为inf,对于i和i' 连一条容量为si的边,源点和终点连边和上面说的一样,容量为inf,这样对于所有的流水线比满足负载限制。

#include<stdio.h>
#include<string.h>

#define E 6000
const int inf=0x3f3f3f3f;
int e,head[150];
int dep[150],que[150],cur[150];
struct node
{
	int x,y;
	int nxt;
	int c,total;
}edge[E];
void addedge(int u,int v,int c)
{
	edge[e].x=u;
	edge[e].y=v;
	edge[e].nxt=head[u];
	edge[e].c=c;
	edge[e].total=c;
	head[u]=e++;

	edge[e].x=v;
	edge[e].y=u;
	edge[e].nxt=head[v];
	edge[e].c=0;
	edge[e].total=0;
	head[v]=e++;
}

int maxflow(int s,int t)
{
	int i,j,k,front,rear,top,min,res=0;
	while(1)
	{
		memset(dep,-1,sizeof(dep));
		front=0;rear=0;		
		que[rear++]=s;
		dep[s]=0;
		while(front!=rear)
		{
			i=que[front++];
			for(j=head[i];j!=-1;j=edge[j].nxt)
				if(edge[j].c&&dep[edge[j].y]==-1)
				{
					dep[edge[j].y]=dep[i]+1;
					que[rear++]=edge[j].y;			
				}
		}
		if(dep[t]==-1)
			break;
		memcpy(cur,head,sizeof(head)); 
		for(i=s,top=0;;)
		{
			if(i==t)
			{
				min=inf;
				for(k=0;k<top;k++)
					if(min>edge[que[k]].c)
					{
						min=edge[que[k]].c;
						front=k;
					}
				for(k=0;k<top;k++)
				{
					edge[que[k]].c-=min;
					edge[que[k]^1].c+=min;
				}
				res+=min;
				i=edge[que[top=front]].x;
				
			}
			for(j=cur[i];cur[i]!=-1;j=cur[i]=edge[cur[i]].nxt)
				if(dep[edge[j].y]==dep[i]+1&&edge[j].c)
					break;
			if(cur[i]!=-1)
			{
				que[top++]=cur[i];
				i=edge[cur[i]].y;
			}
			else
			{
				if(top==0)
					break;
				dep[i]=-1;
				i=edge[que[--top]].x;
			}
		}
	}
	return res;
}
int a[60],b[60][30],g[150][150];
int main()
{
	int n,p,i,j,k;
	while(scanf("%d%d",&p,&n)!=EOF)
	{
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			for(j=0;j<2*p;j++)
				scanf("%d",&b[i][j]);
		}
		e=0;memset(head,-1,sizeof(head));
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
			{
				for(k=0;k<p;k++)
					if((b[i][k+p]==0&&b[j][k]==1)||(b[i][k+p]==1&&b[j][k]==0))
						break;
				if(k>=p)
					addedge(i+n,j,inf);
			}
		for(i=1;i<=n;i++)
		{
			for(k=0;k<p;k++)
				if(b[i][k]==1)
					break;
			if(k>=p)
				addedge(0,i,inf);
			for(k=0;k<p;k++)
				if(b[i][k+p]==0)
					break;
			if(k>=p)
				addedge(i+n,n+n+1,inf);
		}
		for(i=1;i<=n;i++)
			addedge(i,i+n,a[i]);
		int ans=maxflow(0,n+n+1);
		int num=0;
		memset(g,0,sizeof(g));
		for(i=0;i<e;i++)
			if(edge[i].c<edge[i].total)
				g[edge[i].x][edge[i].y]=edge[i].total-edge[i].c;		
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(g[i+n][j])
					num++;
		printf("%d %d\n",ans,num);
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				if(g[i+n][j])
					printf("%d %d %d\n",i,j,g[i+n][j]);
	}
	return 0;
}


 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值