POJ3436(KB11-A 最大流)

ACM Computer Factory

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8133   Accepted: 2943   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

题意:

电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi。 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移除某些部件)。求电脑公司的单位时间最大产量,以及哪些机器有协作关系,即一台机器把它的产品交给哪些机器加工。

输入:电脑由3个部件组成,共有4台机器,1号机器产量15, 能给空电脑加上2号部件,2号 机器能给空电脑加上2号部件和3号部件, 3号机器能把有1个2号部件和3号部件有无均可的电脑变成成品(每种部件各有一个)
输出:单位时间最大产量25,有两台机器有协作关系,
1号机器单位时间内要将15个电脑给3号机器加工
2号机器单位时间内要将10个电脑给3号机器加工

思路:

拆点建图

网络流模型:

1) 添加一个原点S,S提供最初的原料 00000...
2) 添加一个汇点T, T接受最终的产品 11111...
3) 将每个机器拆成两个点: 编号为i的接收节点,和编号为i+n的产出节点(n是机器数目),前者用于接收原料,后者用于提供加工后的半成品或成品。这两个点之间要连一条边,容量为单位时间产量Qi
4) S 连边到所有接收 "0000..." 或 "若干个0及若干个2" 的机器,容量为无穷大
5) 产出节点连边到能接受其产品的接收节点,容量无穷大
6) 能产出成品的节点,连边到T,容量无穷大。
7) 求S到T的最大流

  1 //2017-08-23
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <iostream>
  5 #include <algorithm>
  6 #include <queue>
  7 #include <vector>
  8
  9 using namespace std;
 10
 11 const int N = 110;
 12 const int P = 20;
 13 const int INF = 0x3f3f3f3f;
 14 int head[N], tot;
 15 struct Edge{
 16     int next, to, w;
 17 }edge[N<<4];
 18
 19 void add_edge(int u, int v, int w){
 20     edge[tot].w = w;
 21     edge[tot].to = v;
 22     edge[tot].next = head[u];
 23     head[u] = tot++;
 24
 25     edge[tot].w = 0;
 26     edge[tot].to = u;
 27     edge[tot].next = head[v];
 28     head[v] = tot++;
 29 }
 30
 31 struct Dinic{
 32     int level[N], S, T;
 33     void init(int _S, int _T){
 34         S = _S;
 35         T = _T;
 36         tot = 0;
 37         memset(head, -1, sizeof(head));
 38     }
 39     bool bfs(){
 40         queue<int> que;
 41         memset(level, -1, sizeof(level));
 42         level[S] = 0;
 43         que.push(S);
 44         while(!que.empty()){
 45             int u = que.front();
 46             que.pop();
 47             for(int i = head[u]; i != -1; i = edge[i].next){
 48                 int v = edge[i].to;
 49                 int w = edge[i].w;
 50                 if(level[v] == -1 && w > 0){
 51                     level[v] = level[u]+1;
 52                     que.push(v);
 53                 }
 54             }
 55         }
 56         return level[T] != -1;
 57     }
 58     int dfs(int u, int flow){
 59         if(u == T)return flow;
 60         int ans = 0, fw;
 61         for(int i = head[u]; i != -1; i = edge[i].next){
 62             int v = edge[i].to, w = edge[i].w;
 63             if(!w || level[v] != level[u]+1)
 64                   continue;
 65             fw = dfs(v, min(flow-ans, w));
 66             ans += fw;
 67             edge[i].w -= fw;
 68             edge[i^1].w += fw;
 69             if(ans == flow)return ans;
 70         }
 71         if(ans == 0)level[u] = 0;
 72         return ans;
 73     }
 74     int maxflow(){
 75         int flow = 0;
 76         while(bfs())
 77           flow += dfs(S, INF);
 78         return flow;
 79     }
 80 }dinic;
 81
 82 int in[N][P], out[N][P];
 83 struct Node{
 84     int u, v, w;
 85     Node(int _u, int _v, int _w):u(_u), v(_v), w(_w){}
 86 };
 87
 88 int main()
 89 {
 90     std::ios::sync_with_stdio(false);
 91     //freopen("inputA.txt", "r", stdin);
 92     int n, p, w;
 93     while(cin>>p>>n){
 94         int s = 0, t = 2*n+1;
 95         dinic.init(s, t);
 96         for(int i = 1; i <= n; i++){
 97             cin>>w;
 98             add_edge(i, i+n, w);
 99             bool fg = true;
100             for(int j = 0; j < p; j++){
101                 cin>>in[i][j];
102                 if(in[i][j] == 1)fg = false;
103             }
104             if(fg)add_edge(0, i, INF);
105             for(int j = 0; j < p; j++)
106                   cin>>out[i][j];
107         }
108         for(int i = 1; i <= n; i++){
109             bool all_one = true;
110             for(int k = 0; k < p; k++)
111                   if(out[i][k] == 0){
112                       all_one = false;
113                     break;
114                 }
115             if(all_one){
116                 add_edge(i+n, t, INF);
117             }
118             for(int j = 1; j <= n; j++){
119                 if(i == j)continue;
120                 bool fg = true;
121                 for(int k = 0; k < p; k++){
122                     if((in[j][k] == 1 && out[i][k] == 0)
123                     || (in[j][k] == 0 && out[i][k] == 1)){
124                         fg = false;
125                         break;
126                     }
127                 }
128                 if(fg)add_edge(i+n, j, INF);
129             }
130         }
131         cout<<dinic.maxflow()<<" ";
132         vector<Node> vec;
133         for(int u = n+1; u < 2*n+1; u++){
134             for(int i = head[u]; i != -1; i = edge[i].next){
135                 int v = edge[i].to;
136                 if(v == t)continue;
137                 if(u-n != v && edge[i^1].w != 0){
138                     Node tmp(u-n, v, edge[i^1].w);
139                     vec.push_back(tmp);
140                 }
141             }
142         }
143         cout<<vec.size()<<endl;
144         for(int i = 0; i < vec.size(); i++)
145               cout<<vec[i].u<<" "<<vec[i].v<<" "<<vec[i].w<<endl;
146     }
147
148     return 0;
149 }
时间: 2024-10-29 00:36:20

POJ3436(KB11-A 最大流)的相关文章

poj3436网络流之最大流拆点

这题看了半天看不懂题意...还是看的网上题意写的 加一个源点一个汇点,把每个点拆成两个,这两个点的流量是v,其他联通的边都设为无穷大 输入没有1的点就与源点连接,输出只有1的点就与汇点连接 还有这个输出技巧,因为每条反向弧初始容量设置为0,因此完成增广之后,反向弧的容量即为路径. #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include

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

POJ 3436 ACM Computer Factory 电脑公司生产电脑有N个机器,每个机器单位时间产量为Qi. 电脑由P个部件组成,每个机器工作时只能把有某些部件的半成品电脑(或什么都没有的空电脑)变成有另一些部件的半成品电脑或完整电脑(也可能移除某些部件).求电脑公司的单位时间最大产量,以及哪些机器有协作关系,即一台机器把它的产品交给哪些机器加工. Sample input 3 4 15  0 0 0  0 1 0 10  0 0 0  0 1 1 30  0 1 2  1 1 1 3

网络流-最大流:两枚[poj1459&amp;poj3436]

说说建图吧- poj1459: 增加超级源点,超级汇点,跑一遍即可. #include <cstdio> #include <cstring> #include <vector> #include <cstdlib> #include <cmath> #include <queue> #include <algorithm> using namespace std; const int MAX = 107; const i

ACMComputerFactory(POJ-3436)【最大流】

题目链接:https://vjudge.net/problem/POJ-3436 题意:要用N台机器来组装电脑,每台电脑由P个零部件构成,每一台机器的输入电脑和输出电脑的每部分都有各自的属性,机器本身也有最大产能,现在求这N台机器能够达到的最大组装速率. 思路:可以转化成最大流问题来做. 首先我们在每台机器之间建立可行的边,再对机器本身的输入和输出端口之间建边,再设一个超级源点S和一个超级汇点T,建立S与每台机器之间和每台机器与T之间的可行边,此时,最大产能即为S->T的最大流. 原文地址:ht

POJ-3436 ACM Computer Factory (最大流[Ford-Fulkerson])

ACM Computer Factory http://poj.org/problem?id=3436 Time Limit: 1000MS   Memory Limit: 65536K         Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. That i

POJ3436 ACM Computer Factory 【最大流】

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5412   Accepted: 1863   Special Judge Description As you know, all the computers used for ACM contests must be identical, so the participants compete on equal terms. Th

POJ3436:ACM Computer Factory(最大流)

ACM Computer Factory Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9963   Accepted: 3738   Special Judge 题目链接:http://poj.org/problem?id=3436 Description: As you know, all the computers used for ACM contests must be identical, so the pa

poj3436(最大流+拆点)

题目给出了每个机器的加工要求和加工成品,问单位时间内的最快加工速度 这道题是比较明显的网络流,这里暴力枚举机器判断是否可以形成加工流水线,然后跑最大流.(好像太简洁了,最大流我不能讲得很清楚) 这里需要注意的是拆点,因为题目给的点的限制,但图上使用的是边,所以我这里考虑把点\(i\)拆成\(i\)和\(i+n\),然后建一条\(w[i]\)的边(题目给出的机器加工速度),对于机器之间,考虑\(Edge(i,j)\),建\(w[i]\)或\(inf\)均可,否则就会让某些点加工的半成品超过它能承受

[转载 ]POJ 1273 最大流模板

转载 百度文库花了5分下的 不过确实是自己需要的东西经典的最大流题POJ1273 ——其他练习题 POJ3436 . 题意描述: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水渠中所能流过的水的最大容量.一道基础的最大流题目.但是模板小心使用,目前只是求单源点的最大流. 参考数据: 输入: 5 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10 输出: 50 程序实现: 增广路算法Edmonds_Karp