Advertisement
Josif_tepe

Untitled

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