Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define TRACE(x) x
  6. #define WATCH(x) TRACE( cout << #x" = " << x << endl)
  7. #define PRINT(x) TRACE(printf(x))
  8. #define WATCHR(a, b) TRACE( for(auto c = a; c != b;) cout << *(c++) << " "; cout << endl)
  9. #define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end()); } )
  10.  
  11. #define all(v) (v).begin(), (v).end()
  12. #define rall(v) (v).rbegin(), (v).rend()
  13. #define sz(v) (int) (v).size()
  14. #define rep(i,a,b) for(int (i) = (a); (i) < (b); ++(i))
  15. #define pb push_back
  16. #define mp make_pair
  17.  
  18. using ll = long long;
  19. using vi = vector<int>;
  20. using pii = pair<int, int>;
  21. using pll = pair<ll, ll>;
  22.  
  23. constexpr int inf = 0x3f3f3f3f;
  24. constexpr ll MOD = 1000000007LL;
  25. constexpr double tol = 1e-8;
  26.  
  27. void buff() { ios::sync_with_stdio( false ); cin.tie(nullptr); }
  28.  
  29. constexpr int ms = 413;
  30.  
  31. int dp[2][ms][ms][ms];
  32.  
  33. int main()
  34. {
  35.     memset( dp, inf, sizeof(dp) );
  36.    
  37.     buff();
  38.     int n, r;
  39.     cin >> n >> r;
  40.    
  41.     vector< int > t(n);
  42.    
  43.     for(int& x : t) cin >> x;
  44.    
  45.     for(int i = 0; i < r; ++i)
  46.     {
  47.         int a, b, d;
  48.         cin >> a >> b >> d;
  49.         --a, --b;
  50.         dp[1][a][b][0] = dp[0][a][b][0] = min( dp[0][a][b][0], d );
  51.         dp[1][b][a][0] = dp[0][b][a][0] = min( dp[0][b][a][0], d );
  52.     }
  53.    
  54.     for(int i = 0; i < n; ++i) dp[0][i][i][0] = dp[1][i][i][0] = 0;
  55.  
  56.     vector< int > nodes(n, 0); iota(all(nodes), 0 );
  57.     sort( all(nodes), [&] ( const int& a, const int& b ) { return t[a] < t[b]; });
  58.    
  59.     // Tratando empates
  60.     vector< vi > kth_smallest;
  61.     vector< vi > kth_largest;
  62.     vector< int > cur_group = { nodes[0] };
  63.    
  64.     for(int i = 1; i < n; ++i)
  65.     {
  66.         if( t[ nodes[i] ] == t[ nodes[i - 1] ] )
  67.         {
  68.             cur_group.push_back( nodes[i] );
  69.         }
  70.         else
  71.         {
  72.             kth_smallest.push_back( cur_group );
  73.             cur_group.clear();
  74.             cur_group.push_back( nodes[i] );
  75.         }
  76.     }
  77.    
  78.     kth_smallest.push_back( cur_group );
  79.  
  80.     cur_group.clear();
  81.     cur_group.push_back( nodes[ n - 1] );
  82.    
  83.     for(int i = n - 2; i >= 0; --i)
  84.     {
  85.         if( t[nodes[i]] == t[nodes[i + 1]] )
  86.         {
  87.             cur_group.push_back( nodes[i] );
  88.         }
  89.         else
  90.         {
  91.             kth_largest.push_back( cur_group );
  92.             cur_group.clear();
  93.             cur_group.push_back( nodes[i] );
  94.         }
  95.     }
  96.     kth_largest.push_back( cur_group );
  97.     // Fim do tratamento de empates
  98.    
  99.     int distinct = sz(kth_largest);
  100.     for(int z = 0; z < n; ++z)
  101.     {
  102.         for(int x = 0; x < n; ++x)
  103.         {
  104.             for(int y = 0; y < n; ++y)
  105.             {
  106.                 dp[0][x][y][z + 1] = dp[0][x][y][z];
  107.                 dp[1][x][y][z + 1] = dp[1][x][y][z];
  108.             }
  109.         }
  110.        
  111.         if( z < distinct )
  112.         {
  113.             for(int& node : kth_largest[z] )
  114.             {
  115.                 for(int x = 0; x < n; ++x)
  116.                 {
  117.                     for(int y = 0; y < n; ++y )
  118.                     {    
  119.                         if( dp[1][x][node][z + 1] != inf && dp[1][node][y][z + 1] != inf )
  120.                         {
  121.                             dp[1][x][y][z + 1] = min( dp[1][x][y][z + 1], dp[1][x][node][z + 1] + dp[1][node][y][z + 1] );
  122.                         }
  123.                     }
  124.                 }
  125.             }
  126.             for(int& node : kth_smallest[z] )
  127.             {
  128.                 for(int x = 0; x < n; ++x)
  129.                 {
  130.                     for(int y = 0; y < n; ++y)
  131.                     {
  132.                         if( dp[0][x][node][z + 1] != inf && dp[0][node][y][z + 1] != inf )
  133.                         {
  134.                             dp[0][x][y][z + 1] = min( dp[0][x][y][z + 1], dp[0][x][node][z + 1] + dp[0][node][y][z + 1] );
  135.                         }
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.     }
  141.    
  142.     int q;
  143.     cin >> q;
  144.    
  145.     for(int i = 0; i < q; ++i)
  146.     {
  147.         int a, b, k, t;
  148.         cin >> a >> b >> k >> t;
  149.         --a; --b;
  150.         int ans = dp[t][a][b][k];
  151.         cout << ( ans != inf ? ans : -1 ) << '\n';
  152.     }
  153.  
  154.     return 0;
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement