Advertisement
K_Y_M_bl_C

Untitled

Feb 10th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #ifdef _DEBUG
  2. #define _GLIBCXX_DEBUG
  3. #endif
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <bits/stdc++.h>
  7.  
  8. using namespace __gnu_pbds;
  9. using namespace std;
  10.      
  11. typedef long long ll;
  12. typedef vector<ll> vll;
  13. typedef pair<int, int> pii;
  14. typedef vector<int> vi;
  15. typedef long double ld;
  16. typedef tree<pair<ll, int>, null_type, less<pair<ll, int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
  17.      
  18. #define mk make_pair
  19. #define inb push_back
  20. #define enb emplace_back
  21. #define X first
  22. #define Y second
  23. #define all(v) v.begin(), v.end()
  24. #define sqr(x) (x) * (x)
  25. #define TIME 1.0 * clock() / CLOCKS_PER_SEC
  26. #define y1 AYDARBOG
  27. //continue break pop_back return
  28.      
  29. int solve();
  30.      
  31. int main()
  32. {
  33.     //ios_base::sync_with_stdio(0);
  34.     //cin.tie(0);
  35. #define TASK "treedp"
  36. #ifndef _DEBUG
  37.     //freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  38. #endif
  39.     solve();
  40. #ifdef _DEBUG
  41.     fprintf(stderr, "\nTIME: %.3f\n", TIME);
  42. #endif
  43. }
  44.      
  45. const int INF = 2e9 + 7;
  46. const ll LINF = 1e18;
  47.    
  48. const int BUFSZ = (int)1e6 + 7;
  49. char buf[BUFSZ];
  50. string get_str()
  51. {
  52.     scanf(" %s", buf);
  53.     return string(buf);
  54. }
  55.  
  56. struct edge
  57. {
  58.     int a, b, cst;
  59.     edge() {};
  60.     edge(int a, int b, int cst) : a(a), b(b), cst(cst) {};
  61.     bool operator< (edge x) const
  62.     {
  63.         if (cst == x.cst)
  64.         {
  65.             if (a == x.a)
  66.             {
  67.                 return b < x.b;
  68.             }
  69.             return a < x.a;
  70.         }
  71.         return cst < x.cst;
  72.     }
  73.     void print()
  74.     {
  75.         printf("FROM: %d TO: %d T: %d\n", a + 1, b + 1, cst);
  76.     }
  77. };
  78.  
  79. int solve()
  80. {
  81.     int n, m, d;
  82.     scanf("%d %d %d", &n, &m, &d);
  83.     vector<set<edge>> G(n);
  84.     for (int i = 0; i < m; ++i)
  85.     {
  86.         edge e;
  87.         scanf("%d %d %d", &e.a, &e.b, &e.cst);
  88.         --e.a, --e.b;
  89.         G[e.a].insert(e);
  90.         swap(e.a, e.b);
  91.         G[e.a].insert(e);
  92.     }
  93.     int q;
  94.     scanf("%d", &q);
  95.     while (q--)
  96.     {
  97.         int s, f;
  98.         scanf("%d %d", &s, &f);
  99.         --s, --f;
  100.         if (s == f)
  101.         {
  102.             puts("0");
  103.             continue;
  104.         }
  105.         int ans = INF;
  106.         queue<edge> q;
  107.         queue<int> dd;
  108.         vector<set<edge>> g = G;
  109.         for (auto x : g[s])
  110.         {
  111.             q.push(x);
  112.             dd.push(1);
  113.         }
  114.         g[s].clear();
  115.         while (!q.empty())
  116.         {
  117.             edge cur = q.front();
  118.             int cdst = dd.front();
  119.             dd.pop();
  120.             //printf("CURDST: %d CUR: ", cdst);
  121.             //cur.print();
  122.             q.pop();
  123.             int u = cur.b;
  124.             if (u == f)
  125.             {
  126.                 ans = min(ans, cdst);
  127.             }
  128.             int t = cur.cst;
  129.             for (auto x : g[u])
  130.             {
  131.                 if (abs(x.cst - t) > d)
  132.                     continue;
  133.                 //x.print();
  134.                 q.push(x);
  135.                 dd.push(cdst + 1);
  136.             }
  137.             for (auto it = g[u].begin(); it != g[u].end();)
  138.             {
  139.                 if (abs((*it).cst - t) > d)
  140.                 {
  141.                     ++it;
  142.                 }
  143.                 else
  144.                 {
  145.                     auto it1 = it;
  146.                     ++it;
  147.                     g[u].erase(*it1);
  148.                 }
  149.             }
  150.         }
  151.         if (ans == INF)
  152.             puts("-1");
  153.         else
  154.             printf("%d\n", ans);
  155.     }
  156.     return 0;
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement