Advertisement
Josif_tepe

Untitled

Feb 8th, 2025
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <cstring>
  4. #include <vector>
  5. #include <algorithm>
  6. using namespace std;
  7. const int maxn = 1e5 + 10;
  8. const int INF = 2e9;
  9. vector<pair<pair<int, int>, int>> graph[maxn];
  10. struct node {
  11.     int idx, shortest_path, is_snow_only;
  12.     node() {}
  13.     node(int _idx, int _shortest_path, int _is_snow_only) {
  14.         idx = _idx;
  15.         shortest_path = _shortest_path;
  16.         is_snow_only = _is_snow_only;
  17.     }
  18.     bool operator < (const node & tmp) const {
  19.         return shortest_path > tmp.shortest_path;
  20.     }
  21. };
  22. bool visited[maxn][2];
  23. int main(){
  24.     int n, m;
  25.     cin >> n >> m;
  26.    
  27.     for(int i = 0; i < m; i++) {
  28.         int a, b, c, d;
  29.         cin >> a >> b >> c >> d;
  30.         a--; b--;
  31.         graph[a].push_back(make_pair(make_pair(b, c), d));
  32.         graph[b].push_back(make_pair(make_pair(a, c), d));
  33.     }
  34.    
  35.     memset(visited, false, sizeof visited);
  36.     priority_queue<node> pq;
  37.     pq.push(node(0, 0, 1));
  38.    
  39.     int res_shortest = 2e9, res_snow = -1;
  40.     while(!pq.empty()) {
  41.         node c = pq.top();
  42.         pq.pop();
  43.        
  44.         if(c.idx == n - 1) {
  45.             if(res_shortest > c.shortest_path) {
  46.                 res_shortest = c.shortest_path;
  47.                 res_snow = max(res_snow, c.is_snow_only);
  48.             }
  49.         }
  50.         if(visited[c.idx][c.is_snow_only]) continue;
  51.         visited[c.idx][c.is_snow_only] = true;
  52.        
  53.         for(int i = 0; i < (int) graph[c.idx].size(); i++) {
  54.             int neighbour = graph[c.idx][i].first.first;
  55.             int weight = graph[c.idx][i].first.second;
  56.             int snow = graph[c.idx][i].second;
  57.            
  58.             int next_snow = min(snow, c.is_snow_only);
  59.             if(!visited[neighbour][next_snow]) {
  60.                 pq.push(node(neighbour, c.shortest_path + weight, next_snow));
  61. //                cout << neighbour << endl;
  62.             }
  63.         }
  64.     }
  65.    
  66.     cout << res_shortest << endl;
  67.     if(res_snow == 1) {
  68.         cout << "DA" << endl;
  69.     }
  70.     else {
  71.         cout << "NE" << endl;
  72.     }
  73.     return 0;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement