Alex_tz307

Flux maxim de cost minim Bellman-Ford + Dijkstra

Oct 16th, 2020 (edited)
211
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.  
  3. using namespace std;
  4.  
  5. ifstream fin("fmcm.in");
  6. ofstream fout("fmcm.out");
  7.  
  8. int N, source, sink, min_cost;
  9. vector < vector < int > > Graph, Capacity, Cost;
  10. vector < int > parent, old_d, real_d, D;
  11.  
  12. const int INF = 2e9;
  13.  
  14. inline void min_self(int& a, int b) {
  15.     a = min(a, b);
  16. }
  17.  
  18. void BellmanFord() {
  19.     old_d.assign(N + 1, INF);
  20.     old_d[source] = 0;
  21.     queue < int > Q;
  22.     Q.emplace(source);
  23.     vector < bool > inQ(N + 1);
  24.     inQ[source] = true;
  25.     while(!Q.empty()) {
  26.         int curr = Q.front();
  27.         Q.pop();
  28.         inQ[curr] = false;
  29.         for(int next : Graph[curr])
  30.             if(Capacity[curr][next] && old_d[curr] + Cost[curr][next] < old_d[next]) {
  31.                 old_d[next] = old_d[curr] + Cost[curr][next];
  32.                 if(!inQ[next]) {
  33.                     Q.emplace(next);
  34.                     inQ[next] = true;
  35.                 }
  36.             }
  37.     }
  38. }
  39.  
  40. bool Dijkstra() {
  41.     priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > Q;
  42.     D.assign(N + 1, INF);
  43.     D[source] = real_d[source] = 0;
  44.     Q.emplace(0, source);
  45.     while(!Q.empty()) {
  46.         int cost = Q.top().first,
  47.             curr = Q.top().second;
  48.         Q.pop();
  49.         if(D[curr] != cost)
  50.             continue;
  51.         for(int next : Graph[curr])
  52.             if(Capacity[curr][next]) {
  53.                 int new_d = D[curr] + Cost[curr][next] + old_d[curr] - old_d[next];
  54.                 if(new_d < D[next]) {
  55.                     D[next] = new_d;
  56.                     real_d[next] = real_d[curr] + Cost[curr][next];
  57.                     parent[next] = curr;
  58.                     Q.emplace(D[next], next);
  59.                 }
  60.             }
  61.     }
  62.     old_d = real_d;
  63.     if(D[sink] == INF)
  64.         return false;
  65.     int new_flow = INF;
  66.     for(int node = sink; node != source; node = parent[node])
  67.         min_self(new_flow, Capacity[parent[node]][node]);
  68.     if(new_flow == 0)
  69.         return true;
  70.     min_cost += new_flow * real_d[sink];
  71.     for(int node = sink; node != source; node = parent[node]) {
  72.         Capacity[parent[node]][node] -= new_flow;
  73.         Capacity[node][parent[node]] += new_flow;
  74.     }
  75.     return true;
  76. }
  77.  
  78. int main() {
  79.     fin.sync_with_stdio(false);
  80.     fout.sync_with_stdio(false);
  81.     fin.tie(nullptr);
  82.     fout.tie(nullptr);
  83.     int M;
  84.     fin >> N >> M >> source >> sink;
  85.     Graph.resize(N + 1);
  86.     parent.resize(N + 1);
  87.     real_d.resize(N + 1);
  88.     Capacity = Cost = vector < vector < int > >(N + 1, vector < int >(N + 1));
  89.     while(M--) {
  90.         int u, v, capacity, w;
  91.         fin >> u >> v >> capacity >> w;
  92.         Graph[u].emplace_back(v);
  93.         Graph[v].emplace_back(u);
  94.         Capacity[u][v] = capacity;
  95.         Cost[u][v] = w;
  96.         Cost[v][u] = -w;
  97.     }
  98.     BellmanFord();
  99.     while(Dijkstra());
  100.     fout << min_cost;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment