Advertisement
Alex_tz307

USACO 2019 December Contest, Gold Problem 1. Milk Pumping

May 30th, 2021
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define INF 0x3f3f3f3f
  3.  
  4. using namespace std;
  5. using ld = long double;
  6.  
  7. ifstream fin("pump.in");
  8. ofstream fout("pump.out");
  9.  
  10. const int MAXN = 1e3;
  11. vector<pair<int,int>> G[MAXN];
  12. int w[MAXN], f[MAXN], dp[MAXN], prv[MAXN], flow[MAXN];
  13.  
  14. struct raport {
  15.   int x, y;
  16.  
  17.   bool operator > (const raport &A) const {
  18.     return x * A.y > y * A.x;
  19.   }
  20. };
  21.  
  22. void min_self(int &x, int y) {
  23.   x = min(x, y);
  24. }
  25.  
  26. void Dijkstra(int n, int low) {
  27.   for (int u = 0; u < n; ++u)
  28.     dp[u] = prv[u] = INF;
  29.   dp[0] = 0;
  30.   priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;
  31.   pq.emplace(0, 0);
  32.   while (!pq.empty()) {
  33.     int d, u;
  34.     tie(d, u) = pq.top();
  35.     pq.pop();
  36.     if (dp[u] != d)
  37.       continue;
  38.     for (auto it : G[u]) {
  39.       int v, e;
  40.       tie(v, e) = it;
  41.       if (f[e] < low)
  42.         continue;
  43.       if (dp[v] > d + w[e]) {
  44.         dp[v] = d + w[e];
  45.         prv[v] = u;
  46.         flow[v] = f[e];
  47.         pq.emplace(dp[v], v);
  48.       }
  49.     }
  50.   }
  51. }
  52.  
  53. int main() {
  54.   int n, m;
  55.   fin >> n >> m;
  56.   set<int> F;
  57.   for (int i = 0; i < m; ++i) {
  58.     int u, v;
  59.     fin >> u >> v >> w[i] >> f[i];
  60.     --u, --v;
  61.     G[u].emplace_back(v, i);
  62.     G[v].emplace_back(u, i);
  63.     F.emplace(f[i]);
  64.   }
  65.   raport ans{0, 1};
  66.   for (int flow_rate : F) {
  67.     Dijkstra(n, flow_rate);
  68.     if (dp[n - 1] == INF)
  69.       break;
  70.     raport path{INF, dp[n - 1]};
  71.     int node = n - 1;
  72.     while (prv[node] != INF) {
  73.       min_self(path.x, flow[node]);
  74.       node = prv[node];
  75.     }
  76.     if (path > ans)
  77.       ans = path;
  78.   }
  79.   fout << int(1e6 * (ld)ans.x / ans.y) << '\n';
  80.   return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement