Advertisement
YEZAELP

CUBE-204: Lazanta

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