Dang_Quan_10_Tin

BAI2

Aug 27th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <queue>
  6. #define task "BAI2CDT18"
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 5e3 + 2;
  12. struct Edge
  13. {
  14.     int v;
  15.     ll w;
  16.     Edge() {}
  17.     Edge(int v, ll w)
  18.     {
  19.         this->v = v;
  20.         this->w = w;
  21.     }
  22. };
  23. vector<Edge> adj[N];
  24. const ll Inf = 1e18;
  25. int n, m;
  26. ll d[N], f[N];
  27.  
  28. void Read()
  29. {
  30.     cin >> n >> m;
  31.     for (int i = 1; i <= m; ++i)
  32.     {
  33.         int t, u, v;
  34.         ll l;
  35.         cin >> t >> u >> v >> l;
  36.         adj[u].push_back(Edge(v, l));
  37.         if (t == 2)
  38.             adj[v].push_back(Edge(u, l));
  39.     }
  40. }
  41.  
  42. void Init()
  43. {
  44.     fill_n(d, N, Inf);
  45.     d[1] = 0;
  46.     f[1] = 1;
  47. }
  48.  
  49. bool Relax(int par, int son, ll w)
  50. {
  51.     if (d[son] > d[par] + w)
  52.     {
  53.         d[son] = d[par] + w;
  54.         f[son] = f[par];
  55.         return true;
  56.     }
  57.     if (d[son] == d[par] + w)
  58.         f[son] += f[par];
  59.     return false;
  60. }
  61.  
  62. void Dijkstra()
  63. {
  64.     Init();
  65.     struct Tque
  66.     {
  67.         int v;
  68.         ll w;
  69.         Tque() {}
  70.         Tque(int v, ll w)
  71.         {
  72.             this->v = v;
  73.             this->w = w;
  74.         }
  75.         bool operator<(const Tque &a) const
  76.         {
  77.             return w > a.w;
  78.         }
  79.         bool Valid()
  80.         {
  81.             return d[v] == w;
  82.         }
  83.     };
  84.     priority_queue<Tque> s;
  85.     s.push(Tque(1, 0));
  86.     while (s.size())
  87.     {
  88.         Tque c = s.top();
  89.         s.pop();
  90.         if (!c.Valid())
  91.             continue;
  92.         for (auto i : adj[c.v])
  93.             if (Relax(c.v, i.v, i.w))
  94.                 s.push(Tque(i.v, d[i.v]));
  95.     }
  96. }
  97.  
  98. void Solve()
  99. {
  100.     Dijkstra();
  101.     cout << f[n] << " " << d[n];
  102. }
  103.  
  104. int32_t main()
  105. {
  106.     ios::sync_with_stdio(0);
  107.     cin.tie(0);
  108.     cout.tie(0);
  109.     if (fopen(task ".INP", "r"))
  110.         freopen(task ".INP", "r", stdin),
  111.             freopen(task ".OUT", "w", stdout);
  112.     Read();
  113.     Solve();
  114. }
Add Comment
Please, Sign In to add comment