Dang_Quan_10_Tin

Untitled

Jun 30th, 2020
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <unordered_set>
  5. #define task "SMST"
  6. using namespace std;
  7. using ll = long long;
  8. using ld = long double;
  9.  
  10. const int N = 2e2 + 5;
  11. const ll Inf = 1e18 + 7;
  12. int n, m;
  13. ll d[N][N], ans;
  14.  
  15. unordered_set<int> adj[N];
  16. int t = 0;
  17.  
  18. void Read(){
  19.     cin >> n >> m;
  20. }
  21.  
  22. void Setup(){
  23.     for(int i = 2; i <= n; ++i){
  24.         adj[i].insert(i - 1);
  25.         adj[i - 1].insert(i);
  26.     }
  27.     for(int i = 1; i <= n; ++i)
  28.         for(int j = i + 1; j <= n; ++j)
  29.             d[i][j] = d[j][i] = Inf;
  30. }
  31.  
  32. struct Tque{
  33.     int u, v;
  34.     int now;
  35.     ll w;
  36.     Tque() {}
  37.     Tque(int t, int i, int j, ll y){
  38.         u = i;
  39.         v = j;
  40.         now = t;
  41.         w = y;
  42.     }
  43.     bool operator < (const Tque &a) const{
  44.         return w < a.w;
  45.     }
  46. };
  47.  
  48. void BFS(int x, int y, ll w){
  49.     bool ck[N];
  50.     fill_n(ck, N, 0);
  51.     queue<Tque> s;
  52.     s.push(Tque(x, x, x, -Inf));
  53.     Tque res;
  54.     while(s.size()){
  55.         Tque c = s.front();
  56.         s.pop();
  57.         if(ck[c.now])
  58.             continue;
  59.         ck[c.now] = 1;
  60.         if(c.now == y){
  61.             res = c;
  62.             if(w < res.w){
  63.                 adj[res.u].erase(res.v);
  64.                 adj[res.v].erase(res.u);
  65.                 adj[x].insert(y);
  66.                 adj[y].insert(x);
  67.             }
  68.             break;
  69.         }
  70.         for(auto i : adj[c.now]){
  71.             if(d[i][c.now] > c.w)
  72.                 s.push(Tque(i, i, c.now, d[i][c.now]));
  73.             else
  74.                 s.push(Tque(i, c.u, c.v, c.w));
  75.         }
  76.     }
  77. }
  78.  
  79. bool ck[N];
  80.  
  81. ll Cal(int i){
  82.     ck[i] = 1;
  83.     ll ans = 0;
  84.     for(auto j : adj[i]){
  85.         if(ck[j])
  86.             continue;
  87.         ll res = Cal(j);
  88.         ans = ans == Inf || d[i][j] == Inf || res == Inf ? Inf : d[i][j] + res + ans;
  89.         if(ans == Inf)
  90.             return Inf;
  91.     }
  92.     return ans;
  93. }
  94.  
  95. void Solve(){
  96.  
  97.     while(m--){
  98.         int u, v;
  99.         ll w;
  100.         cin >> u >> v >> w;
  101.         if(d[u][v] <= w)
  102.             goto done;
  103.         d[u][v] = d[v][u] = w;
  104.         if(adj[u].find(v) != adj[u].end())
  105.             goto done;
  106.         BFS(u, v, w);
  107.         done:;
  108.         fill_n(ck, N, 0);
  109.         ll ans = Cal(1);
  110.         if(ans == Inf)
  111.             cout << 123456789 << "\n";
  112.         else
  113.             cout << ans << "\n";
  114.     }
  115. }
  116.  
  117. main(){
  118.     ios_base::sync_with_stdio(0);
  119.     cin.tie(0);
  120.     cout.tie(0);
  121.     if(fopen(task".INP", "r")){
  122.         freopen(task".INP", "r", stdin);
  123.         freopen(task".OUT", "w", stdout);
  124.     }
  125.     Read();
  126.     Setup();
  127.     Solve();
  128. }
Advertisement
Add Comment
Please, Sign In to add comment