Submission #770605

#TimeUsernameProblemLanguageResultExecution timeMemory
770605MEGalodonToll (BOI17_toll)C++14
100 / 100
126 ms11888 KiB
#include <bits/stdc++.h> #define MAXN 50001 #define INF 100000000000000 #define ii pair<int, int> #define iii pair<ii, int> #define ll long long using namespace std; ll dist[MAXN][5][5]; ll ans[MAXN]; int K, N, M, O; ll weights[5][5][MAXN]; void dnc(int l, int r, vector<iii> q){ if( l == r ){ for( auto x : q ) ans[x.second] = -1; return; } int m = (l+r)/2; for( int i{0} ; i < K ; ++i ) for( int j{0} ; j < K ; ++j ){ if( i == j ) dist[m][i][j] = 0;//, dist[m+1][i][j] = 0; else dist[m][i][j] = INF;//, dist[m+1][i][j] = INF; } for( int lev{m-1} ; lev >= l ; --lev ){ for( int i{0} ; i < K ; ++i ) for( int k{0} ; k < K ; ++k ){ dist[lev][i][k] = INF; for( int j{0} ; j < K ; ++j ){ dist[lev][i][k] = min(dist[lev+1][j][k]+weights[i][j][lev], dist[lev][i][k]); } } } for( int lev{m+1} ; lev <= r ; ++lev ){ for( int i{0} ; i < K ; ++i ) for( int k{0} ; k < K ; ++k ){ dist[lev][i][k] = INF; for( int j{0} ; j < K ; ++j ){ dist[lev][i][k] = min(dist[lev-1][i][j]+weights[j][k][lev-1], dist[lev][i][k]); } } } vector<iii> lft, rght; for( auto x : q ){ int a = x.first.first, b = x.first.second; int id = x.second; int l1 = a/K, l2 = b/K; a %= K; b %= K; if( l1 <= m && l2 >= m ){ ans[id] = INF; for( int c{0} ; c < K ; ++c ) ans[id] = min(ans[id], dist[l1][a][c]+dist[l2][c][b]); } else if( l2 <= m ) lft.push_back(x); else rght.push_back(x); } dnc(l, m, lft); dnc(m+1, r, rght); } int main(){ cin>>K>>N>>M>>O; for( int i{0} ; i < K ; ++i ) for( int j{0} ; j < K ; ++j ) for( int k{0} ; k < (N-1)/K ; ++k ) weights[i][j][k] = INF; for( int i{1} ; i <= M ; ++i ){ //multi edges?? int u, v, w; cin>>u>>v>>w; int lev = u/K; u %= K; v %= K; weights[u][v][lev] = w; } vector<iii> queries; for( int i{1} ; i <= O ; ++i ){ int u, v; cin>>u>>v; queries.push_back(iii(ii(u, v), i)); } dnc(0, (N-1)/K, queries); for( int i{1} ; i <= O ; ++i ){ if( ans[i] < INF ) cout<<ans[i]<<'\n'; else cout<<"-1\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...