POJ3436 ACM Computer Factory(最大流/Dinic)题解
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 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
题意:
题意很难理解,想了半天才看懂。有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)题解的更多相关文章
- POJ3436 ACM Computer Factory —— 最大流
题目链接:https://vjudge.net/problem/POJ-3436 ACM Computer Factory Time Limit: 1000MS Memory Limit: 655 ...
- poj-3436.ACM Computer Factory(最大流 + 多源多汇 + 结点容量 + 路径打印 + 流量统计)
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10940 Accepted: ...
- POJ-3436 ACM Computer Factory 最大流 为何拆点
题目链接:https://cn.vjudge.net/problem/POJ-3436 题意 懒得翻,找了个题意. 流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规 ...
- POJ3436 ACM Computer Factory(最大流)
题目链接. 分析: 题意很难懂. 大体是这样的:给每个点的具体情况,1.容量 2.进入状态 3.出去状态.求最大流. 因为有很多点,所以如果一个点的出去状态满足另一个点的进入状态,则这两个点可以连一条 ...
- POJ3436 ACM Computer Factory 【最大流】
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5412 Accepted: 1 ...
- poj3436 ACM Computer Factory, 最大流,输出路径
POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器.每一个机器单位时间产量为Qi. 电脑由P个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...
- POJ-3436 ACM Computer Factory(网络流EK)
As you know, all the computers used for ACM contests must be identical, so the participants compete ...
- Poj 3436 ACM Computer Factory (最大流)
题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题 ...
- POJ-3436:ACM Computer Factory (Dinic最大流)
题目链接:http://poj.org/problem?id=3436 解题心得: 题目真的是超级复杂,但解出来就是一个网络流,建图稍显复杂.其实提炼出来就是一个工厂n个加工机器,每个机器有一个效率w ...
随机推荐
- ubuntu环境下快速搭建开发环境
接触ubuntu已经半年了,虽然游戏啊qq啊在linux下配置稍微麻烦一些,但是作为开发环境,ubuntu真的是好东西,无论是c啊还是php and etc 看到官网上文档开发环境建议wamp,如果是 ...
- android activity and fragment活动周期
1.状态 /* 每个活动一共有四种状态 *:1.运行状态,就是栈顶的那个 * 2.暂停状态:就是不处于栈顶,但是依然可见,比如对话框下面的界面 * 3.停止状态:不处于栈顶,并且不可见 * 4.销毁状 ...
- [MySQL5.6] 最近对group commit的小优化
[MySQL5.6] 最近对group commit的小优化 http://www.tuicool.com/articles/rEZr2q 最近花了一些时间在做MySQL Group Commit的优 ...
- mysql 开启profiling
mysql 开启profiling
- 008-spring cloud gateway-路由谓词RoutePredicate、RoutePredicateFactory
一.概述 Spring Cloud Gateway将路由作为Spring WebFlux HandlerMapping基础结构的一部分进行匹配. Spring Cloud Gateway包含许多内置的 ...
- latex 安装和使用
1:下载 texlivewindows 版 http://tug.org/texlive/acquire-netinstall.html 2:双击exe文件进行安装,安装时选择 将路径添加到环境变量 ...
- HOW TO: 在 Visual C# .NET 应用程序中提供文件拖放功能
本文假定您熟悉下列主题: Windows 窗体列表框控件 Windows 窗体事件处理 生成示例的步骤 列表框控件提供了您需要处理的两个拖放事件: DragEnter 和 DragDrop. 当您在控 ...
- FastDFS单节点安装 & FastDFS+Nginx整合
安装环境 FastDFS_v5.05.tar.gz(http://sourceforge.net/projects/fastdfs/files/) fastdfs-nginx-module ...
- Python tricks(6) -- python代码执行的效率
python作为一个动态语言, 本身学习曲线比较平滑, 效率相比起来会比c++和java低一些. 脚本语言都是运行时编译的, 这个对于效率的影响是非常大的. 我借用参考1的代码, 加了点代码impor ...
- Linux服务器---ssh登录
Ssh登录 Ssh是建立在应用层和传输层的安全协议,专门为远程登录回话和其他网络服务提供安全性.利用ssh可以有效的防止远程管理中的信息泄露问题,同时ssh传输的数据是经过压缩的,可以加快传输 ...