erek1e

CF102569D

Sep 4th, 2024
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <queue>
  5. #include <stack>
  6.  
  7. using namespace std;
  8.  
  9. vector< vector< pair<int, char> > > neighbours;
  10. vector<int> distToN, previous;
  11. vector<char> previousChar;
  12.  
  13. int main() {
  14.     int n, m; cin >> n >> m;
  15.     neighbours.resize(1+n);
  16.     for (int i = 0; i < m; ++i) {
  17.         int u, v; char c;
  18.         cin >> u >> v >> c;
  19.         neighbours[u].push_back({v,c});
  20.         neighbours[v].push_back({u,c});
  21.     }
  22.  
  23.     // bfs
  24.     distToN.resize(1+n);
  25.     distToN[n] = 0;
  26.     queue<int> q;
  27.     q.push(n);
  28.     while (!q.empty()) {
  29.         for (auto x : neighbours[q.front()]) {
  30.             if (!distToN[x.first] && x.first != n) {
  31.                 q.push(x.first);
  32.                 distToN[x.first] = distToN[q.front()] + 1;
  33.             }
  34.         }
  35.         q.pop();
  36.     }
  37.  
  38.     // bfs
  39.     previous.resize(1+n), previousChar.resize(1+n);
  40.     previous[1] = -1;
  41.     vector<int> currentNodes(1, 1);
  42.     vector<bool> hadNode(1+n); // has been placed in currentNodes before
  43.     hadNode[1] = true;
  44.     for (int d = distToN[1]; d > 0; --d) {
  45.         char minChar = 'z';
  46.         for (int node : currentNodes) {
  47.             for (auto edge : neighbours[node]) {
  48.                 if (distToN[edge.first] >= distToN[node]) continue;
  49.                 if (edge.second < minChar) minChar = edge.second;
  50.             }
  51.         }
  52.  
  53.         vector<int> currentNodes2;
  54.         for (int node : currentNodes) {
  55.             for (auto edge : neighbours[node]) {
  56.                 if (distToN[edge.first] >= distToN[node]) continue;
  57.                 if (edge.second == minChar && !hadNode[edge.first]) { // do not add same node in twice
  58.                     previous[edge.first] = node;
  59.                     previousChar[edge.first] = minChar;
  60.                     currentNodes2.push_back(edge.first);
  61.                     hadNode[edge.first] = true;
  62.                 }
  63.             }
  64.         }
  65.         currentNodes = currentNodes2;
  66.     }
  67.  
  68.     stack<char> s;
  69.     stack<int> t;
  70.     cout << distToN[1] << endl;
  71.     for (int node = n; node != -1; node = previous[node]) {
  72.         t.push(node);
  73.         if (previous[node] != -1) s.push(previousChar[node]);
  74.     }
  75.  
  76.     while (!t.empty()) {
  77.         cout << t.top() << ' ';
  78.         t.pop();
  79.     }
  80.     cout << endl;
  81.     while (!s.empty()) {
  82.         cout << s.top();
  83.         s.pop();
  84.     }
  85.     cout << endl;
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment