Advertisement
Oscar2019

Minha resposta

Sep 1st, 2020 (edited)
1,044
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #ifndef ONLINE_JUDGE
  6.     #define W(x, y) cerr << "\033[31m" << #x << " = " << x << "\033[0m" << y;
  7. #else
  8.     #define W(x, y)
  9. #endif
  10.  
  11. using ll = long long;
  12. using pii = pair<int, int>;
  13. using pll = pair<ll, ll>;
  14. using vi = vector<int>;
  15. using vii = vector<pii>;
  16. using vl = vector<ll>;
  17. using vll = vector<pll>;
  18. using ld = long double;
  19. #define ff first
  20. #define ss second
  21. // const ld pi = acosl(-1);
  22. const ll prime = 1000000000 + 7;
  23. const ll INF = 1000000000;
  24.  
  25. int main(){
  26.     ios::sync_with_stdio(false);
  27.     cin.tie(0); cout.tie(0);
  28.  
  29.     ll n, m;
  30.     cin >> n >> m;
  31.     vector<vll> listAdj(n+1);
  32.     vector<map<ll, ll>> vet(n+1);
  33.     for(int i = 0; i < m; ++i){
  34.         ll a, b, c;
  35.         cin >> a >> b >> c;
  36.         listAdj[a].push_back({c, b});
  37.         listAdj[b].push_back({c, a});
  38.     }
  39.     for(int i = 1; i <= n; ++i){
  40.         ll k;
  41.         cin >> k;
  42.         vl vet3(k);
  43.         for(int j = 0; j < k; ++j){
  44.             cin >> vet3[j];
  45.         }
  46.         sort(vet3.begin(), vet3.end());
  47.         ll aux = 0;
  48.         for(int j = 0; j < k; ++j){
  49.             if(aux < vet3[j]){
  50.                 vet[i][aux] = vet3[j]-1;
  51.                 aux = vet3[j] + 1;
  52.             } else{
  53.                 aux = vet3[j] + 1;
  54.             }      
  55.         }
  56.         vet[i][aux] = 0x3f3f3f3f3f3f3f3fLL;
  57.         // for(auto&p:vet[i]){
  58.         //     cout << p.ff << " " << p.ss << " | ";
  59.         // }
  60.         // cout << endl;
  61.     }
  62.     priority_queue<pll, vll, greater<pll>> fila;
  63.     fila.push({0, 1});
  64.     vl dist(n+1, -1);
  65.     while(!fila.empty()){
  66.         auto a = fila.top();
  67.         fila.pop();
  68.         if(dist[a.ss] == -1){
  69.             if(a.ss == n){
  70.                 dist[a.ss] = a.ff;
  71.                 break;
  72.             }
  73.             auto it = vet[a.ss].upper_bound(a.ff);
  74.             if(it != vet[a.ss].begin()){
  75.                 it--;
  76.             }
  77.             if(it->ff <= a.ff && a.ff <= it->ss){
  78.  
  79.             } else{
  80.                 if( a.ff > it->ss){
  81.                     it++;
  82.                 }
  83.                 a.ff = it->ff;
  84.             }
  85.             dist[a.ss] = a.ff;
  86.             for(auto&p: listAdj[a.ss]){
  87.                 if(dist[p.ss] == -1){
  88.                     fila.push({a.ff + p.ff, p.ss});
  89.                 }
  90.             }
  91.         }
  92.     }
  93.     // for(int i = 1; i <= n; ++i){
  94.     //     cout << dist[i] << " \n"[i == n];
  95.     // }
  96.  
  97.     cout << dist[n] << "\n";
  98.  
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement