【最大流+输出路径】POJ-3436 ACM Computer Factory

来源:互联网 发布:淘宝上的美瞳能买吗 编辑:程序博客网 时间:2024/05/22 14:14
ACM Computer Factory
Time Limit: 1000MS Memory Limit: 64M    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 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 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 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

Bold texts appearing in the sample sections are informative and do not form part of the actual data.
————————————————————失望的分割线————————————————————
前言:之前建图没有考虑好数量关系,裸着来结果惨死。甚至在网上找到有人“不拆点”AC的代码,但是有一组数据可以hack掉:
2 410 0 0 0 110 0 0 0 010 0 1 1 110 0 1 1 1
思路:举例来说,X点从A点和B点接受了一些流量,即将流进Y点和Z点,那么问题来了:
A点/B点能发出多少流量?X点能接受/发出多少流量?Y点/Z点能接受多少流量?
A和B发出的流量可能很大,超过了X点的工作效率,X点流进Y点和Z点的流量和又不能超过X点的实际产量。
因此需要将X点拆为X和X'点,两点之间权值为X的效率。其余的顶点之间权值完全可以设置INF。
输入输出规格也就可以分配给X和X'这两个点。对于源点和汇点,源点输出全为0,汇点输入全为1。
还有一个难点,在于路径的记录。这就要理解Dinic模板了。因为每条反向弧初始容量设置为0,因此完成增广之后,反向弧的容量即为路径。
代码如下:
/*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;}


0 0