D_L3

SDA - hw11 - task3

Dec 16th, 2023 (edited)
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <unordered_map>
  7. #include <set>
  8. #include <queue>
  9. #include <climits>
  10.  
  11.  
  12. using namespace std;
  13. int n, m, k; //crossroads, tunnels
  14. int finalIdx;
  15.  
  16. struct Tunnel {
  17.     int from;
  18.     int to;
  19.     int kg;
  20.     int time;
  21.     Tunnel(int from, int to, int kg, int time)
  22.         : from(from), to(to), kg(kg), time(time) {}
  23. };
  24.  
  25. unordered_map<int, vector<Tunnel>> graph;
  26. //from to kg time
  27.  
  28. bool isPossible(int kg) {
  29.     priority_queue<pair<int, int>> queue; //time, idx
  30.     unordered_map<int, int> times; //idx, time
  31.     times[0] = 0;
  32.     queue.push({ 0, 0 });
  33.     vector<bool> visited(n, false);
  34.  
  35.     while (!queue.empty())
  36.     {
  37.         auto curr = queue.top();
  38.         int timeSoFar = curr.first;
  39.         int currIdx = curr.second;
  40.         visited[currIdx] = true;
  41.  
  42.         auto neighbours = graph[currIdx];
  43.         for (auto neighbour : neighbours)
  44.         {
  45.             int time = timeSoFar + neighbour.time;
  46.             if (neighbour.kg > kg || time > k || visited[neighbour.to])
  47.             {
  48.                 continue;
  49.             }
  50.  
  51.             if (neighbour.to == finalIdx)
  52.             {
  53.                 return true;
  54.             }
  55.  
  56.             if (times.count(neighbour.to) == 0 || times[neighbour.to] > time)
  57.             {
  58.                 queue.push({ time, neighbour.to });
  59.                 times[neighbour.to] = time;
  60.             }
  61.         }
  62.  
  63.         while (!queue.empty() && visited[queue.top().second])
  64.         {
  65.             queue.pop();
  66.         }
  67.     }
  68.     return false;
  69. }
  70.  
  71. int binarySearch(int min, int max) {
  72.     int result = -1;
  73.     while (min <= max)
  74.     {
  75.         int mid = min + (max - min) / 2;
  76.  
  77.         if (isPossible(mid)) {
  78.             result = mid;
  79.             max = mid - 1;
  80.         }
  81.         else {
  82.             min = mid + 1;
  83.         }
  84.     }
  85.     return result;
  86. }
  87.  
  88. int main() {
  89.     cin >> n >> m >> k;
  90.     int maxKg = 0;
  91.     finalIdx = n - 1;
  92.     int u, v, c, t;
  93.  
  94.     for (size_t i = 0; i < m; i++)
  95.     {
  96.         cin >> u >> v >> c >> t;
  97.         graph[u - 1].push_back({ u - 1, v - 1, c, t });
  98.         if (c > maxKg)
  99.         {
  100.             maxKg = c;
  101.         }
  102.     }
  103.  
  104.     cout << binarySearch(0, maxKg);
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment