ACM Computer Factory(最大流输出方案)

描述

传送门:POJ3436

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 $Q_i\ S_{i,1}\ S_{i,2}…S_{i,P}\ D_{i,1}\ D_{i,2}…D_{i,P}$, where $Q_i$ specifies performance, $S_{i,j}$ — input specification for part $j$, $D_{i,k}$ — output specification for part $k$.

  • Constraints
    $1 ≤ P ≤ 10, 1 ≤ N ≤ 50, 1 ≤ Q_i ≤ 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

1
2
3
4
5
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

1
2
3
4
5
6
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

1
2
3
2 2
100 0 0 1 0
200 0 1 1 1

Sample Output

Sample output 1

1
2
3
25 2
1 3 15
2 3 10

Sample output 2

1
2
3
4
5
6
4 5
1 3 3
3 5 3
1 2 1
2 4 1
4 5 1

Sample output 3

1
0 0

思路

  • 网络流都是作文题……题目看懂建出图就是跑算法0.0
  • 题意:每台机器有工作的零件输入要求和输出情况,以及最大产量,问如何调度才能使得总产量最高。
  • 由于机器有产量限制,所以我们要把机器拆成两个点(很经典的套路了),具体建模方式:
    • 超级源点:$0$;
    • 机器输入端:$1\sim n$;
    • 机器输出端:$n+1\sim 2n$:
    • 超级汇点:$2n+1$。
    • 每个机器的输入端与输出端相连,容量为机器的产量限制;
    • 如果该机器的输入端没有必须的零件(没有$1$),则超级源点与它相连,容量为$+\infty$;
    • 如果该机器能生产所有零件,则让它与超级汇点相连,容量为$+\infty$;
    • 输出零件与输入零件相匹配的机器之间建边,输出→输入,容量为$+\infty$。
  • 这个题目难点在于建图的实现和输出方案,建图的实现我开了结构体各种成员函数去判断。输出方案的话,只要在网络中跑完最大流后找到流量为正的边即可。(注意只要输出有实际意义的边,拆点而建的边和与源汇相连的边是不用输出的)

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <bits/stdc++.h>
using namespace std;
#define clr(a,x) memset(a, x, sizeof(a))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define X first
#define Y second
#define fastin ios_base::sync_with_stdio(0);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-6;

const int maxn = 55;

struct Edge
{
int from, to, cap, flow;
Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};
struct Dinic
{
int n, m, s, t; //结点数,边数(包括反向弧),源点编号和汇点编号
vector<Edge> edges; //边表。edge[e]和edge[e^1]互为反向弧
vector<int> G[maxn]; //邻接表,G[i][j]表示节点i的第j条边在e数组中的序号
bool vis[maxn]; //BFS使用
int d[maxn]; //从起点到i的距离
int cur[maxn]; //当前弧下标
void init(int n)
{
this->n = n;
for (int i = 0; i < n; i++) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap)
{
edges.pb(Edge(from, to, cap, 0));
edges.pb(Edge(to, from, 0, 0));
m = edges.size();
G[from].pb(m - 2);
G[to].pb(m - 1);
}
bool BFS()
{
clr(vis, 0);
clr(d, 0);
queue<int> q;
q.push(s);
d[s] = 0;
vis[s] = 1;
while (!q.empty())
{
int x = q.front();
q.pop();
for (int i = 0; i < G[x].size(); i++)
{
Edge& e = edges[G[x][i]];
if (!vis[e.to] && e.cap > e.flow)
{
vis[e.to] = 1;
d[e.to] = d[x] + 1;
q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x, int a)
{
if (x == t || a == 0) return a;
int flow = 0, f;
for (int& i = cur[x]; i < G[x].size(); i++)
{
//从上次考虑的弧
Edge& e = edges[G[x][i]];
if (d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0)
{
e.flow += f;
edges[G[x][i] ^ 1].flow -= f;
flow += f;
a -= f;
if (a == 0) break;
}
}
return flow;
}
int Maxflow(int s, int t)
{
this->s = s;
this->t = t;
int flow = 0;
while (BFS())
{
clr(cur, 0);
flow += DFS(s, INF);
}
return flow;
}
void print(int s, int t, int n)
{
int mf = Maxflow(s, t);
vector <Edge> vec;
for (int i = 0; i < edges.size(); i++)
{
Edge& e = edges[i];
if (e.flow <= 0 || e.from == s || e.to == t || e.to - e.from == n) continue;
int u = e.from > n ? e.from - n : e.from;
int v = e.to;
vec.pb(Edge(u, v, 0, e.flow));
}
int m = vec.size();
printf("%d %d\n", mf, m);
for (int i = 0; i < vec.size(); i++)
{
Edge& e = vec[i];
printf("%d %d %d\n", e.from, e.to, e.flow);
}
}
} ans;

struct node
{
int p, in[10], out[10];
bool In, Out;
void init(int p)
{
this->p = p;
for (int i = 0; i < p; i++)
scanf("%d", in + i);
In = in1();
for (int i = 0; i < p; i++)
scanf("%d", out + i);
Out = out0();
}
bool in1()
{
for (int i = 0; i < p; i++)
if (in[i] == 1)
return true;
return false;
}
bool out0()
{
for (int i = 0; i < p; i++)
if (out[i] == 0)
return true;
return false;
}
} a[maxn];

inline bool judge(node a, node b)
{
int n = a.p;
for (int i = 0; i < n; i++)
if (a.out[i] + b.in[i] == 1)
return false;
return true;
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);
freopen("1.out", "w", stdout);
#endif
int p, n;

while (~scanf("%d%d", &p, &n))
{
int s = 0, t = n << 1 | 1;
ans.init(t + 1);
for (int i = 1; i <= n; i++)
{
int w;
scanf("%d", &w);
ans.AddEdge(i, n + i, w);
a[i].init(p);
if (!a[i].In) ans.AddEdge(s, i, INF);
if (!a[i].Out) ans.AddEdge(n + i, t, INF);
for (int j = 1; j < i; j++)
{
if (a[j].In && a[i].Out && judge(a[i], a[j]))
ans.AddEdge(i + n, j, INF);
if (a[i].In && a[j].Out && judge(a[j], a[i]))
ans.AddEdge(j + n, i, INF);
}
}
ans.print(s, t, n);
}
return 0;
}
捐助作者