-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathI.cpp
More file actions
180 lines (162 loc) · 4.69 KB
/
I.cpp
File metadata and controls
180 lines (162 loc) · 4.69 KB
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
#include <bits/stdc++.h>
#define int long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, a, b) for (int i = (a), _b = (b); i >= _b; --i)
#define REP(i, a) for (int i = 0, _a = (a); i < _a; ++i)
#define DEBUG(X) \
{ cerr << #X << " = " << (X) << endl; }
#define PR(A, n) \
{ \
cerr << #A << " = "; \
FOR(_, 1, n) cerr << A[_] << ' '; \
cerr << endl; \
}
#define PR0(A, n) \
{ \
cerr << #A << " = "; \
REP(_, n) cerr << A[_] << ' '; \
cerr << endl; \
}
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int)(x).size())
using namespace std;
double safe_sqrt(double x) { return sqrt(max((double)0.0, x)); }
int GI(ll& x) { return scanf("%lld", &x); }
template <class Flow = int, class Cost = int>
struct MinCostFlow {
const Flow INF_FLOW = 1000111000;
const Cost INF_COST = 1000111000111000LL;
int n, t, S, T;
Flow totalFlow;
Cost totalCost;
vector<int> last, visited;
vector<Cost> dis;
struct Edge {
int to;
Flow cap;
Cost cost;
int next;
Edge(int to, Flow cap, Cost cost, int next)
: to(to), cap(cap), cost(cost), next(next) {}
};
vector<Edge> edges;
MinCostFlow(int n)
: n(n),
t(0),
totalFlow(0),
totalCost(0),
last(n, -1),
visited(n, 0),
dis(n, 0) {
edges.clear();
}
int addEdge(int from, int to, Flow cap, Cost cost) {
edges.push_back(Edge(to, cap, cost, last[from]));
last[from] = t++;
edges.push_back(Edge(from, 0, -cost, last[to]));
last[to] = t++;
return t - 2;
}
pair<Flow, Cost> minCostFlow(int _S, int _T) {
S = _S;
T = _T;
SPFA();
while (1) {
while (1) {
REP(i, n) visited[i] = 0;
if (!findFlow(S, INF_FLOW)) break;
}
if (!modifyLabel()) break;
}
return make_pair(totalFlow, totalCost);
}
private:
void SPFA() {
REP(i, n) dis[i] = INF_COST;
priority_queue<pair<Cost, int> > Q;
Q.push(make_pair(dis[S] = 0, S));
while (!Q.empty()) {
int x = Q.top().second;
Cost d = -Q.top().first;
Q.pop();
// For double: dis[x] > d + EPS
if (dis[x] != d) continue;
for (int it = last[x]; it >= 0; it = edges[it].next)
if (edges[it].cap > 0 && dis[edges[it].to] > d + edges[it].cost)
Q.push(make_pair(-(dis[edges[it].to] = d + edges[it].cost),
edges[it].to));
}
Cost disT = dis[T];
REP(i, n) dis[i] = disT - dis[i];
}
Flow findFlow(int x, Flow flow) {
if (x == T) {
totalCost += dis[S] * flow;
totalFlow += flow;
return flow;
}
visited[x] = 1;
Flow now = flow;
for (int it = last[x]; it >= 0; it = edges[it].next)
// For double: fabs(dis[edges[it].to] + edges[it].cost - dis[x]) < EPS
if (edges[it].cap && !visited[edges[it].to] &&
dis[edges[it].to] + edges[it].cost == dis[x]) {
Flow tmp = findFlow(edges[it].to, min(now, edges[it].cap));
edges[it].cap -= tmp;
edges[it ^ 1].cap += tmp;
now -= tmp;
if (!now) break;
}
return flow - now;
}
bool modifyLabel() {
Cost d = INF_COST;
REP(i, n)
if (visited[i])
for (int it = last[i]; it >= 0; it = edges[it].next)
if (edges[it].cap && !visited[edges[it].to])
d = min(d, dis[edges[it].to] + edges[it].cost - dis[i]);
// For double: if (d > INF_COST / 10) INF_COST = 1e20
if (d == INF_COST) return false;
REP(i, n) if (visited[i]) dis[i] += d;
return true;
}
};
int a[3011], b[3011];
int x[3011], y[3011];
#undef int
int main() {
#define int long long
ios::sync_with_stdio(0);
cin.tie(0);
cout << (fixed) << setprecision(9);
int n, na, nb;
while (cin >> n >> na >> nb) {
FOR(i, 1, n) cin >> a[i];
FOR(i, 1, n) cin >> b[i];
MinCostFlow<int, int> flow(n + 5);
int source = 0;
int sink = n + 1;
int X = n + 2;
int Y = n + 3;
flow.addEdge(source, X, na, 0);
flow.addEdge(source, Y, nb, 0);
FOR(i, 1, n) {
x[i] = flow.addEdge(X, i, 1, -a[i]);
y[i] = flow.addEdge(Y, i, 1, -b[i]);
flow.addEdge(i, sink, 1, 0);
}
auto t = flow.minCostFlow(source, sink);
cout << -t.second << endl;
vector<int> res1, res2;
FOR(i, 1, n) if (flow.edges[x[i]].cap == 0) { res1.push_back(i); }
FOR(i, 1, n) if (flow.edges[y[i]].cap == 0) { res2.push_back(i); }
for (int x : res1) cout << x << ' ';
cout << endl;
for (int x : res2) cout << x << ' ';
cout << endl;
cout << endl;
}
}