/*
solution:
网络流问题,需要求出路径上的流量。先用拆点法建图,然后直接用edmondKarp算法增广。
添加超源点和超汇点,在超源点和空零件的状态间连边,容量为无穷大。完成品和超汇点之
间也要连上边,容量也是无穷大。同时在可以相互配合的机器之间也连上容量为无穷大的边。
相互配合指的是一台机器生产出来的半成品能够由另外一台机器直接加工,这样的两台机器
称之为可相互配合的机器。
note:
在建立超源点->000...,111...->超汇点时,注意超源点->0022..这样的也要连上
date:
2016/8/13
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 50 + 5;
const int maxm = 10 + 5;
const int INF = 1 << 30;
int input[maxn][12], cap[maxn][maxn], n, flow[maxn][maxn];
int edmondKarp(int s, int t) {
queue<int> q;
int p[maxn], a[maxn];
int f = 0;
memset(flow, 0, sizeof(flow));
while(true) {
memset(a, 0, sizeof(a));
a[s] = INF;
q.push(s);
while(!q.empty()) {
int u = q.front(); q.pop();
for(int v = 0; v < 2*(n+1); v++) if(!a[v] && cap[u][v] > flow[u][v]) {
p[v] = u;
q.push(v);
a[v] = min(a[u], cap[u][v]-flow[u][v]);
}
}
if(a[t] == 0) break;
for(int u=t; u != s; u = p[u]) {
flow[p[u]][u] += a[t];
flow[u][p[u]] -= a[t];
}
f += a[t];
}
return f;
}
int main()
{
//freopen("input.txt", "r", stdin);
int p;
while(~scanf("%d%d", &p, &n)) {
memset(cap, 0, sizeof(cap));
for(int i = 0; i < n; i++)
for(int j = 0; j < 2*p+1; j++) {
scanf("%d", &input[i][j]);
cap[i*2][i*2+1] = input[i][0];
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if(i == j) continue;
bool flag = true;
for(int k = 1; k <= p; k++) {
if(!(input[j][k] == 2 || input[j][k] == input[i][p+k])) {
flag = false;
break;
}
}
if(flag) cap[i*2+1][j*2] = INF;
}
}
int ss = 2 * n, tt = 2 * n + 1;
for(int i = 0; i < n; i++) { //???????000..., 111...?????????
bool isSource = true, isEnd = true;
for(int j = 1; j <= p; j++)
if(!(input[i][j] == 0 || input[i][j] == 2)) isSource = false;
for(int j = p+1; j <= 2*p; j++)
if(input[i][j] != 1) isEnd = false;
if(isSource) cap[ss][i*2] = INF;
if(isEnd) cap[i*2+1][tt] = INF;
}
int ans = edmondKarp(ss, tt);
int cnt = 0;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
if(i != j && flow[i*2+1][j*2] > 0)
cnt++;
}
printf("%d %d\n", ans, cnt);
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) {
if(i != j && flow[i*2+1][j*2] > 0)
printf("%d %d %d\n", i+1, j+1, flow[i*2+1][j*2]);
}
}
return 0;
}
poj3436
最新推荐文章于 2019-08-10 09:56:46 发布