POJ3436 ACM Computer Factory —— 最大流
题目链接:https://vjudge.net/problem/POJ-3436
| Time Limit: 1000MS | Memory Limit: 65536K | |||
| Total Submissions: 8544 | Accepted: 3102 | 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
题意:
在一个电脑厂中,一台电脑被分成P个部件,有N台机器,且每台都有其特定的输入部件和输出部件。其中对于输入部件:0代表不能有,1代表必须有,2代表可以可无。对于输出部件:0代表没有,1代表有。因此每台机器都可能存在合作关系:即如果机器A的输出满足机械B的输入,就可以把机器A的成品放到机械B中继续加工。问:怎样安排流水线,才能使得单位时间内制造的电脑最多?
题解:
不拆点做法:
1.建立超级源点,且超级源点与每个输入都为0的机器相连,边的容量为这台机器的容量,表明最多只能提供机器所能容纳的量。
2.如果机器A的输出满足机械B的输入,则把机械A与机械B相连,且边的容量为机械A和机械B的容量的最小值,表明机械A最多只能为机械B提供自己所拥有的全部并且机械B也能接受的。
3.建立超级汇点,且每个输出都为1的机器与超级汇点相连,边的容量为这台机器的容量,表明这台机器最多只能产出自己容量大小的电脑。
4.求最大流即可。
拆点的做法:
1.只是对“不拆点做法”稍加修改:原本连向超级源点或者汇点的边的容量改为无限大,然后对每台机器拆成两个点,内部连一条边,边的容量为这台机器的容量。
2.对机器进行拆点只不过是为了:使得流经此台机器的流量限制在机器容量的范围内。那为什么“不拆点做法”又可以不拆点呢?因为在与超级源点、超级汇点相连的时候,已经把流经每台机器的流量限制住了。
不拆点:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e2+; int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN]; int sap(int start, int end, int nodenum)
{
memset(cur, , sizeof(cur));
memset(dis, , sizeof(dis));
memset(gap, , sizeof(gap));
memset(flow, , sizeof(flow));
int u = pre[start] = start, maxflow = , aug = INF;
gap[] = nodenum; while(dis[start]<nodenum)
{
loop:
for(int v = cur[u]; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && dis[u] == dis[v]+)
{
aug = min(aug, maze[u][v]-flow[u][v]);
pre[v] = u;
u = cur[u] = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u, u = pre[u])
{
flow[u][v] += aug;
flow[v][u] -= aug;
}
aug = INF;
}
goto loop;
} int mindis = nodenum-;
for(int v = ; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && mindis>dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]])==) break;
gap[dis[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} int cap[MAXN], in[MAXN][], out[MAXN][];
int cnt, ans[MAXN*MAXN][];
int main()
{
int n, p;
while(scanf("%d%d",&p,&n)!=EOF)
{
for(int i = ; i<=n; i++)
{
scanf("%d", &cap[i]);
for(int j = ; j<=p; j++) scanf("%d", &in[i][j]);
for(int j = ; j<=p; j++) scanf("%d", &out[i][j]);
} int start = , end = n+;
memset(maze, , sizeof(maze));
for(int i = ; i<=n; i++)
{
for(int j = ; j<=n; j++)
{
if(i==j) continue;
bool flag = true;
for(int k = ; k<=p; k++)
if(out[i][k]+in[j][k]==)
flag = false; if(flag) maze[i][j] = min(cap[i], cap[j]);
} bool flag1 = true, flag2 = true;
for(int k = ; k<=p; k++)
{
if(in[i][k]==) flag1 = false;
if(out[i][k]==) flag2 = false;
}
if(flag1) maze[start][i] = cap[i];
if(flag2) maze[i][end] = cap[i];
} int maxflow = sap(start, end, n+);
cnt = ;
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
{
if(i==j) continue;
if(flow[i][j]>)
{
ans[++cnt][] = i;
ans[cnt][] = j;
ans[cnt][] = flow[i][j];
}
} printf("%d %d\n", maxflow, cnt);
for(int i = ; i<=cnt; i++)
printf("%d %d %d\n", ans[i][], ans[i][], ans[i][]);
}
}
拆点:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXN = 1e2+; int maze[MAXN][MAXN];
int gap[MAXN], dis[MAXN], pre[MAXN], cur[MAXN];
int flow[MAXN][MAXN]; int sap(int start, int end, int nodenum)
{
memset(cur, , sizeof(cur));
memset(dis, , sizeof(dis));
memset(gap, , sizeof(gap));
memset(flow, , sizeof(flow));
int u = pre[start] = start, maxflow = , aug = INF;
gap[] = nodenum; while(dis[start]<nodenum)
{
loop:
for(int v = cur[u]; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && dis[u] == dis[v]+)
{
aug = min(aug, maze[u][v]-flow[u][v]);
pre[v] = u;
u = cur[u] = v;
if(v==end)
{
maxflow += aug;
for(u = pre[u]; v!=start; v = u, u = pre[u])
{
flow[u][v] += aug;
flow[v][u] -= aug;
}
aug = INF;
}
goto loop;
} int mindis = nodenum-;
for(int v = ; v<nodenum; v++)
if(maze[u][v]-flow[u][v]> && mindis>dis[v])
{
cur[u] = v;
mindis = dis[v];
}
if((--gap[dis[u]])==) break;
gap[dis[u]=mindis+]++;
u = pre[u];
}
return maxflow;
} int cap[MAXN], in[MAXN][], out[MAXN][];
int cnt, ans[MAXN*MAXN][];
int main()
{
int n, p;
while(scanf("%d%d",&p,&n)!=EOF)
{
for(int i = ; i<=n; i++)
{
scanf("%d", &cap[i]);
for(int j = ; j<=p; j++) scanf("%d", &in[i][j]);
for(int j = ; j<=p; j++) scanf("%d", &out[i][j]);
} int start = , end = *n+;
memset(maze, , sizeof(maze));
for(int i = ; i<=n; i++)
{
maze[i][n+i] = cap[i];
for(int j = ; j<=n; j++)
{
if(i==j) continue;
bool flag = true;
for(int k = ; k<=p; k++)
if(out[i][k]+in[j][k]==)
flag = false; if(flag) maze[n+i][j] = INF;
} bool flag1 = true, flag2 = true;
for(int k = ; k<=p; k++)
{
if(in[i][k]==) flag1 = false;
if(out[i][k]==) flag2 = false;
}
if(flag1) maze[start][i] = INF;
if(flag2) maze[n+i][end] = INF;
} int maxflow = sap(start, end, *n+);
cnt = ;
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
{
if(i==j) continue;
if(flow[n+i][j])
{
ans[++cnt][] = i;
ans[cnt][] = j;
ans[cnt][] = flow[n+i][j];
}
} printf("%d %d\n", maxflow, cnt);
for(int i = ; i<=cnt; i++)
printf("%d %d %d\n", ans[i][], ans[i][], ans[i][]);
}
}
POJ3436 ACM Computer Factory —— 最大流的更多相关文章
- 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 【最大流】
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个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...
- 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 ...
随机推荐
- 解决 IDEA 中src下xml等资源文件无法读取的问题
该问题的实质是,idea对classpath的规定. 在eclipse中,把资源文件放在src文件夹下,是可以找到的: 但是在idea中,直接把资源文件放在src文件夹下,如果不进行设置,是不能被找到 ...
- 品酒大会 BZOJ 4199
品酒大会 [问题描述] [输入格式] [输出格式] [样例输入] 10ponoiiipoi 2 1 4 7 4 8 3 6 4 7 [样例输出] 45 56 10 56 3 32 0 0 0 0 0 ...
- echarts3样例
<script type="text/javascript" src="echarts.min.js"></script> <di ...
- 取得mib oidname oid 对应关系表
snmptranslate -Tz -m ALL > d:\2.txt 取得所有名称与OID的对应表,很有用
- LibieOJ 6165 一道水题 (线性筛)
题目链接 LOJ6165 题目意思其实就是求LCM(1, 2, 3, ..., n) 直接用线性筛求出1到1e8之间的所有质数 然后对于每个质数p,他对答案的贡献为$p^{i}$ 其中$p^{i}$小 ...
- DFS与BFS对比
前面已经说过了深搜和广搜了,是不是有点还不是很好的分清他们?(其实分不分的请都没大有关系) 下面我们来看一看广搜与深搜的区别吧. 算法步骤上的区别 深度优先遍历图算法步骤: 1.访问顶点v 2,.依次 ...
- MD5加密算法Java代码
原文:http://www.open-open.com/code/view/1428398234916 import java.security.MessageDigest; import java. ...
- 【面试 JDK】【第一篇】Object类面试详解
1.Object类有哪些方法 1>clone()方法 保护方法,实现对象的浅复制,只有实现了Cloneable接口才可以调用该方法,否则抛出CloneNotSupportedException异 ...
- 文件重定向,getline()获取一样,屏幕输出流,格式控制符dec,oct,hex,精度控制setprecision(int num),设置填充,cout.width和file(字符),进制输入
1.在window下的命令重定向输出到文件里 2.将内容输入到某个文件里的方式:命令<1.txt (使用1.txt中的命令) 3.读取文件里的名,然后将命令读取最后输出到文件里.命令< ...
- Qt:解析命令行
Qt从5.2版開始提供了两个类QCommandLineOption和QCommandLineParser来解析应用的命令行參数. 一.命令行写法 命令行:"-abc" 在QComm ...