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. fiddler抓包工具使用

    此工具用于抓取302等看不到的包. 设置: 步骤一 步骤二 重启fiddler软件,设置才有效. 设置谷歌浏览器,使浏览器的访问都经过fiddler.(fiddler就成了代理了)设置如下 隐藏图片的 ...

  2. mysql 数据备份与恢复

    1.mysql的备份 命令:"mysqldump -u root -p 数据库名 [表名] > 备份文件名" 不写表名默认备份所有整个数据库. 注意:备份的文件中没有创建数据 ...

  3. 模仿linux内核定时器代码,用python语言实现定时器

    大学无聊的时候看过linux内核的定时器,如今已经想不起来了,也不知道当时有没有看懂,如今想要模仿linux内核的定时器.用python写一个定时器,已经想不起来它的设计原理了.找了一篇blog,li ...

  4. 1分钟了解协同过滤,pm都懂了

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/z50L2O08e2u4afToR9A/article/details/79565720 projec ...

  5. android动态设置边框颜色

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  6. mysql 开启profiling

    mysql 开启profiling

  7. 【spring mvc】application context的生命周期

    上一次讲application context中bean的生命周期,后面贴了一部分代码,但根本没理解代码意思,有幸在博客园看到一篇关于这部分的代码解析,特别长,特此做了一些整理笔记,并附上链接:htt ...

  8. CSS表格(未完成)

    CSS 表格 使用 CSS 可以使 HTML 表格更美观. 表格边框 指定CSS表格边框,使用border属性. 下面的例子指定了一个表格的Th和TD元素的黑色边框:

  9. [js]js中原型的继承

    js继承01 思路: 单例/工厂/构造函数--演进到原型 搞清原型结构 原型继承 模拟系统原型继承 实现自己的继承 观察原型继承特点 演进到原型链这一步 //单例模式: 防止变量名冲突: // 思路: ...

  10. glob.glob()、os.path.split()函数、global和nonlocal关键字

    1. glob.glob() glob模块是Python最简单的模块之一, 内容非常少, 用它可以查找符合特定规则的文件路径名, 查找文件时只会用到三个匹配符: * :匹配0个或多个字符 ? : 匹配 ...