Guest User

Untitled

a guest
Dec 18th, 2017
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <cmath>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6. #include <set>
  7. #include <map>
  8. #include <list>
  9. #include <time.h>
  10. #include <math.h>
  11. #include <random>
  12. #include <deque>
  13. #include <queue>
  14. #include <cassert>
  15. #include <unordered_map>
  16. #include <iomanip>
  17. #include <bitset>
  18.  
  19. using namespace std;
  20.  
  21. typedef long long ll;
  22.  
  23. mt19937 rnd(228);
  24.  
  25. struct edge
  26. {
  27.     int v, to, time, cost;
  28. };
  29.  
  30. const int N = 2000 + 1;
  31. const double inf = 1e18;
  32.  
  33. vector <edge> g[N];
  34. int sum_a[N];
  35. int sum_b[N];
  36. double dist[N];
  37. ll best[N];
  38.  
  39. int n, m;
  40.  
  41. void build_paths(double cur)
  42. {
  43.     for (int i = 0; i < n; i++)
  44.     {
  45.         dist[i] = inf;
  46.     }
  47.     dist[0] = 0;
  48.     set <pair <double, int> > q;
  49.     q.insert({0, 0});
  50.     while (!q.empty())
  51.     {
  52.         int v = q.begin()->second;
  53.         q.erase(q.begin());
  54.         for (auto x : g[v])
  55.         {
  56.             if (dist[x.to] > dist[x.v] + x.time * cur + x.cost * (1 - cur))
  57.             {
  58.                 q.erase({dist[x.to], x.v});
  59.                 sum_a[x.to] = sum_a[x.v] + x.time;
  60.                 sum_b[x.to] = sum_b[x.v] + x.cost;
  61.                 dist[x.to] = dist[x.v] + x.time * cur + x.cost * (1 - cur);
  62.                 q.insert({dist[x.to], x.to});
  63.             }
  64.         }
  65.     }
  66.     for (int i = 0; i < n; i++)
  67.     {
  68.         if (dist[i] <= inf / 2)
  69.         {
  70.             best[i] = min(best[i], sum_a[i] * (ll) sum_b[i]);
  71.         }
  72.     }
  73. }
  74.  
  75. int main()
  76. {
  77.     scanf("%d%d", &n, &m);
  78.     for (int i = 0; i < n; i++)
  79.     {
  80.         best[i] = 1e18;
  81.     }
  82.     vector <edge> e;
  83.     for (int i = 0; i < m; i++)
  84.     {
  85.         int u, v, time, cost;
  86.         scanf("%d%d%d%d", &u, &v, &time, &cost);
  87.         u--, v--;
  88.         e.push_back({u, v, time, cost});
  89.         e.push_back({v, u, time, cost});
  90.         g[u].push_back({u, v, time, cost});
  91.         g[v].push_back({v, u, time, cost});
  92.     }
  93.     double x = 0;
  94.     while (true)
  95.     {
  96.         double new_x = 1;
  97.         build_paths(x);
  98.         for (auto c : e)
  99.         {
  100.             int v = c.v, to = c.to, time = c.time, cost = c.cost;
  101.             if (best[v] == 1e18)
  102.             {
  103.                 continue;
  104.             }
  105.             int new_path_a = sum_a[v] + time;
  106.             int new_path_b = sum_b[v] + cost;
  107.             int cur_path_a = sum_a[to];
  108.             int cur_path_b = sum_b[to];
  109.             new_path_a -= new_path_b;
  110.             cur_path_a -= cur_path_b;
  111.             double pos = (cur_path_b - new_path_b) / (double) (new_path_a - cur_path_a);
  112.             if (x < pos && pos <= new_x)
  113.             {
  114.                 new_x = pos;
  115.             }
  116.         }
  117.         if (x == new_x)
  118.         {
  119.             break;
  120.         }
  121.         x = new_x;
  122.     }
  123.     for (int i = 1; i < n; i++)
  124.     {
  125.         if (best[i] == 1e18)
  126.         {
  127.             best[i] = -1;
  128.         }
  129.         printf("%lld\n", best[i]);
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment