Submission #172693

#TimeUsernameProblemLanguageResultExecution timeMemory
172693muhammad_hokimiyonEvacuation plan (IZhO18_plan)C++14
100 / 100
949 ms48028 KiB
#include <bits/stdc++.h> //#pragma GCC optimize("Ofast") #define fi first #define se second #define ll long long using namespace std; const int N = 2e5 + 7; const int M = 18; const ll mod = 998244353; int n,m,q,k; int a[N]; int d[N]; int p[N]; int cnt; int dep[N]; int tin[N]; int tout[N]; int up[N][M]; int mn[N][M]; bool used[N]; vector < pair < int , int > > v[N]; vector < pair < int , int > > g[N]; void dfs( int x , int p , int z ) { up[x][0] = p; mn[x][0] = z; tin[x] = ++cnt; dep[x] = dep[p] + 1; for( int i = 1; i < M; i++ ){ up[x][i] = up[up[x][i - 1]][i - 1]; mn[x][i] = min(mn[x][i - 1] , mn[up[x][i - 1]][i - 1]); } for( auto y : g[x] ){ if( y.fi != p ){ dfs(y.fi , x , y.se); } } tout[x] = cnt; } bool upper( int x , int y ) { return (tin[x] <= tin[y] && tout[x] >= tout[y]); } int lca( int x , int y ) { if( upper(x , y) )return x; if( upper(y , x) )return y; for( int i = M - 1; i >= 0; i-- ){ if( !upper(up[x][i] , y) ){ x = up[x][i]; } } return up[x][0]; } int get( int x ) { return (p[x] == x ? x : p[x] = get(p[x])); } int get1( int x , int y ) { int ans = 1e9; for( int i = M - 1; i >= 0; i-- ){ if( (1 << i) <= y ){ y -= (1 << i); ans = min(ans , mn[x][i]); x = up[x][i]; } } return ans; } void solve1() { cin >> n >> m; for( int i = 1; i <= m; i++ ){ int x,y,z; cin >> x >> y >> z; v[x].push_back({y , z}); v[y].push_back({x , z}); } for( int i = 1; i <= n; i++ ){ d[i] = 1e9; p[i] = i; } cin >> k; set < pair < int , int > > s; for( int i = 1; i <= k; i++ ){ cin >> a[i]; s.insert({0 , a[i]}); d[a[i]] = 0; } while( !s.empty() ){ auto x = *s.begin(); s.erase(s.begin()); for( auto y : v[x.se] ){ if( y.se + d[x.se] < d[y.fi] ){ s.erase({d[y.fi] , y.fi}); d[y.fi] = y.se + d[x.se]; s.insert({d[y.fi] , y.fi}); } } } vector < pair < int , int > > f; for( int i = 1; i <= n; i++ ){ f.push_back({d[i] , i}); } sort( f.rbegin() , f.rend() ); for( int i = 0; i < (int)f.size(); i++ ){ used[f[i].se] = true; for( auto y : v[f[i].se] ){ if( used[y.fi] && get(y.fi) != get(f[i].se) ){ g[f[i].se].push_back({y.fi,f[i].fi}); g[y.fi].push_back({f[i].se,f[i].fi}); p[get(f[i].se)] = get(y.fi); } } } dfs(1 , 1 , 0); cin >> q; while( q-- ){ int x,y; cin >> x >> y; int gg = lca(x , y); cout << min( get1(x , dep[x] - dep[gg]) , get1(y , dep[y] - dep[gg]) ) << "\n"; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); //freopen( "input.txt" , "r" , stdin ); //freopen( "output.txt" , "w" , stdout ); int cghf = 1;//cin >> cghf; while( cghf-- ){ solve1(); } }
#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...