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 ...
随机推荐
- C++ 中的 C_str() 函数用法
转中转 ~\(≧▽≦)/~ :http://blog.csdn.net/nancy_m/article/details/7583550 语法: const char *c_str(); c_str() ...
- Error处理: “非法字符: \65279”的解决办法
将eclipse项目转为maven项目的时候,编译时遇到 “非法字符: \65279”的报错. 出错内容是: *.java:1: 非法字符: \65279 [javac] package com ...
- Fabrice Bellard其人 ---- FFMPEG及其他……
有些计算机科学家的名字耳熟能详:阿兰·图灵(Alan Turing).高纳德(Donald Knuth).艾兹赫尔·戴克斯特拉(Edsger Dijkstra),这些人的名气甚至大于他们突破性的成就. ...
- C++ assert 的一点说明
断言(ASSERT)的用法 转载自http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数 ...
- linux的内存性能评估
linux的内存性能评估 参考自:自学it网,http://www.zixue.it/. (1)使用free指令监控内存,参数-m以M为单位显示,-h人性化显示单位. [test@localhost ...
- Spring 3.0 注解
原文 :http://www.blogjava.net/ashutc/archive/2011/04/14/348270.html 另两 参考博客 : http://kingtai168.iteye. ...
- UTF-8 编码的文件在处理时要注意 BOM 文件头问题
最近在给项目团队开发一个基于 Java 的通用的 XML 分析器时,设计了一个方法,能够读取现成的 XML 文件进行分析处理,当然 XML 都是采用 UTF-8 进行编码的.但是在用 UltraEdi ...
- pandaboard用wifi时打不开网页
不可以手动修改/etc/resolv.conf,因为重启会被自动清空(raspberry pi 可以) 修改 resolvconf服务的配置文件: /etc/resolvconf/resolv.con ...
- ansible、zabbix、tcpdump
Ansible 源码安装 https://blog.csdn.net/williamfan21c/article/details/53439307 Ansible安装过程中常遇到的错误 http:// ...
- hadoop 学习(二)
我们很荣幸能够见证Hadoop十年从无到有,再到称王.感动于技术的日新月异时,希望通过这篇内容深入解读Hadoop的昨天.今天和明天,憧憬下一个十年. 本文分为技术篇.产业篇.应用篇.展望篇四部分 技 ...