Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker, "/STACK:16777216")
- #include <cstdio>
- #include <cmath>
- #include <iostream>
- #include <map>
- #include <vector>
- #include <algorithm>
- #include <iomanip>
- using namespace std;
- #define debug(x) cerr << fixed << setprecision(2) << "DEBUG: " << #x << " = " << x << endl;
- #define all(x) x.begin(), x.end()
- #define sz(x) ((int)(x.size()))
- #define forn(i, n) for (int i = 0; i < n; ++i)
- #define PATH "C:\\Users\\ValenKof\\Desktop\\"
- #define pb push_back
- #define mp make_pair
- typedef long long ll;
- const int K = 5000;
- const int N = 1 + K + K + 1;
- const int M = 2 * (K + 20000 + K);
- const int INF = 1000000000;
- int S, T, F = 0;
- int w1[K], w2[K];
- int head[N];
- int from[N];
- int dist[N];
- int cost[M];
- int cap[M];
- int linke[M];
- int dest[M];
- int id[M];
- bool inqueue[N];
- const int BUFFER = 1 << 15;
- int q[BUFFER];
- int qt = 0, qh = 0;
- void add_arc(int u, int v, int COST, int CAP, int ID) {
- dest[F] = v;
- linke[F] = head[u];
- cost[F] = COST;
- cap[F] = CAP;
- id[F] = ID;
- head[u] = F++;
- dest[F] = u;
- linke[F] = head[v];
- cost[F] = -COST;
- cap[F] = 0;
- id[F] = ID;
- head[v] = F++;
- }
- inline void push(const int u) {
- if (!inqueue[u]) {
- q[qt++] = u;
- inqueue[u] = true;
- qt &= BUFFER - 1;
- }
- }
- inline int pop() {
- int result = q[qh++];
- qh &= BUFFER - 1;
- inqueue[result] = false;
- return result;
- }
- inline bool empty() {
- return qh == qt;
- }
- bool bf() {
- fill(dist, dist + T + 1, -INF);
- dist[S] = 0;
- push(S);
- while (!empty()) {
- int u = pop();
- for (int arc = head[u]; arc != -1; arc = linke[arc]) {
- int v = dest[arc];
- if (cap[arc] > 0 && dist[v] < dist[u] + cost[arc]) {
- dist[v] = dist[u] + cost[arc];
- from[v] = arc;
- push(v);
- }
- }
- }
- return dist[T] != -INF;
- }
- int dfs() {
- int v = T;
- while (v != S) {
- int arc = from[v];
- --cap[arc];
- ++cap[arc ^ 1];
- v = dest[arc ^ 1];
- }
- return dist[T];
- }
- vector<int> tmp;
- void shuffle(int u) {
- tmp.resize(0);
- for (int arc = head[u]; arc != -1; arc = linke[arc]) {
- tmp.pb(arc);
- }
- random_shuffle(all(tmp));
- head[u] = -1;
- for (int i = 0; i < sz(tmp); ++i) {
- linke[tmp[i]] = head[u];
- head[u] = tmp[i];
- }
- }
- int main() {
- //freopen(PATH"in.txt", "r", stdin);
- //freopen(PATH"out.txt", "w", stdout);
- freopen("matching.in", "r", stdin);
- freopen("matching.out", "w", stdout);
- srand(110292);
- int n, m, e;
- scanf("%d %d %d", &n, &m, &e);
- S = n + m;
- T = S + 1;
- fill(head, head + T + 1, -1);
- for (int i = 0; i < n; ++i) {
- scanf("%d", &w1[i]);
- }
- for (int i = 0; i < m; ++i) {
- scanf("%d", &w2[i]);
- }
- for (int i = 0, u, v; i < e; ++i) {
- scanf("%d %d", &u, &v);
- --u, --v;
- add_arc(u, n + v, w1[u] + w2[v], 1, i + 1);
- }
- for (int i = 0; i < n; ++i) {
- add_arc(S, i, 0, 1, -1);
- }
- for (int i = 0; i < m; ++i) {
- add_arc(n + i, T, 0, 1, -1);
- }
- for (int i = 0; i <= T; ++i) {
- //shuffle(i);
- }
- int ans = 0;
- while (bf()) {
- int pushed = dfs();
- if (pushed <= 0) break;
- ans += pushed;
- }
- printf("%d\n", ans);
- vector<int> arcs;
- for (int i = 0; i < n; ++i) {
- for (int arc = head[i]; arc != -1; arc = linke[arc]) {
- if (dest[arc] != S && cap[arc] == 0) {
- arcs.pb(id[arc]);
- }
- }
- }
- printf("%d\n", sz(arcs));
- sort(all(arcs));
- for (int i = 0; i < sz(arcs); ++i) {
- printf("%d ", arcs[i]);
- }
- printf("\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement