Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <queue>
  4. #include <set>
  5. #include <map>
  6. #include <vector>
  7. #include <string>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <memory.h>
  11. #include <utility>
  12. #include <cmath>
  13. #include <cassert>
  14. using namespace std;
  15.  
  16. #define forn( i,n ) for ( int i=0; i<(int)(n); i++ )
  17. #define pb push_back
  18. #define mp make_pair
  19. #define sz(a) (int)((a).size())
  20. typedef long long ll;
  21. typedef long double ld;
  22. typedef pair<int,int> pii;
  23.  
  24. const int maxn = 100005;
  25. const int inf = 1000005000;
  26.  
  27. priority_queue<pii, vector<pii>, greater<pii> > q;
  28. int n, m, s, t, k;
  29. vector<pii> g[maxn];
  30. int c[2*maxn], p[2*maxn];
  31. int d[maxn];
  32.  
  33.  
  34. int path(int s, int t, int w)
  35. {
  36.   forn (i, n) d[i] = inf;
  37.   while (!q.empty()) q.pop();    
  38.  
  39. //    printf("%d \n", sz(q));
  40.  
  41.   q.push(mp(d[s] = 0, s));
  42.  
  43.  
  44.   while (!q.empty())
  45.   {
  46.     int dd = q.top().first, x = q.top().second;
  47.     q.pop();
  48.  
  49.     if (dd > d[x]) continue;
  50.     if (x == t) return d[x];
  51.  
  52.     forn (i, sz(g[x]))
  53.     {
  54.       int y = g[x][i].first, e = g[x][i].second;
  55.       int cost = c[e] + p[e] * w;
  56. //      printf("%d -> %d : %d\n", x, y, cost);
  57.       if (d[y] > d[x] + cost)
  58.         q.push(mp(d[y] = d[x]+cost, y));
  59.     }                                  
  60.  
  61.   }
  62.  
  63.   return inf;
  64. }
  65.  
  66.  
  67.  
  68. int solve(int w)
  69. {
  70.   int res = path(s, t, w) + path(t, s, w);
  71. //  printf("%d = %d\n", w, res);
  72.   return res;
  73. }
  74. int main()
  75. {
  76. //  freopen("a.in", "r", stdin);
  77.  
  78.   scanf("%d %d %d %d %d", &n, &m, &s, &t, &k);
  79.   --s, --t;
  80. //  printf("%d %d\n", s, t);
  81.  
  82.  
  83.   forn (i, m)
  84.   {
  85.     int x, y, c1, p1, c2, p2;
  86.     scanf("%d %d %d %d %d %d", &x, &y, &c1, &p1, &c2, &p2);
  87.     --x, --y;
  88.  
  89.     c[2*i] = c1,   p[2*i] = p1;
  90.     c[2*i+1] = c2, p[2*i+1] = p2;
  91.     g[x].pb(mp(y, 2*i));
  92.     g[y].pb(mp(x, 2*i+1));
  93.   }
  94.  
  95. ///  puts("!!");
  96.  
  97.   int l = 0, r = k;
  98.  
  99.   while (r-l > 15)
  100.   {
  101.     int m1 = (2*l+r) / 3;
  102.     int m2 = (l+2*r) / 3;
  103.  
  104.     int h1 = solve(m1);
  105.     int h2 = solve(m2);
  106.  
  107.     if (h1 < h2) r = m2;
  108.     else l = m1;
  109.   }
  110.  
  111.   int res = inf;
  112.  
  113.   for (int i=l; i<=r; ++i)
  114.     res = min(res, solve(i));
  115.  
  116.   printf("%d\n", res);
  117.  
  118.  
  119.  
  120.   return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement