POJ 3436 ACM Computer Factory (网络流,最大流)
POJ 3436 ACM Computer Factory (网络流,最大流)
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
Http
POJ:https://vjudge.net/problem/POJ-3436
Source
网络流,最大流
题目大意
工厂里有若干机器人负责生产电脑,每个机器人负责加工一部分(这个加工可以是加上一个零件,也可以是减少一个零件),同时,每个机器人对进入其加工的电脑也有要求,对于每一个零件,1代表必须存在才能加工,0代表必须不存在才能加工,而2代表无所谓。每个机器人加工的效率是不一样的。现在给出所有机器人对进入的电脑的要求和出口的形式,以及加工效率,求最大的生产量。
解决思路
题目不是很好理解,需要多读几遍。
我们可以用网络流来解决这道题。
对于每一个机器人i,我们把它拆成两个点(笔者用i和i+n表示,其中n是所有机器人的数量),i表示入口,i+n表示出口并连一条边i->(i+n) ,流量就是机器人的工作效率。
我们再枚举每一对机器人i和j,若i的出口产品能被j接受,则在(i+n)->j之间连一条无穷大的边,当然也可以把边权赋为i与j中效率小的那个(因为再怎么也不会超过它)
然后我们再来考虑源点与汇点,笔者这里用0表示源点,n*2+1表示汇点。我们再枚举每一个机器人,如果某一个机器人的入口全部都是0,则从源点向它连一条无穷大的边;如果某一个机器人的出口全是1,则从它向汇点连一条无穷大的边。
这样,我们在这张图上跑一边最大流即可。
最后我们来考虑如何输出选择的连接方案。
我们可以在建完图后把原图备份一下,在跑完最大流后,将跑完后的图与原图对比,如果发现有流的减少,则说明走了这条边,统计一下即可。
虽然说笔者是使用EK算法实现最大流的,但更推荐时间效率更高的Dinic算法,具体请移步我的这篇文章
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxN=200;
const int maxP=30;
const int inf=2147483647;
class Edge//输出时要用
{
public:
int u,v,w;
};
int n,m,P;
int G[maxN][maxN];//图
int G2[maxN][maxN];//备份的图,用来对比输出
int W[maxN];//每个机器人的效率
int In[maxN][maxP];//机器人对入口的要求
int Out[maxN][maxP];//机器人的出口规格
int Path[maxN];//EK算法中存增广路径
int Flow[maxN];//流
Edge Outp[maxN*maxN];//用于输出
bool bfs();//bfs增广
int main()
{
memset(G,0,sizeof(G));
scanf("%d%d",&P,&n);
for (int i=1;i<=n;i++)//输入
{
scanf("%d",&W[i]);
for (int j=1;j<=P;j++)
scanf("%d",&In[i][j]);
for (int j=1;j<=P;j++)
scanf("%d",&Out[i][j]);
}
for (int i=1;i<=n;i++)//给每个点的入点到出点连一条边
G[i][i+n]=W[i];
for (int i=1;i<=n;i++)//给每对出口与入口相对应的点连一条边
for (int j=1;j<=n;j++)
{
bool ok=1;
for (int k=1;k<=P;k++)
if (((Out[i][k]==1)&&(In[j][k]==0))||((Out[i][k]==0)&&(In[j][k]==1)))
{
ok=0;
break;
}
if (ok)
G[i+n][j]=min(W[i],W[j]);
}
for (int i=1;i<=n;i++)//判断能否和汇点或源点连接
{
bool ok=1;
for (int j=1;j<=P;j++)
if (In[i][j]==1)
{
ok=0;
break;
}
if (ok)
G[0][i]=inf;
ok=1;
for (int j=1;j<=P;j++)
if (Out[i][j]==0)
{
ok=0;
break;
}
if (ok)
G[i+n][n*2+1]=inf;
}
memcpy(G2,G,sizeof(G2));//把G存起来,备份一边,方便后面输出答案
int Ans=0;
while (bfs())//笔者这里用EK算法实现最大流
{
int di=Flow[n*2+1];
int now=2*n+1;
int last=Path[now];
while (now!=0)//更新残量网络
{
G[last][now]-=di;
G[now][last]+=di;
now=last;
last=Path[now];
}
Ans+=di;
}
printf("%d ",Ans);//输出答案,统计路径
int top=0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (G2[i+n][j]>G[i+n][j])
{
top++;
Outp[top].u=i;
Outp[top].v=j;
Outp[top].w=G2[i+n][j]-G[i+n][j];
}
printf("%d\n",top);//输出路径
for (int i=1;i<=top;i++)
printf("%d %d %d\n",Outp[i].u,Outp[i].v,Outp[i].w);
return 0;
}
bool bfs()
{
memset(Path,-1,sizeof(Path));
memset(Flow,0,sizeof(Flow));
queue<int> Q;
while (!Q.empty())
Q.pop();
Q.push(0);
Flow[0]=inf;
do
{
int u=Q.front();
Q.pop();
for (int i=0;i<=2*n+1;i++)
{
if ((Path[i]==-1)&&(G[u][i]>0))
{
Path[i]=u;
Q.push(i);
Flow[i]=min(Flow[u],G[u][i]);
}
}
}
while (!Q.empty());
if (Flow[2*n+1]>0)
return 1;
return 0;
}
POJ 3436 ACM Computer Factory (网络流,最大流)的更多相关文章
- POJ - 3436 ACM Computer Factory 网络流
POJ-3436:http://poj.org/problem?id=3436 题意 组配计算机,每个机器的能力为x,只能处理一定条件的计算机,能输出特定的计算机配置.进去的要求有1,进来的计算机这个 ...
- POJ - 3436 ACM Computer Factory(最大流)
https://vjudge.net/problem/POJ-3436 题目描述: 正如你所知道的,ACM 竞赛中所有竞赛队伍使用的计算机必须是相同的,以保证参赛者在公平的环境下竞争.这就是所有这些 ...
- POJ 3436 ACM Computer Factory(最大流+路径输出)
http://poj.org/problem?id=3436 题意: 每台计算机包含P个部件,当所有这些部件都准备齐全后,计算机就组装完成了.计算机的生产过程通过N台不同的机器来完成,每台机器用它的性 ...
- Poj 3436 ACM Computer Factory (最大流)
题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题 ...
- POJ 3436 ACM Computer Factory 最大流,拆点 难度:1
题目 http://poj.org/problem?id=3436 题意 有一条生产线,生产的产品共有p个(p<=10)零件,生产线上共有n台(n<=50)机器,每台机器可以每小时加工Qi ...
- poj 3436 ACM Computer Factory 最大流+记录路径
题目 题意: 每一个机器有一个物品最大工作数量,还有一个对什么物品进行加工,加工后的物品是什么样.给你无限多个初始都是000....的机器,你需要找出来经过这些机器操作后最多有多少成功的机器(111. ...
- kuangbin专题专题十一 网络流 POJ 3436 ACM Computer Factory
题目链接:https://vjudge.net/problem/POJ-3436 Sample input 1 3 4 15 0 0 0 0 1 0 10 0 0 0 0 1 1 30 0 1 2 1 ...
- POJ 3436 ACM Computer Factory (拆点+输出解)
[题意]每台计算机由P个零件组成,工厂里有n台机器,每台机器针对P个零件有不同的输入输出规格,现在给出每台机器每小时的产量,问如何建立流水线(连接各机器)使得每小时生产的计算机最多. 网络流的建图真的 ...
- POJ 3436 ACM Computer Factory
题意: 为了追求ACM比赛的公平性,所有用作ACM比赛的电脑性能是一样的,而ACM董事会专门有一条生产线来生产这样的电脑,随着比赛规模的越来越大,生产线的生产能力不能满足需要,所以说ACM董事会想 ...
随机推荐
- C# winform 设置WebBowser 内核版本
一种是在网页头部 用 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 使用当前浏览器最新 ...
- 20155317《网络对抗》Exp4 恶意代码分析
20155317<网络对抗>Exp4 恶意代码分析 基础问题回答 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用 ...
- 预定义的类型“System.Object”未定义或未导入
打开一个以前的程序 ,发现报这个错误.检查了程序,发现程序的引用 System 不见了 ,尝试 引用失败.. 查了有人说重新建立 Sln文件有用.. 一头雾水,随后 尝试操作 ,程序有用了 具体步骤: ...
- python 字符串的split()函数详解
split翻译为分裂. split()就是将一个字符串分裂成多个字符串组成的列表. split()当不带参数时以空格进行分割,当代参数时,以该参数进行分割. //---当不带参数时 example: ...
- LoRa---她的简介和她的专业术语
LoRa是LPWAN(低功耗广域网)通信技术的一种,其作用距离超过 15 公里,连接节点可达 100 万个.低功耗与长距离极限的组合可将最大数据速率提升至每秒 50千比特(Kbps). LoRa 是 ...
- Eclipse中JBoss插件配置
JBoss 服务器集成到Eclispe(考虑Eclipse版本Version: Indigo Service Release 2) http://www.cnblogs.com/sunddenly/p ...
- Jlink使用技巧系列教程索引
近两个月,写写停停,总算完成了这个系列的教程,不容易啊! Jlink使用技巧系列文章: 以下是各文章的链接,直接点击即可进入阅读. Jlink使用技巧之合并烧写文件 Jlink使用技巧之烧写SPI F ...
- [CF963E]Circles of Waiting[高斯消元网格图优化+期望]
题意 你初始位于 \((0,0)\) ,每次向上下左右四个方向走一步有确定的概率,问你什么时候可以走到 以 \((0,0)\)为圆心,\(R\) 为半径的圆外. \(R\le 50\) 分析 暴力 \ ...
- Azure 基础:Queue Storage
Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在前文中介绍了 File Storage 的基本用 ...
- VS2010带不出System.Data.OracleClient这个引用的解决方案
在使用VS2010的时候有时会带不出System.Data.OracleClient这个引用,可以使用以下解决方法: 右击项目的属性,在弹出窗口中有一个“目标框架”下拉框选项,默认会是.NET FRA ...