UVa 1212 - Duopoly

链接

传送门

题意

两家公司有多个申请,每个申请包含一些资源的使用权并且会给政府一些资金,同一个公司的申请中不含相同的资源。资源的使用权不能同时批给两个公司,求政府的最大收益。

思路

从原点向T公司的申请连容量为申请金额的边,从M公司的申请向汇点连容量为申请金额的边,两公司冲突的申请之间连容量为无穷大的边。政府解决冲突的最小代价就是源点到汇点的最小割。

代码

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
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = 6010;
const int maxm = 300010;
int p[maxn], vis[maxm];
int g[maxn >> 1][maxn >> 1];

struct Edge {
int from, to, cap, flow;
Edge() {}
Edge(int u, int v, int c, int f): from(u), to(v), cap(c), flow(f) {}
};

struct ISAP {
int n, m, s, t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int p[maxn];
int d[maxn];
int num[maxn];
int cur[maxn];

void AddEdge(int from, int to, int cap) {
edges.push_back(Edge(from, to, cap, 0));
edges.push_back(Edge(to, from, 0, 0));
m = int(edges.size());
G[from].push_back(m - 2);
G[to].push_back(m - 1);
}

void ClearAll(int n) {
this -> n = n;
for (int i = 0; i < n; ++i) {
G[i].clear();
}
edges.clear();
}

bool BFS() {
memset(vis, 0, sizeof vis);
vis[t] = true;
d[t] = 0;
queue<int> Q;
Q.push(t);
while (!Q.empty()) {
int x = Q.front();
Q.pop();
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i] ^ 1];
if (!vis[e.from] && e.cap > e.flow) {
vis[e.from] = true;
d[e.from] = d[x] + 1;
Q.push(e.from);
}
}
}
return vis[s];
}

int Augment() {
int x = t, a = inf;
while (x != s) {
Edge& e = edges[p[x]];
a = min(a, e.cap - e.flow);
x = e.from;
}
x = t;
while (x != s) {
edges[p[x]].flow += a;
edges[p[x] ^ 1].flow -= a;
x = edges[p[x]].from;
}
return a;
}

int Maxflow(int s, int t) {
this -> s = s, this -> t = t;
int flow = 0;
BFS();
memset(num, 0, sizeof num);
for (int i = 0; i < n; ++i) {
++num[d[i]];
}
memset(cur, 0, sizeof cur);
int x = s;
while (d[s] < n) {
if (x == t) {
flow += Augment();
x = s;
}
bool ok = false;
for (int i = cur[x]; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow && d[x] == d[e.to] + 1) {
ok = true;
p[e.to] = G[x][i];
cur[x] = i;
x = e.to;
break;
}
}
if (!ok) {
int m = n - 1;
for (int i = 0; i < G[x].size(); ++i) {
Edge& e = edges[G[x][i]];
if (e.cap > e.flow) {
m = min(m, d[e.to]);
}
}
if (--num[d[x]] == 0) {
break;
}
++num[d[x] = m + 1];
cur[x] = 0;
if (x != s) {
x = edges[p[x]].from;
}
}
}
return flow;
}

void ClearFlow() {
for (int i = 0; i < m; ++i) {
edges[i].flow = 0;
}
}
} isap;

int main() {
int t, tt = 1;
scanf("%d", &t);
while (t--) {
int n, m, ans = 0;
scanf("%d", &n);
memset(vis, -1, sizeof vis);
for (int i = 0; i < n; ++i) {
scanf("%d", &p[i]);
ans += p[i];
while (getchar() != '\n') {
int x;
scanf("%d", &x);
vis[x] = i;
}
}
scanf("%d", &m);
int k = n + m;
for (int i = n; i < k; ++i) {
scanf("%d", &p[i]);
ans += p[i];
while (getchar() != '\n') {
int x;
scanf("%d", &x);
if (vis[x] != -1) {
if (g[vis[x]][i - n] != tt) {
isap.AddEdge(vis[x], i, inf);
g[vis[x]][i - n] = tt;
}
}
}
}
isap.n = k + 5;
for (int i = 0; i < n; ++i) {
isap.AddEdge(k, i, p[i]);
}
for (int i = n; i < k; ++i) {
isap.AddEdge(i, k + 1, p[i]);
}
ans -= isap.Maxflow(k, k + 1);
printf("Case %d:\n%d\n", tt++, ans);
if (t) {
puts("");
}
isap.ClearAll(k + 5);
}
return 0;
}