题目链接:https://vjudge.net/problem/POJ-3436

ACM Computer Factory
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 jDi,k — output specification for part k.

Constraints

1 ≤ P ≤ 10, 1 ≤ ≤ 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

Bold texts appearing in the sample sections are informative and do not form part of the actual data.

Source

Northeastern Europe 2005, Far-Eastern Subregion

题意:

在一个电脑厂中,一台电脑被分成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 —— 最大流的更多相关文章

  1. poj-3436.ACM Computer Factory(最大流 + 多源多汇 + 结点容量 + 路径打印 + 流量统计)

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10940   Accepted:  ...

  2. POJ3436 ACM Computer Factory(最大流)

    题目链接. 分析: 题意很难懂. 大体是这样的:给每个点的具体情况,1.容量 2.进入状态 3.出去状态.求最大流. 因为有很多点,所以如果一个点的出去状态满足另一个点的进入状态,则这两个点可以连一条 ...

  3. POJ-3436 ACM Computer Factory 最大流 为何拆点

    题目链接:https://cn.vjudge.net/problem/POJ-3436 题意 懒得翻,找了个题意. 流水线上有N台机器装电脑,电脑有P个部件,每台机器有三个参数,产量,输入规格,输出规 ...

  4. POJ3436 ACM Computer Factory 【最大流】

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5412   Accepted: 1 ...

  5. poj3436 ACM Computer Factory, 最大流,输出路径

    POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器.每一个机器单位时间产量为Qi. 电脑由P个部件组成,每一个机器工作时仅仅能把有某些部件的半成品电脑(或什么都没有 ...

  6. POJ3436 ACM Computer Factory(最大流/Dinic)题解

    ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8944   Accepted: 3 ...

  7. POJ-3436 ACM Computer Factory(网络流EK)

    As you know, all the computers used for ACM contests must be identical, so the participants compete ...

  8. Poj 3436 ACM Computer Factory (最大流)

    题目链接: Poj 3436 ACM Computer Factory 题目描述: n个工厂,每个工厂能把电脑s态转化为d态,每个电脑有p个部件,问整个工厂系统在每个小时内最多能加工多少台电脑? 解题 ...

  9. POJ-3436:ACM Computer Factory (Dinic最大流)

    题目链接:http://poj.org/problem?id=3436 解题心得: 题目真的是超级复杂,但解出来就是一个网络流,建图稍显复杂.其实提炼出来就是一个工厂n个加工机器,每个机器有一个效率w ...

随机推荐

  1. Numpy 布尔型数组

    一  给定一个列表,返回大于10的元素. 在python中,有两种方法.一种方法是循环遍历,第二种方法是使用内置函数filter() 在数组中,有更为简单的方法.即布尔型索引 布尔型索引: 将同样大小 ...

  2. SLF4J 简单日志门面 介绍和使用

    参考:http://singleant.iteye.com/blog/934593        http://liuzidong.iteye.com/blog/776072 介绍: 简单日记门面(s ...

  3. 系统进程的Watchdog

    编写者:李文栋 /rayleeya http://rayleeya.iteye.com/blog/1963408 3.1 Watchdog简介 对于像笔者这样没玩过硬件的纯软程序员来说,第一次看到这个 ...

  4. ubuntu下不同版本python安装pip及pip的使用

    由于ubuntu系统自带python2.7(默认)和python3.4,所以不需要自己安装python. 可以使用python -V和python3 -V查看已安装python版本. 在不同版本的py ...

  5. ajax中没法用response下载文件啊

    ajax 下载不太现实第一,http 不支持直接的二进制传输,二进制数据需要编码 例如base64 ,这点服务器端可以实现第二,客户端获得编码后的文件要转换,js应该也可以第三点,最为致命,js无法操 ...

  6. [Bzoj5043][Lydsy1709月赛]密码破译(按位dp)

    5043: [Lydsy1709月赛]密码破译 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 477  Solved: 125[Submit][Sta ...

  7. Hadoop 连接mysql

    1 mysql数据导入到hdfs数据 hadoop提供了org.apache.hadoop.io.Writable接口来实现简单的高效的可序列化的协议,该类基于DataInput和DataOutput ...

  8. 2018.11.7 PION 模拟赛

    期望:100 + 80 + 75 = 255 实际:0 + 80 + 60 = 140 唉~一天比一天犯的错误智障,感觉noip要凉啊... 吓得我赶紧吃几颗药补补脑子. 奶一下大佬: lgj AK ...

  9. Java 利用DFA算法 屏蔽敏感词

    原文:http://www.open-open.com/code/view/1435214601278 import java.io.BufferedReader; import java.io.Fi ...

  10. 聊聊WiFi Hacks:为何你的Karma攻击不好使了

    0.前言 三年前我发表了一篇文章<黑客有办法让你自动连上陌生WiFi>,介绍Karma攻击可以让你的无线设备自动连上黑客的WiFi.当时引起了还算比较热烈的讨论,关于WiFi安全,关于Ka ...