CodeForces-229B.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #include <algorithm>
  2. #include <functional>
  3. #include <iostream>
  4. #include <limits>
  5. #include <queue>
  6. #include <utility>
  7. #include <vector>
  8. using namespace std;
  9. struct Edge { int to; long long weight; };
  10. int main() {
  11. ios::sync_with_stdio(false);
  12. cin.tie(nullptr);
  13. int n, m; cin >> n >> m;
  14. vector<vector<Edge>> g(n);
  15. while (m--) {
  16. int u, v; long long w; cin >> u >> v >> w; --u; --v;
  17. g[u].push_back({v, w}); g[v].push_back({u, w});
  18. }
  19. vector<vector<long long>> blocked(n);
  20. for (int i = 0; i < n; ++i) {
  21. int k; cin >> k; blocked[i].resize(k);
  22. for (auto &x : blocked[i]) cin >> x;
  23. }
  24. const long long inf = numeric_limits<long long>::max() / 4;
  25. vector<long long> dist(n, inf); dist[0] = 0;
  26. priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<>> pq;
  27. pq.push({0, 0});
  28. while (!pq.empty()) {
  29. auto [d, u] = pq.top(); pq.pop();
  30. if (d != dist[u]) continue;
  31. if (u == n - 1) { cout << d << '\n'; return 0; }
  32. long long depart = d;
  33. auto it = lower_bound(blocked[u].begin(), blocked[u].end(), depart);
  34. while (it != blocked[u].end() && *it == depart) ++depart, ++it;
  35. for (Edge e : g[u]) if (dist[e.to] > depart + e.weight) {
  36. dist[e.to] = depart + e.weight;
  37. pq.push({dist[e.to], e.to});
  38. }
  39. }
  40. cout << "-1\n";
  41. }