Dang_Quan_10_Tin

DELTA

Jun 4th, 2022
1,311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #define task "DELTA"
  2. #include <iostream>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. using ll = long long;
  11. using ld = long double;
  12.  
  13. constexpr int N = 5e2 + 5;
  14. constexpr ll Inf = 1e17;
  15. constexpr int mod[2] = {1000000007, 998244353};
  16.  
  17. int n, m;
  18.  
  19. vector<pair<int, int>> adj[N];
  20. struct Edge
  21. {
  22.     int u, v, w;
  23. } e[N * N];
  24.  
  25. int ans[N * N];
  26. int mark, check[N], l;
  27.  
  28. vector<int> candidate;
  29. int in[N], out[N];
  30.  
  31. ll d[N][N];
  32. int cnt[N][N][2];
  33.  
  34. void Read()
  35. {
  36.     cin >> n >> m;
  37.  
  38.     for (int i = 1; i <= m; ++i)
  39.     {
  40.         cin >> e[i].u >> e[i].v >> e[i].w;
  41.         adj[e[i].u].emplace_back(e[i].v, i);
  42.         adj[e[i].v].emplace_back(e[i].u, i);
  43.     }
  44. }
  45.  
  46. inline void Add(int &x, const int &y, const int &mod)
  47. {
  48.     x += y;
  49.     if (x >= mod)
  50.         x -= mod;
  51. }
  52.  
  53. void Dijkstra(int x, ll d[N])
  54. {
  55.     ++mark;
  56.     fill_n(d, N, Inf);
  57.     d[x] = 0;
  58.     cnt[x][x][0] = cnt[x][x][1] = 1;
  59.  
  60.     for (int i = 1; i <= n; ++i)
  61.     {
  62.         pair<ll, int> ans = {Inf, 0};
  63.  
  64.         for (int j = 1; j <= n; ++j)
  65.             if (check[j] != mark && d[j] < ans.first)
  66.                 ans = {d[j], j};
  67.  
  68.         if (ans.first == Inf)
  69.             break;
  70.  
  71.         check[ans.second] = mark;
  72.  
  73.         for (auto i : adj[ans.second])
  74.             if (d[i.first] > ans.first + e[i.second].w)
  75.             {
  76.                 d[i.first] = ans.first + e[i.second].w;
  77.                 cnt[x][i.first][0] = cnt[x][ans.second][0];
  78.                 cnt[x][i.first][1] = cnt[x][ans.second][1];
  79.             }
  80.             else if (d[i.first] == ans.first + e[i.second].w)
  81.             {
  82.                 Add(cnt[x][i.first][0], cnt[x][ans.second][0], mod[0]);
  83.                 Add(cnt[x][i.first][1], cnt[x][ans.second][1], mod[1]);
  84.             }
  85.     }
  86. }
  87.  
  88. void dfs(int v, ll d[N])
  89. {
  90.     in[v] = ++l;
  91.  
  92.     for (auto i : adj[v])
  93.         if (d[i.first] == d[v] + e[i.second].w && !in[i.first])
  94.         {
  95.             candidate.emplace_back(i.second);
  96.             dfs(i.first, d);
  97.         }
  98.  
  99.     out[v] = l;
  100. }
  101.  
  102. void Solve()
  103. {
  104.     for (int i = 1; i <= n; ++i)
  105.         Dijkstra(i, d[i]);
  106.  
  107.     for (int i = 1; i <= n; ++i)
  108.     {
  109.         candidate.clear();
  110.  
  111.         for (int j = 1; j <= n; ++j)
  112.             in[j] = out[j] = 0;
  113.  
  114.         l = 0;
  115.         dfs(i, d[i]);
  116.  
  117.         for (auto j : candidate)
  118.         {
  119.             int v = in[e[j].u] > in[e[j].v] ? e[j].u : e[j].v;
  120.             int u = e[j].u + e[j].v - v;
  121.  
  122.             for (int t = 1; t <= n; ++t)
  123.                 if (in[v] <= in[t] && in[t] <= out[v] && 1ll * cnt[i][u][0] * cnt[v][t][0] % mod[0] == cnt[i][t][0] && 1ll * cnt[i][u][1] * cnt[v][t][1] % mod[1] == cnt[i][t][1])
  124.                     ++ans[j];
  125.         }
  126.     }
  127.  
  128.     for (int i = 1; i <= m; ++i)
  129.         cout << ans[i] / 2 << "\n";
  130. }
  131.  
  132. int32_t main()
  133. {
  134.     ios_base::sync_with_stdio(0);
  135.     cin.tie(0);
  136.     cout.tie(0);
  137.  
  138.     if (fopen(task ".INP", "r"))
  139.     {
  140.         freopen(task ".INP", "r", stdin);
  141.         freopen(task ".OUT", "w", stdout);
  142.     }
  143.  
  144.     Read();
  145.     Solve();
  146. }
Advertisement
Add Comment
Please, Sign In to add comment