Advertisement
Guest User

E

a guest
Apr 14th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:16777216")
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <map>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <iomanip>
  9.  
  10. using namespace std;
  11.  
  12. #define debug(x) cerr << fixed << setprecision(2) << "DEBUG: " << #x << " = " << x << endl;
  13. #define all(x) x.begin(), x.end()
  14. #define sz(x) ((int)(x.size()))
  15. #define forn(i, n) for (int i = 0; i < n; ++i)
  16. #define PATH "C:\\Users\\ValenKof\\Desktop\\"
  17. #define pb push_back
  18. #define mp make_pair
  19.  
  20. typedef long long ll;
  21.  
  22. const int K = 5000;
  23. const int N = 1 + K + K + 1;
  24. const int M = 2 * (K + 20000 + K);
  25. const int INF = 1000000000;
  26.  
  27. int S, T, F = 0;
  28.  
  29. int w1[K], w2[K];
  30.  
  31. int head[N];
  32. int from[N];
  33. int dist[N];
  34.  
  35. int cost[M];
  36. int cap[M];
  37. int linke[M];
  38. int dest[M];
  39.  
  40. int id[M];
  41.  
  42. bool inqueue[N];
  43.  
  44. const int BUFFER = 1 << 15;
  45. int q[BUFFER];
  46.  
  47. int qt = 0, qh = 0;
  48.  
  49. void add_arc(int u, int v, int COST, int CAP, int ID) {
  50.   dest[F] = v;
  51.   linke[F] = head[u];
  52.   cost[F] = COST;
  53.   cap[F] = CAP;
  54.   id[F] = ID;
  55.   head[u] = F++;
  56.  
  57.   dest[F] = u;
  58.   linke[F] = head[v];
  59.   cost[F] = -COST;
  60.   cap[F] = 0;
  61.   id[F] = ID;
  62.   head[v] = F++;
  63. }
  64.  
  65. inline void push(const int u) {
  66.   if (!inqueue[u]) {
  67.     q[qt++] = u;
  68.     inqueue[u] = true;
  69.     qt &= BUFFER - 1;
  70.   }
  71. }
  72.  
  73. inline int pop() {
  74.   int result = q[qh++];
  75.   qh &= BUFFER - 1;
  76.   inqueue[result] = false;
  77.   return result;
  78. }
  79.  
  80. inline bool empty() {
  81.   return qh == qt;
  82. }
  83.  
  84. bool bf() {
  85.   fill(dist, dist + T + 1, -INF);
  86.  
  87.   dist[S] = 0;
  88.   push(S);
  89.   while (!empty()) {
  90.     int u = pop();
  91.     for (int arc = head[u]; arc != -1; arc = linke[arc]) {
  92.       int v = dest[arc];
  93.       if (cap[arc] > 0 && dist[v] < dist[u] + cost[arc]) {
  94.         dist[v] = dist[u] + cost[arc];
  95.         from[v] = arc;
  96.         push(v);
  97.       }
  98.     }
  99.   }
  100.  
  101.   return dist[T] != -INF;
  102. }
  103.  
  104. int dfs() {
  105.   int v = T;
  106.   while (v != S) {
  107.     int arc = from[v];
  108.     --cap[arc];
  109.     ++cap[arc ^ 1];
  110.     v = dest[arc ^ 1];
  111.   }  
  112.   return dist[T];
  113. }
  114.  
  115.  
  116.  vector<int> tmp;
  117.  void shuffle(int u) {
  118.   tmp.resize(0);
  119.   for (int arc = head[u]; arc != -1; arc = linke[arc]) {
  120.     tmp.pb(arc);
  121.   }
  122.   random_shuffle(all(tmp));
  123.   head[u] = -1;
  124.   for (int i = 0; i < sz(tmp); ++i) {
  125.     linke[tmp[i]] = head[u];
  126.     head[u] = tmp[i];
  127.   }
  128.  }
  129.  
  130. int main() {
  131.     //freopen(PATH"in.txt", "r", stdin);
  132.   //freopen(PATH"out.txt", "w", stdout);
  133.    
  134.   freopen("matching.in", "r", stdin);
  135.   freopen("matching.out", "w", stdout);
  136.  
  137.   srand(110292);
  138.   int n, m, e;
  139.   scanf("%d %d %d", &n, &m, &e);
  140.  
  141.   S = n + m;
  142.   T = S + 1;
  143.   fill(head, head + T + 1, -1);
  144.    
  145.   for (int i = 0; i < n; ++i) {
  146.     scanf("%d", &w1[i]);
  147.   }  
  148.  
  149.   for (int i = 0; i < m; ++i) {
  150.     scanf("%d", &w2[i]);
  151.   }
  152.  
  153.   for (int i = 0, u, v; i < e; ++i) {
  154.     scanf("%d %d", &u, &v);
  155.     --u, --v;
  156.     add_arc(u, n + v, w1[u] + w2[v], 1, i + 1);
  157.   }
  158.  
  159.   for (int i = 0; i < n; ++i) {
  160.     add_arc(S, i, 0, 1, -1);
  161.   }
  162.   for (int i = 0; i < m; ++i) {
  163.     add_arc(n + i, T, 0, 1, -1);
  164.   }
  165.  
  166.   for (int i = 0; i <= T; ++i) {
  167.     //shuffle(i);
  168.   }
  169.  
  170.   int ans = 0;
  171.   while (bf()) {
  172.     int pushed = dfs();
  173.     if (pushed <= 0) break;
  174.     ans += pushed;    
  175.   }
  176.  
  177.  
  178.   printf("%d\n", ans);
  179.   vector<int> arcs;
  180.   for (int i = 0; i < n; ++i) {
  181.     for (int arc = head[i]; arc != -1; arc = linke[arc]) {
  182.       if (dest[arc] != S && cap[arc] == 0) {
  183.         arcs.pb(id[arc]);
  184.       }
  185.     }
  186.   }
  187.   printf("%d\n", sz(arcs));
  188.   sort(all(arcs));
  189.   for (int i = 0; i < sz(arcs); ++i) {
  190.     printf("%d ", arcs[i]);
  191.   }
  192.   printf("\n");
  193.   return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement