题目链接: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. cf468B Two Sets

    Little X has n distinct integers: p1, p2, ..., pn. He wants to divide all of them into two sets A an ...

  2. LightOJ1106 Gone Fishing

    Gone Fishing John is going on a fishing trip. He has h hours available, and there are n lakes in the ...

  3. Merge into语句用法及其效率问题

    Merge into语句用法及其效率问题 /*Merge into 详细介绍MERGE语句用来合并UPDATE和INSERT语句.通过MERGE语句,根据一张表或子查询的连接条件对另外一张表进行查询, ...

  4. NOIP 前夕 模板整理

    归并排序: #include<iostream> #include<cstdio> #include<cstring> using namespace std; ] ...

  5. SharedPreferences 存储数组+双击退出

    public static void saveApkEnalbleArray(Context context,boolean[] booleanArray) { SharedPreferences p ...

  6. Docker-PostgresSQL

    Postgresql  Docker安装运行 mac环境: 1.拉取官方镜像,并创建容器 zhoumatoMacBook-Pro:~ zhou$ docker search postgresql NA ...

  7. hdu 4587 2013南京邀请赛B题/ / 求割点后连通分量数变形。

    题意:求一个无向图的,去掉两个不同的点后最多有几个连通分量. 思路:枚举每个点,假设去掉该点,然后对图求割点后连通分量数,更新最大的即可.算法相对简单,但是注意几个细节: 1:原图可能不连通. 2:有 ...

  8. 洛谷——P2819 图的m着色问题

    P2819 图的m着色问题 题目背景 给定无向连通图G和m种不同的颜色.用这些颜色为图G的各顶点着色,每个顶点着一种颜色.如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的.图的 ...

  9. CDOJ_844 程序设计竞赛

    原题地址:http://acm.uestc.edu.cn/#/problem/show/844 "你动规无力,图论不稳,数据结构松散,贪心迟钝,没一样像样的,就你还想和我同台竞技,做你的美梦 ...

  10. [Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3396  Solved: 1568[Submit][Statu ...