fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int INF = 1e9 + 5;
  5.  
  6. int t;
  7.  
  8. int main() {
  9. ios::sync_with_stdio(false);
  10. cin.tie(0);
  11.  
  12. cin >> t;
  13. while (t--) {
  14. int n, m;
  15. cin >> n >> m;
  16.  
  17. vector<int> b(n + 1);
  18. for (int i = 1; i <= n; ++i)
  19. cin >> b[i];
  20.  
  21. vector<vector<pair<int, int>>> adj(n + 1); // {to, weight}
  22. for (int i = 0; i < m; ++i) {
  23. int s, t, w;
  24. cin >> s >> t >> w;
  25. adj[s].emplace_back(t, w);
  26. }
  27.  
  28. auto canReach = [&](int maxBattery) {
  29. vector<bool> visited(n + 1, false);
  30. queue<int> q;
  31. q.push(1);
  32. visited[1] = true;
  33.  
  34. while (!q.empty()) {
  35. int u = q.front(); q.pop();
  36. for (auto [v, w] : adj[u]) {
  37. if (w <= maxBattery && !visited[v]) {
  38. visited[v] = true;
  39. q.push(v);
  40. }
  41. }
  42. }
  43. return visited[n];
  44. };
  45.  
  46. int low = 0, high = 1e9, ans = -1;
  47. while (low <= high) {
  48. int mid = (low + high) / 2;
  49. if (canReach(mid)) {
  50. ans = mid;
  51. high = mid - 1;
  52. } else {
  53. low = mid + 1;
  54. }
  55. }
  56.  
  57. cout << ans << '\n';
  58. }
  59.  
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 5328KB
stdin
4
3 3
2 0 0
1 2 1
2 3 1
1 3 2
5 6
2 2 5 0 1
1 2 2
1 3 1
1 4 3
3 5 5
2 4 4
4 5 3
2 0
1 1
4 4
3 10 0 0
1 2 1
1 3 3
2 3 10
3 4 5
stdout
-1
-1
-1
-1