Advertisement
Josif_tepe

Untitled

Jan 30th, 2022
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <fstream>
  5. #include <array>
  6. using namespace std;
  7.  
  8. struct node
  9. {
  10.     int idx;
  11.     int time;
  12.     int cost;
  13.  
  14.     node(){}
  15.     node(int _idx, int _time, int _cost)
  16.     {
  17.         idx=_idx;
  18.         time=_time;
  19.         cost=_cost;
  20.     }
  21.     bool operator<(const node &tmp) const
  22.     {
  23.         return time>tmp.time;
  24.     }
  25. };
  26. int d[3005][2005];
  27.  
  28. int main()
  29. {
  30. //    ifstream cin("in.txt");
  31.     int s,a,n;
  32.     cin>>s>>a>>n;
  33.     int x,y,t,m;
  34.     vector<array<int,3>>par[a+5];
  35.     for(int i=0;i<n;i++)
  36.     {
  37.         cin>>x>>y>>t>>m;
  38.         par[x].push_back({y,t,m});
  39.         par[y].push_back({x,t,m});
  40.     }
  41.     priority_queue<node>Q;
  42.     Q.push(node(1,0,0));
  43.     for(int i=0;i<=a;i++)
  44.     {
  45.         for(int j=0;j<=s;j++)
  46.         {
  47.             d[i][j]=2e9;
  48.         }
  49.     }
  50.     d[1][0] = 0;
  51.     while(!Q.empty())
  52.     {
  53.         node teme=Q.top();
  54.         Q.pop();
  55.         for(int i=0;i<par[teme.idx].size();i++)
  56.         {
  57.             int sosed=par[teme.idx][i][0];
  58.             int pat=par[teme.idx][i][1];
  59.             int cena=par[teme.idx][i][2];
  60.             if(cena + teme.cost <= s and pat + teme.time < d[sosed][cena + teme.cost])
  61.             {
  62.                 d[sosed][cena + teme.cost]=pat+teme.time;
  63.                 Q.push(node(sosed,pat + teme.time , cena + teme.cost));
  64.             }
  65.         }
  66.     }
  67.     int najmal=2e9;
  68.     for(int i=0;i<=s;i++)
  69.     {
  70.         najmal = min(najmal, d[a][i]);
  71.     }
  72.     if(najmal == 2e9) {
  73.         najmal = -1;
  74.     }
  75.     cout << najmal << endl;
  76.     return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement