【最大流+输出路径】POJ-3436 ACM Computer Factory
来源:互联网 发布:淘宝上的美瞳能买吗 编辑:程序博客网 时间:2024/05/22 14:14
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 Pnumbers 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,1Di,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 13 415 0 0 0 0 1 010 0 0 0 0 1 130 0 1 2 1 1 13 0 2 1 1 1 1Sample input 23 55 0 0 0 0 1 0100 0 1 0 1 0 13 0 1 0 1 1 01 1 0 1 1 1 0300 1 1 2 1 1 1Sample input 32 2100 0 0 1 0200 0 1 1 1
Sample Output
Sample output 125 21 3 152 3 10Sample output 24 51 3 33 5 31 2 12 4 14 5 1Sample output 30 0
Hint
2 410 0 0 0 110 0 0 0 010 0 1 1 110 0 1 1 1
思路:举例来说,X点从A点和B点接受了一些流量,即将流进Y点和Z点,那么问题来了:/*ID: j.sure.1PROG:LANG: C++*//****************************************/#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <string>#include <climits>#include <iostream>#define LL long longusing namespace std;const int INF = 0x3f3f3f3f;/****************************************/const int N = 55, M = 1e5;int tot, head[N], cur[N], lev[N], q[N], s[N], S, T;struct Node {int u, v, cap;int next;}edge[M];int P, n, table[N<<1][10] = {0};int path[N][N] = {0}, line = 0;void init(){tot = 0;memset(head, -1, sizeof(head));}void add(int u, int v, int c){edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;edge[tot].next = head[u]; head[u] = tot++;}bool bfs(){memset(lev, -1, sizeof(lev));lev[S] = 0;int fron = 0, rear = 0;q[rear++] = S;while(fron < rear) {int u = q[fron%N]; fron++;for(int i = head[u]; i != -1; i = edge[i].next) {int v = edge[i].v;if(edge[i].cap && lev[v] == -1) {lev[v] = lev[u] + 1;q[rear%N] = v; rear++;if(v == T) return true;}}}return false;}void Path(int u, int v, int w){if(u <= n || v > n) return ;//不是应该记录的路径u -= n;//节约内存if(u != S && v != T) {if(path[u][v]) {path[u][v] = max(path[u][v], w);}else {path[u][v] = w;line++;}}}int Dinic(){int ret = 0;while(bfs()) {memcpy(cur, head, sizeof(head));int u = S, top = 0;while(1) {if(u == T) {int mini = INF, loc;for(int i = 0; i < top; i++) {if(mini > edge[s[i]].cap) {mini = edge[s[i]].cap;loc = i;}}for(int i = 0; i < top; i++) {edge[s[i]].cap -= mini;edge[s[i]^1].cap += mini;//反向弧的扩充,即为要记录的路径Path(edge[s[i]].u, edge[s[i]].v, edge[s[i]^1].cap);}ret += mini;top = loc;u = edge[s[top]].u;}int &i = cur[u];for(; i != -1; i = edge[i].next) {int v = edge[i].v;if(edge[i].cap && lev[v] == lev[u] + 1) break;}if(i != -1) {s[top++] = i;u = edge[i].v;//更新u为下一点}else {if(!top) break;lev[u] = -1;u = edge[s[--top]].u;}}}return ret;}int main(){#ifdef J_Sure//freopen("000.in", "r", stdin);//freopen("888.out", "w", stdout);#endifscanf("%d%d", &P, &n);S = 0; T = n<<1|1;init();int c;for(int i = 1; i <= n; i++) {scanf("%d", &c);add(i, n+i, c); add(n+i, i, 0);for(int p = 0; p < P; p++) {scanf("%d", &table[i][p]);}for(int p = 0; p < P; p++) {scanf("%d", &table[n+i][p]);}}for(int i = 1; i <= n; i++) {bool flag_s = true, flag_t = true;for(int p = 0; p < P; p++) {if(table[i][p] == 1) flag_s = false;if(table[n+i][p] != 1) flag_t = false;}if(flag_s) {add(S, i, INF); add(i, S, 0);}if(flag_t) {add(n+i, T, INF); add(T, n+i, 0);}//满足与源点/汇点相连的条件for(int j = 1; j <= n; j++) if(i != j) {bool flag = true;for(int p = 0; p < P; p++) {if(table[n+i][p] + table[j][p] == 1) {flag = false; break;}//0与1不能搭配}if(flag) {add(n+i, j, INF); add(j, n+i, 0);}}}int ret = Dinic();//注意,尽量避免直接输出一个有返回值的函数printf("%d %d\n", ret, line);for(int i = 1; i <= n; i++) {for(int j = 1; j <= n; j++) if(path[i][j]) {printf("%d %d %d\n", i, j, path[i][j]);}}return 0;}
- POJ 3436 ACM Computer Factory (最大流 + 输出路径)
- 【最大流+输出路径】POJ-3436 ACM Computer Factory
- POJ 3436 ACM Computer Factory 最大流输出路径
- poj 3436 ACM Computer Factory 最大流拆点+输出路径
- poj 3436 ACM Computer Factory 【图论-网络流-最大流-EK-输出路径】
- POJ 3436ACM Computer Factory 最大流+路径输出 dinic模板
- POJ 3436-ACM Computer Factory(最大流输出路径-Edmond-Karp算法)
- [POJ 3436]ACM Computer Factory[最大流][打印路径]
- poj 3436 ACM Computer Factory(最大流)
- poj 3436 ACM Computer Factory (最大流)
- poj 3436 ACM Computer Factory (最大流)
- POJ 3436 ACM Computer Factory 最大流
- POJ 3436 ACM Computer Factory 最大流
- POJ 3436 ACM Computer Factory(最大流)
- poj 3436 ACM Computer Factory(最大流)
- poj3436 ACM Computer Factory, 最大流,输出路径
- poj 3436 ACM Computer Factory (最大流 拆点建图 找路径)@
- poj 3436 ACM Computer Factory(网络流最大流)
- 给自己的备忘录——SharedPreferences
- Hdu 5029 Relief grain(树链剖分)
- Openfire开发配置,Openfire源代码配置,OpenFire二次开发配置
- Art of Multiprocessor Programming 答案 ch17
- JAVA组件焦点的特性:获取组件时其顶层组件必须为可见的
- 【最大流+输出路径】POJ-3436 ACM Computer Factory
- Linux shell脚本编写基础
- 设计数组类扩展数组的功能
- JavaFX战旗类游戏开发 第六课 移动范围的获取
- 老鸟的Python入门教程
- java电影网站开发经验1
- 探究iOS Constraints 中的Multiplier参数。
- 阿里云服务器CentOS系统中mb_strlen 不支持
- HDU-#5037 Frog(贪心)