Dang_Quan_10_Tin

MEETING POINT

Aug 27th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cstdio>
  5. #include <algorithm>
  6. #define task "MEETINGPOINT"
  7. using namespace std;
  8. using ll = long long;
  9.  
  10. const int N = 1e5 + 2;
  11. ll d[3][N];
  12. struct Edge
  13. {
  14.     int v;
  15.     ll w;
  16.     Edge(int u, ll r)
  17.     {
  18.         v = u;
  19.         w = r;
  20.     }
  21. };
  22. vector<Edge> adj[N];
  23. const ll Inf = 1e18 + 7;
  24. int n, m, k, id[N];
  25. ll minn[N], a, b;
  26.  
  27. void Read()
  28. {
  29.     cin >> n >> m >> k;
  30.     while (m--)
  31.     {
  32.         int u, v;
  33.         ll w;
  34.         cin >> u >> v >> w;
  35.         adj[u].push_back(Edge(v, w));
  36.         adj[v].push_back(Edge(u, w));
  37.     }
  38. }
  39.  
  40. void Init(ll d[], int x)
  41. {
  42.     fill_n(d, N, Inf);
  43.     d[x] = 0;
  44. }
  45.  
  46. bool Relax(ll d[], int par, int son, ll w)
  47. {
  48.     if (d[son] > d[par] + w)
  49.     {
  50.         d[son] = d[par] + w;
  51.         return true;
  52.     }
  53.     return false;
  54. }
  55.  
  56. void Dijkstra(ll d[], int x)
  57. {
  58.     Init(d, x);
  59.     struct Tque
  60.     {
  61.         int v;
  62.         ll w;
  63.         Tque() {}
  64.         Tque(int m, ll n)
  65.         {
  66.             v = m;
  67.             w = n;
  68.         }
  69.         bool operator<(const Tque &a) const
  70.         {
  71.             return w > a.w;
  72.         }
  73.         bool Valid(ll d[])
  74.         {
  75.             return d[v] == w;
  76.         }
  77.     };
  78.     vector<bool> ck(N);
  79.     priority_queue<Tque> s;
  80.     s.push(Tque(x, 0));
  81.     while (s.size())
  82.     {
  83.         //cin >> a >> b;
  84.         Tque c = s.top();
  85.         s.pop();
  86.         if (!c.Valid(d) || ck[c.v])
  87.             continue;
  88.         ck[c.v] = 1;
  89.         for (Edge i : adj[c.v])
  90.             if (Relax(d, c.v, i.v, i.w))
  91.                 s.push(Tque(i.v, d[i.v]));
  92.     }
  93. }
  94.  
  95. bool Check(ll v)
  96. {
  97.     int l = 1, m, h = n;
  98.     while (l <= h)
  99.     {
  100.         m = (l + h) / 2;
  101.         if (d[1][id[m]] * a <= v)
  102.             l = m + 1;
  103.         else
  104.             h = m - 1;
  105.     }
  106.     return h > 0 && minn[h] * b <= v;
  107. }
  108.  
  109. void Solve()
  110. {
  111.     Dijkstra(d[1], 1);
  112.     Dijkstra(d[2], n);
  113.     for (int i = 1; i <= n; ++i)
  114.         id[i] = i;
  115.     sort(id + 1, id + n + 1, [&](int &x, int &y) { return d[1][x] < d[1][y]; });
  116.     minn[0] = Inf;
  117.     for (int i = 1; i <= n; ++i)
  118.         minn[i] = min(minn[i - 1], d[2][id[i]]);
  119.     while (k--)
  120.     {
  121.         cin >> a >> b;
  122.         ll l = 0, m, h = Inf;
  123.         while (l <= h)
  124.         {
  125.             //cerr << m << "\n";
  126.             m = (l + h) / 2;
  127.             if (Check(m))
  128.                 h = m - 1;
  129.             else
  130.                 l = m + 1;
  131.         }
  132.         cout << l << "\n";
  133.     }
  134. }
  135.  
  136. main()
  137. {
  138.     ios::sync_with_stdio(0);
  139.     cin.tie(0);
  140.     cout.tie(0);
  141.     if (fopen(task ".INP", "r"))
  142.         freopen(task ".INP", "r", stdin),
  143.             freopen(task ".OUT", "w", stdout);
  144.     Read();
  145.     Solve();
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment