Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define all(x) (x).begin(), (x).end()
  3.  
  4. using namespace std;
  5. using vi = vector<int>;
  6.  
  7. constexpr int ms = 413;
  8. constexpr int inf = 0x3f3f3f3f;
  9.  
  10. int dp[2][ms][ms];
  11.  
  12. int main()
  13. {
  14.  
  15.     memset( dp, inf, sizeof(dp) );
  16.     ios::sync_with_stdio( false ); cin.tie( nullptr );
  17.     int n, m;
  18.     cin >> n >> m;
  19.     vector< int > temperature( n );
  20.     for(int& t : temperature ) cin >> t;
  21.  
  22.     for(int i = 0; i < m; ++i)
  23.     {
  24.         int a, b, d;
  25.         cin >> a >> b >> d;
  26.         --a, --b;
  27.         dp[0][a][b] = dp[0][b][a] = dp[1][a][b] = dp[1][b][a] = min( dp[0][a][b], d );
  28.     }
  29.    
  30.     int q;
  31.     cin >> q;
  32.  
  33.     deque< tuple<int, int, int, int> > Q[2];
  34.    
  35.     for(int i = 0; i < q; ++i)
  36.     {
  37.         int a, b, k, t;
  38.         cin >> a >> b >> k >> t;
  39.         Q[t].emplace_back(k, a - 1, b - 1, i);
  40.     }
  41.    
  42.     for(int i = 0; i < 2; ++i) sort( all(Q[i] ) );
  43.    
  44.     vector< vi > nodes_by_k[2];
  45.    
  46.     vector< int > ans(q, -1);
  47.  
  48.     for(int i = 0; i < 2; ++i) nodes_by_k[i].resize( n + 1 );
  49.  
  50.     vector< int > cpy = temperature;
  51.     sort( all(cpy) );
  52.     cpy.erase( unique( all(cpy) ), cpy.end() );
  53.     vector< int > cpy2 = cpy;
  54.     reverse( all(cpy2) );
  55.  
  56.  
  57.     for(int i = 0; i < n; ++i)
  58.     {
  59.         int tmp = temperature[i];
  60.         int i_k = lower_bound( all(cpy), tmp ) - cpy.begin() + 1;
  61.         nodes_by_k[0][i_k].push_back( i );
  62.         i_k     = lower_bound( all(cpy2), tmp, [] ( const int& a, const int& b) { return a > b; } ) - cpy2.begin() + 1;
  63.         nodes_by_k[1][i_k].push_back( i );
  64.     }
  65.  
  66.  
  67.     for(int z = 0; z <= n; ++z)
  68.     {
  69.         vector< tuple<int, int, int> > go[2];
  70.         for(int i = 0; i < 2; ++i)
  71.         {
  72.             while( !Q[i].empty() && get<0>(Q[i].front() ) == z )
  73.             {
  74.                 go[i].emplace_back( get<1>(Q[i].front() ), get<2>(Q[i].front() ), get<3>(Q[i].front()) );
  75.                 Q[i].pop_front();
  76.             }
  77.            
  78.             for(const auto& piv : nodes_by_k[i][z] )
  79.             {
  80.                 for(int x = 0; x < n; ++x)
  81.                 {
  82.                     for(int y = 0; y < n; ++y)
  83.                     {
  84.                         if( dp[i][x][piv] != inf && dp[i][piv][y] != inf )
  85.                         {
  86.                             dp[i][x][y] = min( dp[i][x][y], dp[i][x][piv] + dp[i][piv][y] );
  87.                         }
  88.                     }
  89.                 }
  90.             }
  91.         }
  92.         for(int i = 0; i < 2; ++i)
  93.         {
  94.             for( const auto& qry : go[i] )
  95.             {
  96.                 int u = get<0>(qry), v = get<1>(qry), id = get<2>(qry);
  97.                 ans[ id ] = dp[i][u][v];
  98.             }
  99.         }
  100.     }
  101.     for(int i = 0; i < q; ++i) cout << ( ans[i] == inf ? -1 : ans[i] ) << endl;
  102.     return 0;
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement