DarkArtheme

Дейкстра

Mar 4th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.19 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6.  
  7. #define endl "\n"
  8. #define ff first
  9. #define ss second
  10. #define int ll
  11. #define fori(n) for (int i = 0; i < (n); i++)
  12. #define forj(n) for (int j = 0; j < (n); j++)
  13.  
  14. //#pragma comment(linker, "/STACK:20000000000")
  15. //#pragma GCC optimize("O3")
  16. //#pragma GCC optimize("Ofast")
  17. //#pragma GCC optimize("unroll-loops")
  18.  
  19. inline void boostIO() {
  20.     ios_base::sync_with_stdio(false);
  21.     cin.tie(0);
  22.     cout.tie(0);
  23.     cout.precision(10);
  24. }
  25.  
  26. inline int getint() {
  27.     int val = 0;
  28.     char c;
  29.     while ((c = getchar()) && !(c >= '0' && c <= '9'));
  30.     do {
  31.         val = (val * 10) + c - '0';
  32.     } while ((c = getchar()) && (c >= '0' && c <= '9'));
  33.     return val;
  34. }
  35. inline int safe_mul(int x, int y, int mod) {
  36.     return x * 1LL * y % mod;
  37. }
  38.  
  39. inline void safe_add(int& x, int y, int mod) {
  40.     x += y;
  41.     if (x >= mod)
  42.         x -= mod;
  43. }
  44.  
  45. const int INF = 1e17;
  46. const int modulo = 1e9 + 7;
  47. const double EPS = 1e-7;
  48. const double pi = 3.14159265358979323846;
  49.  
  50. const int maxn = 2e5 + 500;
  51.  
  52. vector<vector<int>> G;
  53. vector<int> W[maxn];
  54. vector<int> used;
  55. vector<int> pr;
  56. int flag = 0;
  57.  
  58. int Len[maxn]{INF};
  59. set<pair<int,int>> Set;
  60.  
  61. void dijcstra(int n, int st){
  62.     Len[st] = 0;
  63.     Set.insert(make_pair(0, st));
  64.     vector<int> prev(n, -1);
  65.     while (!Set.empty()) {
  66.         pair<int, int> p = *Set.begin();
  67.         int v = p.second;
  68.         Set.erase(Set.begin());
  69.         for (int i = 0; i < G[v].size(); ++i) {
  70.             int to = G[v][i];
  71.             if (Len[v] + W[v][i] < Len[to]) {
  72.                 if (Len[to] != INF) {
  73.                     auto it = Set.find(make_pair(Len[to], to));
  74.                     Set.erase(it);
  75.                 }
  76.                 prev[to] = v;
  77.                 Len[to] = Len[v] + W[v][i];
  78.                 Set.insert(make_pair(Len[to], to));
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. int32_t main() {
  85.     int n;
  86.     cin >> n;
  87.     G.assign(n, vector<int>());
  88.     used.assign(n, 0);
  89.     pr.assign(n, -1);
  90.     fori(n - 1){
  91.         int u, v;
  92.         cin >> u >> v;
  93.         --u; --v;
  94.         G[u].push_back(v);
  95.     }
  96.    
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment