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 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 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
2 4
10 0 0 0 1
10 0 0 0 0
10 0 1 1 1
10 0 1 1 1
思路:举例来说,X点从A点和B点接受了一些流量,即将流进Y点和Z点,那么问题来了:
/*
ID: j.sure.1
PROG:
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 long
using 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);
#endif
scanf("%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;
}