Submission #229198

#TimeUsernameProblemLanguageResultExecution timeMemory
229198DodgeBallManEvacuation plan (IZhO18_plan)C++14
0 / 100
612 ms80484 KiB
#include <bits/stdc++.h>
#define pii pair<long long, long long>
#define x first
#define y second

using namespace std;

const int N = 1e5 + 10;
long long n, m, dp[21][2*N], d[2*N], mn[2*N], dep[2*N], k, Q, par[2*N], cnt;
vector<long long> t[2*N], pred;
vector<pii> g[N], all;
priority_queue<pii> q;

void dijk() {
    while( !q.empty() ) {
        pii temp = q.top(); q.pop();
        for( pii i : g[temp.y] ) {
            int v = i.x, di = i.y - temp.x;
            if( di < d[v] ) { d[v] = di; q.emplace( -1*di, v ); }
        }
    }
}

void dfs( int u = cnt, int p = 0 ) {
    dp[0][u] = p;
    dep[u] = dep[p] + 1;
    for( int i = 1 ; i < 20 ; i++ ) dp[i][u] = dp[i-1][dp[i-1][u]];
    for( int v : t[u] ) if( v != p ) {
        dfs( v, u );
        mn[u] = min( mn[u], mn[v] );
    }
}

int lca( int a, int b ) {
    if( dep[a] < dep[b] ) swap( a, b );
    for( int i = 19 ; i >= 0 ; i-- ) if( dep[dp[i][a]] >= dep[b] ) a = dp[i][a];
    if( a == b ) return a;
    for( int i = 19 ; i >= 0 ; i-- ) if( dp[i][a] != dp[i][b] ) a = dp[i][a], b = dp[i][b];
    return dp[0][a];
}

int findroot( int a ) {
    if( a == par[a] ) return a;
    else return par[a] = findroot( par[a] );
}

int main()
{
    scanf("%lld %lld",&n,&m);
    for( int i = 0, a ; i < m ; i++ ) {
        long long b, c;
        scanf("%d %lld %lld",&a,&b,&c);
        g[a].emplace_back( pii( b, c ) ), g[b].emplace_back( pii( a, c ) );
    }
    scanf("%lld",&k);
    fill( d, d+2*N, 1e17 );
    for( int i = 0 ; i < k ; i++ ) {
        long long a;
        scanf("%lld",&a);
        d[a] = 0;
        q.emplace( 0, a );
    }
    dijk();
    //printf("HEY");
    for( int i = 1 ; i <= n ; i++ ) all.emplace_back( pii( d[i], i ) );
    sort( all.begin(), all.end(), greater<pii>() );
    for( int i = 1 ; i <= 2*n ; i++ ) par[i] = i;
    cnt = n;
    //printf("NODE : ");
    for( pii i : all ) {
        int u = i.y, di = i.x;
        //printf("%d ",u);
        for( pii x : g[u] ) {
            int v = x.x;
            if( d[v] > d[u] ) {
                int a = findroot( ( int )u ), b = findroot( ( int )v );
                if( a == b ) continue ;
                par[a] = par[b] = ++cnt;
                t[cnt].emplace_back( a ), t[cnt].emplace_back( b );
            }
        }
    }
    fill( mn, mn+2*N, 1e17 );
    for( int i = 1 ; i <= n ; i++ ) mn[i] = d[i];
    dfs();
    scanf("%d",&Q);
    for( int i = 0, a, b ; i < Q ; i++ ) {
        scanf("%d %d",&a,&b);
        int c = lca( a, b );
        printf("%lld\n", mn[c] );
    }
    return 0;
}

Compilation message (stderr)

plan.cpp: In function 'int main()':
plan.cpp:71:22: warning: unused variable 'di' [-Wunused-variable]
         int u = i.y, di = i.x;
                      ^~
plan.cpp:86:18: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
     scanf("%d",&Q);
                ~~^
plan.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld %lld",&n,&m);
     ~~~~~^~~~~~~~~~~~~~~~~~~
plan.cpp:52:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %lld %lld",&a,&b,&c);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
plan.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%lld",&k);
     ~~~~~^~~~~~~~~~~
plan.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%lld",&a);
         ~~~~~^~~~~~~~~~~
plan.cpp:86:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d",&Q);
     ~~~~~^~~~~~~~~
plan.cpp:88:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d",&a,&b);
         ~~~~~^~~~~~~~~~~~~~~
#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...