Advertisement
YEZAELP

CUBE-189: God of War

Jun 9th, 2021
701
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int INF = 1e9;
  5. const int N = 1e5;
  6. using pi = pair <int, int>;
  7. using pii = pair <int, pi>;
  8. vector <pi> g[N+10];
  9. int dis[N+10][2];
  10. bool vs[N+10][2];
  11.  
  12. int main(){
  13.  
  14.     int n, m;
  15.     scanf("%d%d", &n, &m);
  16.  
  17.     int s, t;
  18.     scanf("%d%d", &s, &t);
  19.  
  20.     int sum = 0;
  21.  
  22.     for(int i=1;i<=m;i++){
  23.         int u, v, w;
  24.         scanf("%d%d%d", &u, &v, &w);
  25.         g[u].push_back({v, w});
  26.         g[v].push_back({u, w});
  27.         sum += w;
  28.     }
  29.  
  30.     for(int i=0;i<=n;i++){
  31.         dis[i][0] = dis[i][1] = INF;
  32.         vs[i][0] = vs[i][1] = false;
  33.     }
  34.  
  35.     priority_queue <pii, vector <pii>, greater <pii>> pq;
  36.     dis[s][0] = 0;
  37.     pq.push({dis[s][0], {s, 0}});
  38.  
  39.     while(!pq.empty()){
  40.         int d = pq.top().first;
  41.         int u = pq.top().second.first;
  42.         int c = pq.top().second.second;
  43.         pq.pop();
  44.         if(vs[u][c]) continue;
  45.         // printf("u %d d %d c %d \n", u, d, c);
  46.         vs[u][c] = true;
  47.         if(u == t and c == 1) break;
  48.         for(auto vw: g[u]){
  49.             int v = vw.first;
  50.             int w = vw.second;
  51.             if(c == 0){
  52.                 if(!vs[v][0] and d + w < dis[v][0]){
  53.                     dis[v][0] = d + w;
  54.                     pq.push({dis[v][0], {v, 0}});
  55.                 }
  56.                 if(!vs[v][1] and d < dis[v][1]){
  57.                     dis[v][1] = d;
  58.                     pq.push({dis[v][1], {v, 1}});
  59.                 }
  60.             }
  61.             else if(c == 1 and !vs[v][1] and d + w < dis[v][1]){
  62.                 dis[v][1] = d + w;
  63.                 pq.push({dis[v][1], {v, 1}});
  64.             }
  65.         }
  66.     }
  67.  
  68.     // printf("%d %d\n", sum, dis[t][1]);
  69.  
  70.     printf("%d", sum - dis[t][1]);
  71.  
  72.     return 0;
  73. }
  74.  
  75. /*
  76.  
  77.  
  78. 4 4
  79. 0 4
  80. 0 1 1
  81. 1 2 1
  82. 2 3 1
  83. 0 4 1000
  84.  
  85. 1003
  86.  
  87. 7 10
  88. 4 5
  89. 0 1 2
  90. 0 2 3
  91. 0 3 1
  92. 4 0 10
  93. 3 4 8
  94. 2 1 5
  95. 1 6 2
  96. 5 2 2
  97. 5 6 4
  98. 2 3 7
  99.  
  100. 39
  101.  
  102. */
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement