Advertisement
Hezov

Joc Grigore Moisil 2014

Jun 22nd, 2025
391
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <array>
  4. #include <queue>
  5. #include <vector>
  6. using namespace std;
  7. ifstream cin("joc.in");
  8. ofstream cout("joc.out");
  9. typedef long long ll;
  10. const ll mxN = 5e4, INF = 1e16;
  11. priority_queue<array<ll,2>> pq;
  12. vector<int> adj[mxN+10], sol;
  13. bool visited[mxN+10];
  14. ll nivel[mxN+10], d[mxN+10], parent[mxN+10];
  15. int main()
  16. {
  17.     int cerinta;
  18.     cin >> cerinta;
  19.     int n , s, f;
  20.     cin >> n >> s >> f;
  21.     for(int i = 1;i<=n;i++)
  22.         cin >> nivel[i];
  23.     for(int from = 1;from <= n;from++)
  24.     {
  25.         int k; cin >> k;
  26.         for(int it = 1;it<=k;it++)
  27.         {
  28.             int to; cin >> to;
  29.             adj[from].push_back(to);
  30.         }
  31.     }
  32.     for(int i = 1;i<=n;i++)
  33.         d[i] = INF;
  34.     d[s] = 0;
  35.     pq.push({0,s});
  36.     while(!pq.empty())
  37.     {
  38.         int from = pq.top()[1];
  39.         pq.pop();
  40.         if(visited[from])
  41.             continue;
  42.         visited[from] = true;
  43.         for(auto it : adj[from])
  44.         {
  45.             int to = it;
  46.             int nivelA = nivel[from];
  47.             int nivelB = nivel[to];
  48.             if(nivelA < nivelB) swap(nivelA,nivelB);
  49.             ll cost = nivelA / nivelB;
  50.             if(d[from] + cost < d[to])
  51.             {
  52.                 parent[to] = from;
  53.                 d[to] = d[from] + cost;
  54.                 pq.push({-d[to],to});
  55.             }
  56.         }
  57.     }
  58.     if(cerinta == 1)
  59.         cout << d[f] << '\n';
  60.     else
  61.     {
  62.         for(int p = f;p!=0;p = parent[p])
  63.             sol.push_back(p);
  64.         reverse(sol.begin(),sol.end());
  65.         cout << sol.size() << '\n';
  66.         for(auto it : sol)
  67.             cout << it << ' ';
  68.  
  69.     }
  70.     return 0;
  71. }
  72.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement