POJ3436 ACM Computer Factory 【最大流】
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 5412 | Accepted: 1863 | 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
Source
题意:一个电脑由n个部件组成,如今有m台机器,每台机器能够将一个组装状态的电脑组合成还有一个状态。如(0, 1, 2)表示第一个部件未完毕。第二个部件完毕,第三个部件可完毕可不完毕。然后给出m个机器单位时间内能完毕的任务数以及详细的输入和输出状态。求整个系统单位时间内的电脑成品产量以及详细的机器间的传输关联。
题解:这题能够转换成最大流来做,一个机器的输出状态能够跟还有一个机器的输入状态关联,仅仅要它们的状态“equals”,然后再设置一个超级源点和汇点,再就能够用Dinic解题了。
#include <stdio.h>
#include <string.h>
#define maxn 55
#define inf 0x3fffffff struct Node {
int in[10], out[10]; // 拆点
int Q; // 容量
} M[maxn];
int G[maxn << 1][maxn << 1], que[maxn << 1], m, n, mp;
int G0[maxn << 1][maxn << 1], deep[maxn << 1], vis[maxn << 1]; bool equals(int a[], int b[]) {
for(int k = 0; k < n; ++k) {
if(a[k] != 2 && b[k] != 2 && a[k] != b[k])
return false;
}
return true;
} bool countLayer() {
int i, id = 0, now, front = 0;
memset(deep, 0, sizeof(deep));
deep[0] = 1; que[id++] = 0;
while(front < id) {
now = que[front++];
for(i = 0; i <= mp; ++i)
if(G[now][i] && !deep[i]) {
deep[i] = deep[now] + 1;
if(i == mp) return true;
que[id++] = i;
}
}
return false;
} int Dinic() {
int i, id = 0, maxFlow = 0, minCut, pos, u, v, now;
while(countLayer()) {
memset(vis, 0, sizeof(vis));
vis[0] = 1; que[id++] = 0;
while(id) {
now = que[id - 1];
if(now == mp) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = que[i - 1]; v = que[i];
if(G[u][v] < minCut) {
minCut = G[u][v]; pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = que[i - 1]; v = que[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(id && que[id - 1] != pos)
vis[que[--id]] = 0;
} else {
for(i = 0; i <= mp; ++i) {
if(G[now][i] && deep[now] + 1 == deep[i] && !vis[i]) {
que[id++] = i; vis[i] = 1; break;
}
}
if(i > mp) --id;
}
}
}
return maxFlow;
} int main() {
//freopen("stdin.txt", "r", stdin);
int i, j, sum, count;
while(scanf("%d%d", &n, &m) == 2) {
memset(G, 0, sizeof(G));
for(i = 1; i <= m; ++i) {
scanf("%d", &M[i].Q);
for(j = 0; j < n; ++j) scanf("%d", &M[i].in[j]);
for(j = 0; j < n; ++j) scanf("%d", &M[i].out[j]);
G[i][i + m] = M[i].Q;
}
// 连接出口跟入口
for(i = 1; i <= m; ++i) {
for(j = i + 1; j <= m; ++j) {
if(equals(M[i].out, M[j].in))
G[i + m][j] = inf;
if(equals(M[j].out, M[i].in))
G[j + m][i] = inf;
}
}
// 设置超级源点和超级汇点
for(i = 1; i <= m; ++i) { // 源点
G[0][i] = inf;
for(j = 0; j < n; ++j)
if(M[i].in[j] == 1) {
G[0][i] = 0; break;
}
}
mp = m << 1 | 1;
for(i = 1; i <= m; ++i) { // 汇点
G[i + m][mp] = inf;
for(j = 0; j < n; ++j)
if(M[i].out[j] != 1) {
G[i + m][mp] = 0; break;
}
}
// 备份原图
memcpy(G0, G, sizeof(G));
sum = Dinic();
count = 0;
// 推断哪些路径有流走过
for(i = m + 1; i < mp; ++i)
for(j = 1; j <= m; ++j)
if(G0[i][j] > G[i][j]) ++count;
printf("%d %d\n", sum, count);
// 输出机器间的关系
if(count)
for(i = m + 1; i < mp; ++i)
for(j = 1; j <= m; ++j)
if(G0[i][j] > G[i][j])
printf("%d %d %d\n", i - m, j, G0[i][j] - G[i][j]);
}
return 0;
}
2015.4.20
#include <stdio.h>
#include <string.h>
#include <vector> using std::vector; const int maxn = 102;
const int maxp = 10;
const int maxm = 2500;
const int inf = 0x3f3f3f3f;
const int sOut[maxp] = {};
int P, N;
struct Node2 {
int in[maxp], out[maxp];
int c;
} node[maxn]; int G[maxn][maxn], G0[maxn][maxn], queue[maxn];
bool vis[maxn]; int Layer[maxn]; bool countLayer(int s, int t) {
memset(Layer, 0, sizeof(Layer));
int id = 0, front = 0, now, i;
Layer[s] = 1; queue[id++] = s;
while(front < id) {
now = queue[front++];
for(i = s; i <= t; ++i)
if(G[now][i] && !Layer[i]) {
Layer[i] = Layer[now] + 1;
if(i == t) return true;
else queue[id++] = i;
}
}
return false;
}
// 源点,汇点,源点编号必须最小。汇点编号必须最大
int Dinic(int s, int t) {
int minCut, pos, maxFlow = 0;
int i, id = 0, u, v, now;
while(countLayer(s, t)) {
memset(vis, 0, sizeof(vis));
vis[s] = true; queue[id++] = s;
while(id) {
now = queue[id - 1];
if(now == t) {
minCut = inf;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
if(G[u][v] < minCut) {
minCut = G[u][v];
pos = u;
}
}
maxFlow += minCut;
for(i = 1; i < id; ++i) {
u = queue[i - 1];
v = queue[i];
G[u][v] -= minCut;
G[v][u] += minCut;
}
while(queue[id - 1] != pos)
vis[queue[--id]] = false;
} else {
for(i = 0; i <= t; ++i) {
if(G[now][i] && Layer[now] + 1 == Layer[i] && !vis[i]) {
vis[i] = 1; queue[id++] = i; break;
}
}
if(i > t) --id;
}
}
}
return maxFlow;
} void init()
{
memset(G, 0, sizeof(G));
} bool canBeLinked(const int out[], const int in[])
{
int i;
for (i = 0; i < P; ++i) {
if (out[i] == 0 && in[i] == 1) break;
if (out[i] == 1 && in[i] == 0) break;
} return i == P;
} void getMap()
{
int u, v, c, i, j, k, cnt;
for (i = 1; i <= N; ++i) {
scanf("%d", &c);
G[i][N+i] = c; for (j = 0; j < P; ++j)
scanf("%d", &node[i].in[j]);
for (j = cnt = 0; j < P; ++j) {
scanf("%d", &node[i].out[j]);
if (node[i].out[j] == 1) ++cnt;
} if (canBeLinked(sOut, node[i].in)) G[0][i] = inf;
if (cnt == P) G[i+N][2*N+1] = inf;
} for (i = 1; i <= N; ++i) {
// connection
for (j = 1; j <= N; ++j) {
if (i != j && canBeLinked(node[i].out, node[j].in)) {
G[i+N][j] = inf;
// printf("...%d..%d...\n", i + N, j);
}
}
} memcpy(G0, G, sizeof(G));
/*
for (i = 1; i <= N; ++i) {
printf("...%d: ", i);
for (j = 0; j < P; ++j)
printf("%d%c", node[i].in[j], j == P - 1 ? '\n' : ' ');
for (j = 0; j < P; ++j)
printf("%d%c", node[i].out[j], j == P - 1 ? '\n' : ' ');
} for (i = 0; i <= 2 * N + 1; ++i) {
printf("%d: ", i);
for (j = 0; j <= 2 * N + 1; ++j) {
printf("%d%c", G[i][j], j == 2*N+1 ? '\n' : ' ');
}
}*/
} int solve()
{
int ret = Dinic(0, 2 * N + 1);
vector<int> vec;
int cnt = 0, i, j, u, v, c; for (i = 1; i <= N; ++i) {
for (j = 1; j <= N; ++j) {
if (G[i+N][j] < G0[i+N][j]) {
vec.push_back(i);
vec.push_back(j);
vec.push_back(G0[i+N][j] - G[i+N][j]);
}
}
} printf("%d %d\n", ret, vec.size() / 3);
for (i = 0; i < vec.size(); ) {
u = vec[i++];
v = vec[i++];
c = vec[i++];
printf("%d %d %d\n", u, v, c);
}
} int main()
{
while (~scanf("%d%d", &P, &N)) {
init();
getMap();
solve();
}
return 0;
}
POJ3436 ACM Computer Factory 【最大流】的更多相关文章
- 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: ...
- POJ3436 ACM Computer Factory(最大流)
题目链接. 分析: 题意很难懂. 大体是这样的:给每个点的具体情况,1.容量 2.进入状态 3.出去状态.求最大流. 因为有很多点,所以如果一个点的出去状态满足另一个点的进入状态,则这两个点可以连一条 ...
- POJ-3436 ACM Computer Factory 最大流 为何拆点
题目链接:https://cn.vjudge.net/problem/POJ-3436 题意 懒得翻,找了个题意. 流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规 ...
- poj3436 ACM Computer Factory, 最大流,输出路径
POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器.每一个机器单位时间产量为Qi. 电脑由P个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...
- POJ3436 ACM Computer Factory(最大流/Dinic)题解
ACM Computer Factory Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8944 Accepted: 3 ...
- 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 ...
随机推荐
- 我的wifi
首先利用百度查找 怎么承载网络,托管网络的用户名和密码 . 1.以管理员身份运行命令提示符: 快捷键win+R→输入cmd→回车 2.启用并设定虚拟WiFi网卡: 运行命令:netsh wlan se ...
- 用Flask实现视频数据流传输
Flask 是一个 Python 实现的 Web 开发微框架.这篇文章是一个讲述如何用它实现传送视频数据流的详细教程. 我敢肯定,现在你已经知道我在O’Reilly Media上发布了有关Flask的 ...
- 一个Java程序的执行过程(转)
我们手工执行java程序是这样的: 1.在记事本中或者是UE的文本编辑器中,写好源程序: 2.使用javac命令把源程序编译成.class文件: 编译后的.class(类字节码)文件中会包含 ...
- ECharts一个强大的商业产品图表库
Architecture ECharts (Enterprise Charts 商业产品图表库) 提供商业产品常用图表库,底层基于ZRender,创建了坐标系,图例,提示,工具箱等基础组件,并在此上构 ...
- H面试程序(28):字符串处理转换
//2 字符串处理转换 //问题描述: //在给定字符串中找出单词( “单词”由大写字母和小写字母字符构成, //其他非字母字符视为单词的间隔,如空格.问号.数字等等:另外单个字母不算单词): //找 ...
- JavaScript 中的日期和时间
前言 本篇的介绍涵盖以下部分: 1. 时间标准指的是什么?UCT和GMT 的概念.关联和区别? 2. 时间表示标准有哪些? 3. JS 中时间的处理 日期时间标准 日期的标准就不多说了 -- 公元纪年 ...
- 【一】仿微信飞机大战cocos2d-x3.0rc1
參考 [偶尔e网事] 的 [cocos2d-x入门实战]微信飞机大战 cocos2dx 2.0版本号,偶尔e网事他写的很具体,面面俱到,大家很有必要看下.能够通过以下链接跳转: cocos2d-x入 ...
- Jsp分页实例---真分页
网页的分页功能的实现比较简单,实现方法也多种多样. 今天总结一个简单的Jsp真分页实例. 首先,提到分页就要先明确一个概念,何为真分页何谓假分页. 假分页:一次性从数据库读出表的所有数据一次性的返回给 ...
- Python字符串原理剖析------万恶的+号
字符串原理剖析pyc文件,执行python代码时,如果导入了其他的.py文件,那么执行过程中会自动生成一个与其同名的.pyc文件,该文件就是python解释器变异之后产生的字节码 PS:代码经过编译可 ...
- HEVC码率控制浅析——HM代码阅读之一
HM的码率控制提案主要参考如下三篇:K0103,M0036,M0257.本文及后续文章将基于HM12.0进行讨论,且首先仅讨论K0103对应的代码,之后再陆续补充M0036,M0257对应的代码分析, ...