keker123

Untitled

Dec 2nd, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <climits>
  5. #include <queue>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9.  
  10. int V;
  11.  
  12. bool bfs(vector<vector<int>> rGraph, int s, int t, int parent[], int scale){
  13.     bool visited[V];
  14.  
  15.     memset(visited, 0, sizeof(visited));
  16.  
  17.     queue<int> q;
  18.     q.push(s);
  19.     visited[s] = true;
  20.     parent[s] = -1;
  21.     while (!q.empty()) {
  22.         int u = q.front();
  23.         q.pop();
  24.         for (int v = 0; v < V; v++){
  25.             if (!visited[v] && rGraph[u][v] >= scale){
  26.                 if (v == t){
  27.                     parent[v] = u;
  28.                     return true;
  29.                 }
  30.                 q.push(v);
  31.                 parent[v] = u;
  32.                 visited[v] = true;
  33.             }
  34.         }
  35.     }
  36.     return false;
  37. }
  38.  
  39. int karp(vector<vector<int>>& graph, int s, int t, int scale){
  40.     int u, v;
  41.     vector<vector<int>> rGraph(V, vector<int>(V));
  42.     for (u = 0; u < V; u++)
  43.         for (v = 0; v < V; v++)
  44.             rGraph[u][v] = graph[u][v];
  45.  
  46.     int parent[V];
  47.     int max_flow = 0;
  48.     while(bfs(rGraph, s, t, parent, scale)){
  49.         int path_flow = INT_MAX;
  50.         for (v = t; v != s; v = parent[v]) {
  51.             u = parent[v];
  52.             path_flow = min(path_flow, rGraph[u][v]);
  53.         }
  54.         for (v = t; v != s; v = parent[v]) {
  55.             u = parent[v];
  56.             rGraph[u][v] -= path_flow;
  57.             rGraph[v][u] += path_flow;
  58.             graph[u][v] -= path_flow;
  59.             graph[v][u] += path_flow;
  60.         }
  61.         max_flow += path_flow;
  62.     }
  63.     return max_flow;
  64. }
  65.  
  66.  
  67.  
  68. signed main(){
  69.  
  70.     int max_scale = 0;
  71.     int n, m, k, e;
  72.     cin >> n >> m >> k >> e;
  73.     int source = 0;
  74.     int sink = n + m + 2*k + 1;
  75.     V = sink + 1;
  76.     vector<vector<int>> capacity(sink + 1, vector<int>(sink + 1, 0));
  77.     vector<int> prod1(k), prod2(k);
  78.     for (int i = 1; i <= n; ++i) {
  79.         int a;
  80.         cin >> a;
  81.         capacity[source][i] = a;
  82.         max_scale = max(max_scale, a);
  83.     }
  84.     for (int i = n + 1; i <= n + m; ++i) {
  85.         int b;
  86.         cin >> b;
  87.         capacity[source][i] = b;
  88.         max_scale = max(max_scale, b);
  89.     }
  90.     for (int i = 0; i < e; ++i) {
  91.         int type, producer, factory, count;
  92.         cin >> type >> producer >> factory >> count;
  93.         max_scale = max(max_scale, count);
  94.         if (type == 1) {
  95.             capacity[producer][n + m + factory] = count;
  96.             prod1[factory] += count;
  97.         } else {
  98.             capacity[n + producer][n + m + factory] = count;
  99.             prod2[factory] += count;
  100.         }
  101.     }
  102.     for(int i = 1; i <= k; ++i){
  103.         capacity[n + m + i][n + m + k + i] = min(prod1[i], prod2[i]);
  104.     }
  105.     for (int i = 1; i <= k; ++i) {
  106.         int c;
  107.         cin >> c;
  108.         capacity[n + m + k + i][sink] = c;
  109.         max_scale = max(max_scale, c);
  110.     }
  111.  
  112.     int scale = 1;
  113.     while(2*scale <= max_scale) scale *= 2;
  114.     int topflow = 0;
  115.     while(scale >= 1){
  116.         topflow += karp(capacity, 0, V - 1, scale);
  117.         scale /= 2;
  118.     }
  119.  
  120.     cout << topflow;
  121.     return 0;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment