Advertisement
Josif_tepe

Untitled

Sep 20th, 2021
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <queue>
  6. using namespace  std;
  7. struct node {
  8.     int index,shortest_path,snow;
  9.  
  10.     node(int i,int p,int s){
  11.         index = i;
  12.         shortest_path = p;
  13.         snow = s;
  14.     }
  15.  
  16.     bool operator < (const node &tmp)const{
  17.         return shortest_path > tmp.shortest_path;
  18.     }
  19. };
  20. struct broevi {
  21.     int b,c,d;
  22.  
  23.     broevi(int _b,int _c, int _d){
  24.         b = _b;
  25.         c = _c;
  26.         d = _d;
  27.     }
  28. };
  29. int main() {
  30.     int n, m;
  31.     cin >> n >> m;
  32.     vector<broevi> graph [n + 5];
  33.     for (int i = 0; i < m; i++) {
  34.         int a,b,c,d;
  35.         cin >> a >> b >> c >> d;
  36.         graph[a].push_back(broevi(b,c,d));
  37.         graph[b].push_back(broevi(a,c,d));
  38.     }
  39.    priority_queue<node> pq;
  40.    pq.push(node(1,0,1));
  41.  
  42.    vector<bool> visited (n+1, false);
  43.    vector<int> distance ( n+1,2e9);
  44.  
  45.    while (!pq.empty()){
  46.        node current = pq.top();
  47.        pq.pop();
  48.  
  49.        visited[current.index] = true;
  50.        if(current.index == n && current.snow == 1){
  51.            cout << current.shortest_path << endl;
  52.            cout << "DA" << endl;
  53.            break;
  54.        }
  55.        if(current.index == n && current.snow == 0){
  56.            cout << current.shortest_path << endl;
  57.            cout << "NE" << endl;
  58.            break;
  59.        }
  60.        for (int i = 0; i <graph[current.index].size(); i++) {
  61.            int sosed = graph[current.index][i].b;
  62.            int pat = graph[current.index][i].c;
  63.            int sneg = graph[current.index][i].d;
  64.  
  65.            if(!visited[sosed] && sneg * current.snow == 1 and current.shortest_path + pat < distance[sosed]) {
  66.                pq.push(node(sosed,current.shortest_path+pat,current.snow));
  67.                distance[sosed] = current.shortest_path + pat;
  68.            }
  69.            else if (!visited[sosed] ){
  70.                pq.push(node(sosed,current.shortest_path+pat,0));
  71.            }
  72.        }
  73.    }
  74.     return 0;
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement