ACM Computer Factory 网络流

题目链接 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=90592#problem/B

 题目大意 

ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3107   Accepted: 1031   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,1Di,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.

Source

Northeastern Europe 2005, Far-Eastern Subregion


 

 

题意:

输入PNP表示零件总类个数,N表示机器台数,然后输入N行,每行的第一个元素为该机器的最大加工零件个数,接下去的P个元素为每台机器对应的P个零件的输入信息,0 表示 该台机器不能用于加工 Pj(1<= j <= P) 这个零件,1表示如果要在该台机器上加工就必须需要Pj这个零件,2表示Pj这个零件的有无对该台机器的加工不产生任何影响,然后又是P个元素,对应P个零部件在该台机器上的输出,如果Pj 对应的元素为0,那么表示该台机器不能产生Pj这个零部件,反之当Pj对应的元素为1,则表示该台机器可以产生零部件Pj



第一次做网络流题目 总是错  贴的也是别人代码 







#include #include using namespace std; const int inf=10001; int s; //超级源 int t; //超级汇 int n; //总结点数(包括超级源、超级汇) int p; //每台机器的部分数 int cap[52][52];// 边容量 int min(int a,int b) { return a<b?a:b; } /*利用BFS找增广链求网络最大流*/ int maxflow(void) { int queue[52]; int head,tail; int pre[52]; //结点i的前驱 int minflow; int flow = 0; int x,y; while(true) { memset(pre, -1, sizeof(pre)); for(queue[head=tail=0]=s;head<=tail;head++) { x=queue[head]; for(int i=0;(i0 && pre[i]==-1) //当结点u指向i的边存在,且i还没有标记前驱时 { pre[i]=x;//记录结点i的前驱为u queue[++tail]=i; } } if(pre[t]==-1) break;//BFS后汇点没有被标记,则跳出while,已经不存在增广链 minflow=inf;//初始化 for(x=pre[y=t];y!=s;)//回溯 { if(cap[x][y] < minflow) minflow=cap[x][y];//寻找当前增广链中最小容量的边,记录其边权(容量) y=x; x=pre[y]; } for(x=pre[y=t];y!=s;) //当前增广链 流量调整 { cap[x][y] -= minflow; //正向弧容量减少 cap[y][x] += minflow; //反向弧容量增加 y=x; x=pre[y]; } flow += minflow; //最大流=每次寻得的增广链的调整量之和 } return flow;//返回最大流 } int main(int i,int j,int k) { int in[52][21]; int out[52][3]; int backup[52][52];//备份图 int N; //除超级源、超级汇以外的总结点数 int line; //生产线数(容量发生变化的边数) int flow; //最大流 while (cin>>p>>N) { /*Initial*/ memset(cap,0,sizeof(cap)); //所有正向弧和反向弧的容量都初始化为0 s=0;//超级源 t=N+1; //超级汇 n=N+2; //总结点数+2 line=0; //记录变化的边的数量(生产线数量) /*Input*/ for(i=1;i<=N;i++) for(j=0;j<2 p="" 1="" j="" cin="">>in[i][j]; //用一个数列存储第i个结点的信息 in[i][0] 为结点i的容量 bool flag_s, flag_t; for(i=1;i<=N;i++) { flag_s=flag_t=true; for(k=0;k

i,边容量为i的容量 if(flag_t) cap[i][t]=in[i][0]; //当输出列全为1时,i->t,边容量为i的容量 bool flag=true; for(j=1;j<=N;j++) if(i!=j) { flag=true; for(k=0;(k

j,边容量为i的容量和j的容量的最小值 } } /*利用BFS找增广链求网络最大流*/ memcpy(backup, cap, sizeof(cap)); //把寻找增广链前的图的容量信息复制 flow=maxflow(); //返回最大流 /*Output*/ for(i=1;i<=N;i++) //注意范围,排除了含超级源和超级汇的边 for(j=1;j<=N;j++) if (cap[i][j] < backup[i][j])//比较调整前后的边权,若容量减少了,则输出这条边的信息 { out[line][0]=i; //i,j为生产线的两端点 out[line][1]=j; out[line][2]=backup[i][j] - cap[i][j];//变化的流量值(该生产线的最大生产量) line++; } cout<<flow<<' '<<line<<endl; for(i=0;i<line;i++) cout<<out[i][0]<<' '<<out[i][1]<<' '<<out[i][2]<<endl; } return 0; }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值