fork download
  1. #include <algorithm>
  2. #include <array>
  3. #include <bitset>
  4. #include <cassert>
  5. #include <chrono>
  6. #include <climits>
  7. #include <cmath>
  8. #include <complex>
  9. #include <cstring>
  10. #include <unordered_set>
  11. #include <functional>
  12. #include <iomanip>
  13. #include <iostream>
  14. #include <map>
  15. #include <stack>
  16. #include <numeric>
  17. #include <queue>
  18. #include <random>
  19. #include <set>
  20. #include <vector>
  21.  
  22. using namespace std;
  23.  
  24. #pragma GCC diagnostic ignored "-Wunused-variable"
  25. #pragma GCC diagnostic ignored "-Wunused-parameter"
  26.  
  27. using ll = long long;
  28. using ull = unsigned long long;
  29. using ld = long double;
  30.  
  31. #define sz(x) ((int)(x).size())
  32. #define all(x) (x).begin(), (x).end()
  33. #define rall(x) (x).rbegin(), (x).rend()
  34. #define hashmap unordered_map
  35. #define hashset unordered_set
  36. #define pb push_back
  37. #define ff first
  38. #define ss second
  39. #define en "\n"
  40.  
  41. const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  42. const ll linf = 0x3f3f3f3f3f3f3f3f;
  43. const int inf = 0x3f3f3f3f;
  44.  
  45. void __print(int x) { cerr << x; }
  46. void __print(ll x) { cerr << x; }
  47. void __print(ull x) { cerr << x; }
  48. void __print(double x) { cerr << x; }
  49. void __print(ld x) { cerr << x; }
  50. void __print(char x) { cerr << '\'' << x << '\''; }
  51. void __print(const string &x) { cerr << '"' << x << '"'; }
  52. void __print(bool x) { cerr << (x ? "true" : "false"); }
  53.  
  54. #ifdef LOCAL
  55. #define debug(...) \
  56. { \
  57.   cerr << "[" << #__VA_ARGS__ << "] = ["; \
  58.   _print(__VA_ARGS__); \
  59. }
  60. #else
  61. #define debug(...)
  62. #endif
  63.  
  64. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  65.  
  66. ll rnd(ll l, ll r)
  67. {
  68. return uniform_int_distribution<ll>(l, r)(rng);
  69. }
  70.  
  71. template<typename T, typename V>
  72. void __print(const pair<T, V> &x);
  73.  
  74. template<typename T>
  75. void __print(const T &x);
  76.  
  77. template<typename T, size_t N>
  78. void __print(const array<T, N> &x);
  79.  
  80. template<typename... T>
  81. void __print(const tuple<T...> &x);
  82.  
  83. template<class T>
  84. bool maximize(T &a, const T &b)
  85. {
  86. if(a < b)
  87. {
  88. a = b;
  89.  
  90. return true;
  91. }
  92.  
  93. return false;
  94. }
  95.  
  96. template<class T>
  97. bool minimize(T &a, const T &b)
  98. {
  99. if(a > b)
  100. {
  101. a = b;
  102.  
  103. return true;
  104. }
  105.  
  106. return false;
  107. }
  108.  
  109. template<typename T, typename V>
  110. void __print(const pair<T, V> &x)
  111. {
  112. cerr << '{';
  113. __print(x.ff);
  114. cerr << ", ";
  115. __print(x.ss);
  116. cerr << '}';
  117. }
  118.  
  119. template<typename T>
  120. void __print(const T &x)
  121. {
  122. int f = 0;
  123. cerr << '{';
  124. for(auto &i : x) cerr << (f++ ? ", " : ""), __print(i);
  125. cerr << "}";
  126. }
  127.  
  128. template<typename T, size_t N>
  129. void __print(const array<T, N> &x)
  130. {
  131. cerr << '{';
  132. int f = 0;
  133. for(auto &i : x) cerr << (f++ ? ", " : ""), __print(i);
  134. cerr << '}';
  135. }
  136.  
  137. template<typename... T>
  138. void __print(const tuple<T...> &x)
  139. {
  140. cerr << '(';
  141. apply([&](const auto&... args)
  142. {
  143. int f = 0;
  144. ((cerr << (f++ ? ", " : ""), __print(args)), ...);
  145. }, x);
  146. cerr << ')';
  147. }
  148.  
  149. void _print()
  150. {
  151. cerr << "]" << en;
  152. }
  153.  
  154. template<typename T, typename... V>
  155. void _print(T t, V... v)
  156. {
  157. __print(t);
  158. if(sizeof...(v)) cerr << ", ";
  159. _print(v...);
  160. }
  161.  
  162. namespace brute_force
  163. {
  164. void progress()
  165. {
  166.  
  167. }
  168. }
  169.  
  170. struct Edge
  171. {
  172. int v;
  173. ll w;
  174. };
  175.  
  176. int n, m, q;
  177. vector<vector<Edge>> adj;
  178.  
  179. namespace accepted_solution
  180. {
  181. vector<vector<ll>> dijktra()
  182. {
  183. vector<vector<ll>> dist(n + 1, vector<ll>(n + 1, linf));
  184. for(int st = 1 ; st <= n ; st++)
  185. {
  186. dist[st][st] = 0;
  187. priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>> pq;
  188. pq.push({0, st});
  189.  
  190. while(!pq.empty())
  191. {
  192. ll d = pq.top().ff;
  193. int u = pq.top().ss;
  194. pq.pop();
  195.  
  196. if(d != dist[st][u]) continue;
  197. for(const Edge &e : adj[u])
  198. {
  199. if(minimize(dist[st][e.v], d + e.w)) pq.push({dist[st][e.v], e.v});
  200. }
  201. }
  202. }
  203.  
  204. return dist;
  205. }
  206.  
  207. void solve()
  208. {
  209. vector<vector<ll>> dist = dijktra();
  210.  
  211. while(q--)
  212. {
  213. int x, y;
  214. cin >> x >> y;
  215.  
  216. if(dist[x][y] >= linf >> 1) cout << -1 << en;
  217. else cout << dist[x][y] << en;
  218. }
  219. }
  220. }
  221.  
  222. int main()
  223. {
  224. ios_base::sync_with_stdio(0);
  225. cin.tie(0);
  226. cout.tie(0);
  227.  
  228. #define task "STAREDU"
  229. if(fopen(task ".inp", "r"))
  230. {
  231. freopen(task ".inp", "r", stdin);
  232. freopen(task ".out", "w", stdout);
  233. }
  234.  
  235. cin >> n >> m >> q;
  236. adj.resize(n + 1);
  237. for(int i = 0 ; i < m ; i++)
  238. {
  239. int u, v;
  240. ll w;
  241. cin >> u >> v >> w;
  242.  
  243. adj[u].pb({v, w});
  244. adj[v].pb({u, w});
  245. }
  246.  
  247. accepted_solution::solve();
  248.  
  249. return 0;
  250. }
  251.  
  252. /*
  253. Compile in terminal:
  254. g++ -std=c++17 -O2 -Wall -Wextra -Wshadow -Wconversion main.cpp -o main.exe
  255. main.exe
  256.  
  257. Debugging:
  258. g++ -std=c++17 -O0 -g -DLOCAL main.cpp -o main
  259. */
Success #stdin #stdout 0s 544KB
stdin
Standard input is empty
stdout
Standard output is empty