fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <limits>
  5.  
  6. const int INF = std::numeric_limits<int>::max();
  7.  
  8. struct Edge {
  9. int destination;
  10. int weight;
  11. };
  12.  
  13. // Hàm tìm tải trọng lớn nhất trên đường đi từ thành phố s đến thành phố t
  14. int findMaxLoad(const std::vector<std::vector<Edge>>& graph, int n, int s, int t) {
  15. std::vector<int> maxLoad(n + 1, 0);
  16. std::priority_queue<std::pair<int, int>> pq;
  17.  
  18. maxLoad[s] = INF;
  19. pq.push({INF, s});
  20.  
  21. while (!pq.empty()) {
  22. int load = pq.top().first;
  23. int currentCity = pq.top().second;
  24. pq.pop();
  25.  
  26. if (currentCity == t) {
  27. return load;
  28. }
  29.  
  30. if (load <= maxLoad[currentCity]) {
  31. for (const Edge& edge : graph[currentCity]) {
  32. int newLoad = std::min(load, edge.weight);
  33. if (newLoad > maxLoad[edge.destination]) {
  34. maxLoad[edge.destination] = newLoad;
  35. pq.push({newLoad, edge.destination});
  36. }
  37. }
  38. }
  39. }
  40.  
  41. return 0;
  42. }
  43.  
  44. int main() {
  45. int n, m, s, t;
  46. std::cin >> n >> m >> s >> t;
  47.  
  48. std::vector<std::vector<Edge>> graph(n + 1);
  49.  
  50. for (int i = 0; i < m; i++) {
  51. int x, y, z;
  52. std::cin >> x >> y >> z;
  53. graph[x].push_back({y, z});
  54. graph[y].push_back({x, z});
  55. }
  56.  
  57. int maxLoad = findMaxLoad(graph, n, s, t);
  58.  
  59. std::cout << maxLoad << std::endl;
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0.01s 5424KB
stdin
4 5 1 4
1 2 10
2 4 1
1 3 5
3 4 3
1 4 2
stdout
3