题目链接: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. bzoj2850巧克力王国

    巧克力王国 Time Limit: 60 Sec  Memory Limit: 512 MBSubmit: 861  Solved: 325[Submit][Status][Discuss] Desc ...

  2. poj 3525 求凸包的最大内切圆

    Most Distant Point from the Sea Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3640   ...

  3. ctrl+c,ctrl+d,ctrl+z在linux程序中意义和区别

    原文: http://blog.csdn.net/sxhlovehmm/article/details/41318111 [侵删] ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.   ...

  4. MYSQL 中GROUP BY

    group by 用法解析 group by语法可以根据给定数据列的每个成员对查询结果进行分组统计,最终得到一个分组汇总表. SELECT子句中的列名必须为分组列或列函数.列函数对于GROUP BY子 ...

  5. Highcharts 动态制作3D柱状图

    学习参考菜鸟网站:http://www.runoob.com/highcharts/highcharts-tutorial.html 我是通过后端返回设备数据,进行前端出图,效果如下: 代码如下: d ...

  6. BZOJ 4810 [Ynoi2017]由乃的玉米田 (莫队 + bitset)

    题目链接  BZOJ 4810 首先对询问离线, 莫队算法处理. 首先我们可以用bitset维护处当前区间中是否存在某个数. 对于询问1, 我们可以用 ((f >> q[i].x) &am ...

  7. git(三):第一次github了解使用

    第一次使用github,看了一下使用说明,创建第一个repository,以后还要多学习. 一.Github创建一个新仓库 ······Creat a new repository 创建一个新仓库,点 ...

  8. luogu P1080 国王游戏

    题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国王站在队伍的最 ...

  9. AppCompatActivity

    刚开始看HelloWorld的目录结构然后就发现Android Studio中的是 import android support.v7.app.AppcompatActivity; public cl ...

  10. validation set以及cross validation的常见做法

    如果给定的样本充足,进行模型选择的一种简单方法是随机地将数据集切分成三部分,分为训练集(training set).验证集(validation set)和测试集(testing set).训练集用来 ...