matistjati

Untitled

Jun 23rd, 2026
6
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define rep(i, a, b) for(int i = a; i < (b); ++i)
  5. #define all(x) begin(x), end(x)
  6. #define sz(x) (int)(x).size()
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef vector<int> vi;
  10.  
  11. const ll INF = numeric_limits<ll>::max() / 4;
  12.  
  13. struct MCMF {
  14. struct edge {
  15. int from, to, rev;
  16. ll cap, cost, flow;
  17. };
  18. int N;
  19. vector<vector<edge>> ed;
  20. vector<ll> dist, pi;
  21. vector<edge*> par;
  22.  
  23. MCMF(int N) : N(N), ed(N), dist(N), pi(N), par(N) {}
  24.  
  25. void addEdge(int from, int to, ll cap, ll cost) {
  26. if (from == to) return;
  27. ed[from].push_back(edge{ from,to,sz(ed[to]),cap,cost,0 });
  28. ed[to].push_back(edge{ to,from,sz(ed[from])-1,0,-cost,0 });
  29. }
  30.  
  31. void path(int s) {
  32. fill(all(dist), INF);
  33. dist[s] = 0;
  34.  
  35. priority_queue<pair<ll, int>> q;
  36. q.push({ 0, s });
  37.  
  38. while (!q.empty()) {
  39. auto [d,u] = q.top(); q.pop();
  40. if (-d > dist[u]) continue;
  41. for (edge& e : ed[u]) {
  42. ll val = pi[u] - d - pi[e.to] + e.cost;
  43. if (e.cap - e.flow > 0 && val < dist[e.to]) {
  44. dist[e.to] = val;
  45. par[e.to] = &e;
  46. q.push({ -dist[e.to], e.to });
  47. }
  48. }
  49. }
  50. rep(i,0,N) pi[i] = min(pi[i] + dist[i], INF);
  51. }
  52.  
  53. pair<ll, ll> maxflow(int s, int t) {
  54. ll totflow = 0, totcost = 0;
  55. while (path(s), dist[t]!=INF) {
  56. ll fl = INF;
  57. for (edge* x = par[t]; x; x = par[x->from])
  58. fl = min(fl, x->cap - x->flow);
  59.  
  60. totflow += fl;
  61. for (edge* x = par[t]; x; x = par[x->from]) {
  62. x->flow += fl;
  63. ed[x->to][x->rev].flow -= fl;
  64. }
  65. }
  66. rep(i,0,N) for(edge& e : ed[i]) totcost += e.cost * e.flow;
  67. return {totflow, totcost/2};
  68. }
  69.  
  70. // If some costs can be negative, call this before maxflow:
  71. void setpi(int s) { // (otherwise, leave this out)
  72. fill(all(pi), INF); pi[s] = 0;
  73. int it = N, ch = 1; ll v;
  74. while (ch-- && it--)
  75. rep(i,0,N) if (pi[i] != INF)
  76. for (edge& e : ed[i]) if (e.cap)
  77. if ((v = pi[i] + e.cost) < pi[e.to])
  78. pi[e.to] = v, ch = 1;
  79. assert(it >= 0); // negative cost cycle
  80. }
  81. };
  82.  
  83. int main() {
  84. cin.tie(0)->sync_with_stdio(0);
  85.  
  86. int sat[3][4] = {
  87. {4,3,2,1},
  88. {8,7,6,5},
  89. {12,11,10,9}
  90. };
  91.  
  92. int n, m;
  93. while (cin >> n >> m && n) {
  94. int numnodes = n + m + 2;
  95. MCMF flow(numnodes);
  96. int source = numnodes - 1;
  97. int sink = numnodes - 2;
  98.  
  99. rep(i, 0, n) {
  100. int c;
  101. cin >> c;
  102. flow.addEdge(m + i, sink, c, 0);
  103. }
  104. rep(i, 0, m) {
  105. flow.addEdge(source, i, 1, 0);
  106. }
  107.  
  108. rep(i, 0, m) {
  109. int y,c1,c2,c3,c4;
  110. cin >> y >> c1 >> c2 >> c3 >> c4;
  111. y--;
  112. int choice[4] = { c1,c2,c3,c4 };
  113. rep(j, 0, 4) {
  114. flow.addEdge(i, m + choice[j], 1, -sat[y][j]);
  115. }
  116. }
  117. flow.setpi(source);
  118. cout << -flow.maxflow(source, sink).second << "\n";
  119. }
  120.  
  121. return 0;
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment