ACM Computer Factory
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8944   Accepted: 3267   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.

题意:

题意很难理解,想了半天才看懂。有n台机器,每台机器有三个参数:输入规格、输出规格、产量。输入规格有三个参数:0(不需要零件)、1(必须要零件)、2(随便)。输出规格有两个参数:0(不产出零件)、1(产出零件)。所以,对于输入规格为“012”的机器,需要输出规格为“010”或者“011”的机器与之相连。若一台机器输入规格是“000”说明他是最开始那台机器(因为不用放入零件),相同,一台机器输出规格是“111”说明他是最后那台机器(因为所有零件他都生产,组装成一台电脑)。求生产最大的产量、能生产电脑的产品线数量...

思考:

用网络流最大流,大白有模板。代码里有比较详细的解释。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=510;
const int INF=1e9;
using namespace std; struct Edge{
int from,to,cap,flow; //cap容量,flow流量
}; struct Dinic{
int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
vector<Edge> edge; //边表
vector<int> G[N]; //邻接表,G[i][j]表示结点i的第j条边在edge中的序号
bool vis[N]; //BFS使用
int d[N]; //从s到i的举例(层数)
int cur[N]; //弧下标 void init(int n){
this->n=n;
edge.clear();
for(int i=0;i<=n;i++) G[i].clear();
} void add(int from,int to,int cap){ //from,to,cap,flow
edge.push_back((Edge){from,to,cap,0}); //压入边
edge.push_back((Edge){to,from,0,0}); //反向弧
m=edge.size();
G[from].push_back(m-2); //边序号
G[to].push_back(m-1); //反向弧序号
} bool bfs(){ //分层
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++){ //同一结点边遍历
Edge& e=edge[G[x][i]]; //e为当前边
if(!vis[e.to] && e.cap>e.flow){ //如果当前边未走过 && 流量还能增加
vis[e.to]=1;
d[e.to]=d[x]+1; //分层
q.push(e.to);
}
}
}
return vis[t]; //没访问t回复false
} int dfs(int x,int a){ //a为当前所有弧最小残量
if(x==t || a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &e=edge[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)) )>0 ){ //有下一层 && 还能增广
e.flow+=f; //流量增加
edge[G[x][i]^1].flow-=f; //反向弧减少
flow+=f; //flow将每一条通路的最小残量相加
a-=f;
if(a==0) break;
}
}
return flow;
} int maxflow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
}; bool judge(int out[],int in[],int p){ //0-0:2 1-1:2
for(int i=0;i<p;i++){
if(out[i]!=in[i] && in[i]!=2) return false;
}
return true;
} int main(){
int s,t,p,n,w[55],in[55][15],out[55][15];
while(~scanf("%d%d",&p,&n)){
Dinic D;
D.init(t);
s=0;t=2*n+1;
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
bool flag=true;
for(int j=0;j<p;j++){
scanf("%d",&in[i][j]);
if(in[i][j]==1) flag=false;
}
if(flag) D.add(s,i,INF); //与源点相连
flag=true;
for(int j=0;j<p;j++){
scanf("%d",&out[i][j]);
if(out[i][j]==0) flag=false;
}
if(flag) D.add(i+n,t,INF); //与汇点相连
}
for(int i=1;i<=n;i++){
D.add(i,i+n,w[i]); //内部相连
for(int j=1;j<=n;j++){
if(i==j) continue;
if(judge(out[i],in[j],p)) D.add(i+n,j,INF);
}
} int flow=D.maxflow(s,t); //得到最大流 int cnt=0;
for(int i=0;i<D.edge.size();i++){
if(D.edge[i].from==s||D.edge[i].to==s||D.edge[i].from==t||D.edge[i].to==t)
continue;
if((D.edge[i].from+n)==D.edge[i].to||(D.edge[i].from-n)==D.edge[i].to)
continue;
if(D.edge[i].flow<0) //找到反向弧
cnt++;
}
printf("%d %d\n",flow,cnt);
for(int i=0;i<D.edge.size();i++){
if(D.edge[i].from==s||D.edge[i].to==s||D.edge[i].from==t||D.edge[i].to==t)
continue;
if((D.edge[i].from+n)==D.edge[i].to||(D.edge[i].from-n)==D.edge[i].to)
continue;
if(D.edge[i].flow<0){ //找到反向弧
cout<<D.edge[i].to-n<<" "<<D.edge[i].from<<" "<<D.edge[i].flow*(-1)<<endl;
}
}
}
return 0;
}

模板:

struct Edge{
int from,to,cap,flow; //cap容量,flow流量
}; struct Dinic{
int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点
vector<Edge> edge; //边表
vector<int> G[N]; //邻接表,G[i][j]表示结点i的第j条边在edge中的序号
bool vis[N]; //BFS使用
int d[N]; //从s到i的举例(层数)
int cur[N]; //弧下标 void init(int n){
this->n=n;
edge.clear();
for(int i=0;i<=n;i++) G[i].clear();
} void add(int from,int to,int cap){ //from,to,cap,flow
edge.push_back((Edge){from,to,cap,0}); //压入边
edge.push_back((Edge){to,from,0,0}); //反向弧
m=edge.size();
G[from].push_back(m-2); //边序号
G[to].push_back(m-1); //反向弧序号
} bool bfs(){ //分层
memset(vis,0,sizeof(vis));
memset(d,-1,sizeof(d));
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty()){
int x=q.front();
q.pop();
for(int i=0;i<G[x].size();i++){ //同一结点边遍历
Edge& e=edge[G[x][i]]; //e为当前边
if(!vis[e.to] && e.cap>e.flow){ //如果当前边未走过 && 流量还能增加
vis[e.to]=1;
d[e.to]=d[x]+1; //分层
q.push(e.to);
}
}
}
return vis[t]; //没访问t回复false
} int dfs(int x,int a){ //a为当前所有弧最小残量
if(x==t || a==0) return a;
int flow=0,f;
for(int &i=cur[x];i<G[x].size();i++){
Edge &e=edge[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)) )>0 ){ //有下一层 && 还能增广
e.flow+=f; //流量增加
edge[G[x][i]^1].flow-=f; //反向弧减少
flow+=f; //flow将每一条通路的最小残量相加
a-=f;
if(a==0) break;
}
}
return flow;
} int maxflow(int s,int t){
this->s=s;
this->t=t;
int flow=0;
while(bfs()){
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
};

POJ3436 ACM Computer Factory(最大流/Dinic)题解的更多相关文章

  1. POJ3436 ACM Computer Factory —— 最大流

    题目链接:https://vjudge.net/problem/POJ-3436 ACM Computer Factory Time Limit: 1000MS   Memory Limit: 655 ...

  2. poj-3436.ACM Computer Factory(最大流 + 多源多汇 + 结点容量 + 路径打印 + 流量统计)

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10940   Accepted:  ...

  3. POJ-3436 ACM Computer Factory 最大流 为何拆点

    题目链接:https://cn.vjudge.net/problem/POJ-3436 题意 懒得翻,找了个题意. 流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规 ...

  4. POJ3436 ACM Computer Factory(最大流)

    题目链接. 分析: 题意很难懂. 大体是这样的:给每个点的具体情况,1.容量 2.进入状态 3.出去状态.求最大流. 因为有很多点,所以如果一个点的出去状态满足另一个点的进入状态,则这两个点可以连一条 ...

  5. POJ3436 ACM Computer Factory 【最大流】

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5412   Accepted: 1 ...

  6. poj3436 ACM Computer Factory, 最大流,输出路径

    POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器.每一个机器单位时间产量为Qi. 电脑由P个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...

  7. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

  8. Poj 3436 ACM Computer Factory (最大流)

    题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题 ...

  9. POJ-3436:ACM Computer Factory (Dinic最大流)

    题目链接:http://poj.org/problem?id=3436 解题心得: 题目真的是超级复杂,但解出来就是一个网络流,建图稍显复杂.其实提炼出来就是一个工厂n个加工机器,每个机器有一个效率w ...

随机推荐

  1. Spark-自定义排序

    一.自定义排序规则-封装类 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /** ...

  2. Area---poj1265(皮克定理+多边形求面积)

    题目链接:http://poj.org/problem?id=1265 题意是:有一个机器人在矩形网格中行走,起始点是(0,0),每次移动(dx,dy)的偏移量,已知,机器人走的图形是一个多边形,求这 ...

  3. vue-页面回退

    <template> <div> <button @click="goback">我是Home01</button> </di ...

  4. Mantle 与Injection

    本来Injection可以本地打补丁实时修改代码,但是不知道Mantle的数据类为何不能打补丁,可能因为Mantle利用了很多运行时的技术吧.

  5. web前端面试小结(1)

    两天大概面试了4家,有电面也有F2F,现将面试中的问题大概汇总下,一方面了解自己的不足,一方面用来勉励自己后面面试加油! 答案网上都有,就不一一写在这里了,后面有时间会把下面的问题分别拉出来详述. 1 ...

  6. PhoneGap+Cordova+SenchaTouch-01-环境搭建

    转http://my.oschina.net/zhongwenhao/blog/369465 环境搭建基于 windows  ,mac系统可以借鉴 1.安装NodeJS 和ruby http://no ...

  7. 025-du命令查看文件大小

    1.查看某个目录下面所有文件占用空间大小并排序.du -sh  目录 2.排序.sort -h

  8. SoapUI 使用变量

    登录问题不好解决, 只能临时用cookie来执行 1.变量定义 2.引用变量 3.调用Header

  9. python多进程打印字符,加锁(Lock加锁)

    先看不加锁的: #coding=utf-8from multiprocessing import Process,Lockimport time def l(num):    #lock.acquir ...

  10. Linux中Postfix邮件认证配置(五)

    Postfix+Dovecot+Sasl工作原理 1.A用户使用MUA客户端借助smtp协议登陆smtpd服务器,需要先进行用户和密码认证,而SMTPD服务器端支持sasl认证,例如有一个sasl客户 ...