Submission #1359641

#TimeUsernameProblemLanguageResultExecution timeMemory
1359641DucNguyen2007Evacuation plan (IZhO18_plan)C++20
0 / 100
112 ms75036 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define TEXT "EXIT"
const int maxN=1e5+5,LG=30;
const ll inf=1e18;
int n,m,k,a[maxN+1],h[maxN+1],q;
ll d[maxN+1];
struct duongdi
{
    int u;
    ll w;
    bool operator < (const duongdi &o)const
    {
        return w>o.w;
    }
};
struct canh
{
    int u,v;
    ll w;
    bool operator < (const canh &o)const
    {
        return w>o.w;
    }
};
struct dsu
{
    int parent[maxN+1];
    dsu()
    {
        memset(parent,-1,sizeof(parent));
    }
    int Find(int v)
    {
        if(parent[v]<0)
        {
            return v;
        }
        return parent[v]=Find(parent[v]);
    }
    void Union(int a,int b)
    {
        a=Find(a);
        b=Find(b);
        if(a!=b)
        {
            if(-parent[a]<-parent[b])
            {
                swap(a,b);
            }
            parent[a]+=parent[b];
            parent[b]=a;
        }
    }
}ds;
vector<canh> dscanh,ndscanh;
vector<duongdi> adj[maxN+1];
priority_queue<duongdi> pq;
pll up[maxN+1][LG+1];
void dfs(int u)
{
    for(auto [v,w]:adj[u])
    {
        if(v!=up[u][0].fi)
        {
            up[v][0].fi=u;
            up[v][0].se=w;
            h[v]=h[u]+1;
            for(int j=1;j<=LG;j++)
            {
                up[v][j].fi=up[up[v][j-1].fi][j-1].fi;
                up[v][j].se=min(up[v][j-1].se,up[up[v][j-1].fi][j-1].se);
            }
            dfs(v);
        }
    }
}
ll get(int u,int v)
{
    ll res=inf;
    if(h[u]!=h[v])
    {
        if(h[u]<h[v])
        {
            swap(u,v);
        }
        for(int j=LG;j>=0;j--)
        {
            if(h[up[u][j].fi]>=h[v])
            {
                res=min(res,up[u][j].se);
                u=up[u][j].fi;
            }
        }
    }
    if(u==v)
    {
        return res;
    }
    for(int j=LG;j>=0;j--)
    {
        if(up[u][j].fi!=up[v][j].fi)
        {
            res=min(res,min(up[u][j].se,up[v][j].se));
            cout<<res<<" "<<up[u][j].se<<" "<<up[v][j].se<<'\n';
            u=up[u][j].fi;
            v=up[v][j].fi;
        }
    }
    //cout<<u<<" "<<v<<" "<<up[u][0].fi<<" "<<up[u][0].se<<" ";
    return min(res,min(up[u][0].se,up[v][0].se));
}
void dijkstra()
{
    for(int i=1;i<=n;i++)
    {
        d[i]=inf;
    }
    for(int i=1;i<=k;i++)
    {
        d[a[i]]=0;
        pq.push({a[i],0});
    }
    while(!pq.empty())
    {
        duongdi tmp=pq.top();
        pq.pop();
        int u=tmp.u;
        if(d[u]>tmp.w)
        {
            continue;
        }
        for(auto [v,w]:adj[u])
        {
            if(d[v]>d[u]+w)
            {
                d[v]=d[u]+w;
                pq.push({v,d[v]});
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        adj[i].clear();
    }
}
int main()
{
    //freopen(TEXT".INP","r",stdin);
    //freopen(TEXT".OUT","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int u,v,w;
        cin>>u>>v>>w;
        adj[u].push_back({v,w});
        adj[v].push_back({u,w});
        dscanh.push_back({u,v,w});
    }
    cin>>k;
    for(int i=1;i<=k;i++)
    {
        cin>>a[i];
    }
    dijkstra();
    for(auto [u,v,w]:dscanh)
    {
        ndscanh.push_back({u,v,min(d[u],d[v])});
    }
    sort(ndscanh.begin(),ndscanh.end());
    for(auto [u,v,w]:ndscanh)
    {
        if(ds.Find(u)!=ds.Find(v))
        {
            ds.Union(u,v);
            //cout<<u<<" "<<v<<" "<<w<<'\n';
            adj[u].push_back({v,w});
            adj[v].push_back({u,w});
        }
    }
    h[1]=1;
    for(int i=0;i<=n;i++)
    {
        for(int j=0;j<=LG;j++)
        {
            up[i][j].se=inf;
        }
    }
    dfs(1);
    cin>>q;
    while(q--)
    {
        int u,v;
        cin>>u>>v;
        cout<<get(u,v)<<'\n';
    }
}
/*5 6
3 5 910955
3 2 948455
5 4 849812
4 3 430993
2 4 126792
2 5 56074
1
2
2
2 5
1 4
*/
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...