Advertisement
Alex_tz307

Cuplaj maxim de cost minim in graf bipartit

Jan 20th, 2021
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5.  
  6. ifstream fin("cmcm.in");
  7. ofstream fout("cmcm.out");
  8.  
  9. const int NMAX = 700;
  10. const int source = 1;
  11. int N, M, E;
  12. vector<pair<int,int>> G[NMAX];
  13. int sink, edge[NMAX][NMAX], capacity[NMAX][NMAX], flow[NMAX][NMAX];
  14. int ans, cnt;
  15.  
  16. void read() {
  17.     fin >> N >> M >> E;
  18.     for(int i = 1; i <= E; ++i) {
  19.         int u, v, w;
  20.         fin >> u >> v >> w;
  21.         ++u, v += N + 1;
  22.         G[u].emplace_back(v, w);
  23.         G[v].emplace_back(u, -w);
  24.         edge[u][v] = i;
  25.         capacity[u][v] = 1;
  26.     }
  27. }
  28.  
  29. void build() {
  30.     sink = N + M + 2;
  31.     for(int i = source + 1; i < N + 2; ++i) {
  32.         G[1].emplace_back(i, 0);
  33.         G[i].emplace_back(1, 0);
  34.         capacity[1][i] = 1;
  35.     }
  36.     for(int i = N + 2; i < sink; ++i) {
  37.         G[i].emplace_back(sink, 0);
  38.         G[sink].emplace_back(i, 0);
  39.         capacity[i][sink] = 1;
  40.     }
  41. }
  42.  
  43. void min_self(int &a, int b) {
  44.     a = min(a, b);
  45. }
  46.  
  47. int BellmanFord() {
  48.     int dp[NMAX], father[NMAX];
  49.     bool used[NMAX];
  50.     memset(dp, 0x3f, sizeof(dp));
  51.     memset(used, false, sizeof(used));
  52.     fill(father, father + NMAX, -1);
  53.     dp[source] = 0;
  54.     used[source] = true;
  55.     queue<int> Q;
  56.     Q.emplace(source);
  57.     while(!Q.empty()) {
  58.         int curr = Q.front();
  59.         Q.pop();
  60.         used[curr] = false;
  61.         for(const pair<int,int> &next : G[curr])
  62.             if(capacity[curr][next.first] > flow[curr][next.first]
  63.                 && dp[next.first] > dp[curr] + next.second) {
  64.                 dp[next.first] = dp[curr] + next.second;
  65.                 father[next.first] = curr;
  66.                 if(!used[next.first]) {
  67.                     Q.emplace(next.first);
  68.                     used[next.first] = true;
  69.                 }
  70.             }
  71.     }
  72.     if(dp[sink] < INF) {
  73.         int flux = INF;
  74.         for(int node = sink; node != source; node = father[node])
  75.             min_self(flux, capacity[father[node]][node] - flow[father[node]][node]);
  76.         for(int node = sink; node != source; node = father[node]) {
  77.             flow[father[node]][node] += flux;
  78.             flow[node][father[node]] -= flux;
  79.         }
  80.         return flux * dp[sink];
  81.     }
  82.     return 0;
  83. }
  84.  
  85. void max_flow() {
  86.     int improve = 1;
  87.     while(improve != 0) {
  88.         improve = BellmanFord();
  89.         ans += improve;
  90.     }
  91. }
  92.  
  93. void print() {
  94.     for(int i = source + 1; i < N + 2; ++i)
  95.         for(int j = N + 2; j < sink; ++j)
  96.             if(flow[i][j] > 0) {
  97.                 ++cnt;
  98.                 break;
  99.             }
  100.     fout << cnt << ' ' << ans << '\n';
  101.     for(int i = source + 1; i < N + 2; ++i)
  102.         for(int j = N + 2; j < sink; ++j)
  103.             if(flow[i][j] > 0) {
  104.                 fout << edge[i][j] << ' ';
  105.                 break;
  106.             }
  107. }
  108.  
  109. int main() {
  110.     read();
  111.     build();
  112.     max_flow();
  113.     print();
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement