Advertisement
_rashed

UVA 12047

Feb 27th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7. ll mem[2][10000];
  8.  
  9. class edge {
  10.     public:
  11.     int from;
  12.     int to;
  13.     ll w;
  14.  
  15.     edge(int f, int t, ll wt) {
  16.         from = f;
  17.         to = t;
  18.         w = wt;
  19.     }
  20.  
  21.     bool operator < (const edge &e) const {
  22.         return w > e.w;
  23.     }
  24. };
  25.  
  26. vector<vector<edge>> graph1;
  27. vector<vector<edge>> graph2;
  28. ll n,m,s,d,p;
  29.  
  30.  
  31.  
  32. int main()
  33. {
  34.     ios_base::sync_with_stdio(false);
  35.     cin.tie(NULL);
  36.     cout.tie(NULL);
  37.     int t;
  38.     cin >> t;
  39.     while(t--) {
  40.         cin >> n >> m >> s >> d >> p;
  41.         s--;
  42.         d--;
  43.         graph1.clear();
  44.         graph1.resize(n);
  45.         graph2.clear();
  46.         graph2.resize(n);
  47.         memset(mem,-1,sizeof(mem));
  48.         priority_queue<edge> all;
  49.         for(int i = 0; i < m; i++) {
  50.             ll u,v,c;
  51.             cin >> u >> v >> c;
  52.             u--,v--;
  53.             graph1[u].push_back(edge(u,v,c));
  54.             graph2[v].push_back(edge(v,u,c));
  55.             all.push(edge(u,v,c));
  56.         }
  57.         priority_queue<edge> pq;
  58.         pq.push(edge(s,s,0));
  59.         while(!pq.empty()) {
  60.             edge curr = pq.top();
  61.             pq.pop();
  62.             if(mem[0][curr.to] == -1) {
  63.                 mem[0][curr.to] = curr.w;
  64.                 for(edge &e : graph1[curr.to]) {
  65.                     pq.push(edge(curr.to,e.to,curr.w + e.w));
  66.                 }
  67.             }
  68.         }
  69.         pq.push(edge(d,d,0));
  70.         while(!pq.empty()) {
  71.             edge curr = pq.top();
  72.             pq.pop();
  73.             if(mem[1][curr.to] == -1) {
  74.                 mem[1][curr.to] = curr.w;
  75.                 for(edge &e : graph2[curr.to]) {
  76.                     pq.push(edge(curr.to,e.to,curr.w + e.w));
  77.                 }
  78.             }
  79.         }
  80.         ll ans = -1;
  81.         while(!all.empty()) {
  82.             edge curr = all.top();
  83.             all.pop();
  84.             if(mem[0][curr.from] != -1 && mem[1][curr.to] != -1
  85.                && mem[0][curr.from] + mem[1][curr.to] + curr.w <= p)
  86.                 ans = curr.w;
  87.         }
  88.         cout << ans << "\n";
  89.     }
  90.     return 0;
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement