Naxocist

metro

Mar 23rd, 2024
759
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void dbg_out() { cerr << endl; }
  5. template<typename Head, typename... Tail>
  6. void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
  7. #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
  8.  
  9. template<typename S, typename T> S amax(S &a, const T &b) { if(b > a) a = b; return a; }
  10. template<typename S, typename T> S amin(S &a, const T &b) { if(b < a) a = b; return a; }
  11.  
  12. #define all(x) x.begin(), x.end()
  13. #define allrev(x) x.rbegin(), x.rend()
  14. #define pb emplace_back
  15. #define sz(x) (int) (x).size()
  16. #define ln '\n'
  17. using ll = long long;
  18. using pi = pair<ll, ll>;
  19. using T = tuple<ll, ll, ll>;
  20. const ll INF = 2e9;
  21.  
  22. void runcase() {
  23.     int n, m; cin >> n >> m;
  24.  
  25.     vector<int> cost(m);
  26.     unordered_map<int, vector<int>> g, revg;
  27.     for(int q=0; q<m; ++q){
  28.         int k, x; cin >> k >> x;
  29.         cost[q] = x;
  30.         vector<int> t;
  31.         while(k--) {
  32.             int u; cin >> u;
  33.             g[q].pb(u);
  34.             revg[u].pb(q);
  35.         }
  36.     }
  37.  
  38.     vector<int> dist(n, INF);
  39.     vector<bool> vis(m);
  40.     priority_queue<pi> pq;
  41.     pq.emplace(0, 0);
  42.     dist[0] = 0;
  43.     while(pq.size()) {
  44.         int d, u; tie(d, u) = pq.top(); pq.pop();
  45.         if(d > dist[u]) continue ;
  46.  
  47.         for(int G : revg[u]) {
  48.             if(vis[G]) continue ;
  49.             vis[G] = 1;
  50.             for(int v : g[G]) {
  51.                 if(dist[u] + cost[G] < dist[v]) {
  52.                     dist[v] = dist[u] + cost[G];
  53.                     pq.emplace(-dist[v], v);
  54.                 }
  55.             }
  56.         }
  57.  
  58.     }
  59.  
  60.     if(dist[n-1] == INF) cout << -1;
  61.     else cout << dist[n-1];
  62.     return ;
  63. }
  64.  
  65. int32_t main() {
  66.     cin.tie(nullptr)->sync_with_stdio(0);
  67.     int TC = 1;
  68.     // cin >> TC;
  69.     while(TC--) runcase();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment