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 ...
随机推荐
- kubernetes网络原理
1.1. 基础原则 每个Pod都拥有一个独立的IP地址,而且假定所有Pod都在一个可以直接连通的.扁平的网络空间中,不管是否运行在同一Node上都可以通过Pod的IP来访问. k8s中Pod的IP是最 ...
- (1.2)mysql 索引概念
索引的存储分类:mysql目前提供了以下4种索引 [1]B-Tree索引:最常见的索引类型,大部分引擎都支持B树索引 [2]HASH索引:只有Memory引擎支持,使用场景简单 [3]R-Tree索引 ...
- SQLServer DBA 三十问
原贴:http://www.cnblogs.com/fygh/archive/2011/10/18/2216166.html 答案:https://blog.csdn.net/cjssimei527/ ...
- 混淆矩阵在Matlab中PRtools模式识别工具箱的应用
声明:本文用到的代码均来自于PRTools(http://www.prtools.org)模式识别工具箱,并以matlab软件进行实验. 混淆矩阵是模式识别中的常用工具,在PRTools工具箱中有直接 ...
- Andrew Ng-ML-第十八章-大规模机器学习
1.学习大数据集 图1.学习大数据集 当数据集量为m=1亿时,进行梯度下降将会花费较大时间. 可以使用小量数据集进行训练,然后得出学习曲线. 左图是高方差,右图是高偏差. 总之是要通过高效的学习算法来 ...
- [LeetCode] 197. Rising Temperature_Easy tag: SQL
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to ...
- testng入门教程16数据驱动(把数据写在xml)
testng入门教程16数据驱动(把数据写在xml) testng入门教程16数据驱动(把数据写在xml)把数据写在xml文件里面,在xml文件右键选择runas---testng执行 下面是case ...
- Canvas标签基础
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Dapper Extensions中修改Dialect
如果是MySql数据库,则修改为:DapperExtensions.DapperExtensions.SqlDialect = new MySqlDialect(); DapperExtensions ...
- SQL中SELECT INTO和INSERT INTO SELECT语句介绍
表复制是经常要用到的操作,下面就将为您介绍SQL中SELECT INTO和INSERT INTO SELECT语句,供您参考. Insert是T-sql中常用语句,Insert INTO table( ...