Submission #1167725

#TimeUsernameProblemLanguageResultExecution timeMemory
1167725lizaCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
249 ms25560 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target ("avx2,bmi,bmi2,popcnt,lzcnt")
using namespace std;

const int N = 100005;
vector<pair<int, long long>> graph[N];

int mark[N]={0};
long long minU[N], minV[N];
long long dv[N], dt[N], ds[N], du[N];
vector<int> opt[N];
vector<pair<long long, int>> ve;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, m, s, t, u, v; cin >> n >> m >> s >> t >> u >> v;

    for(int i = 0; i < m; i++)
    {
        int a, b;
        long long c;
        cin >> a >> b >> c;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }
    priority_queue <pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
    for(int i = 0; i <= n; i++)ds[i]= 1e18;
    int vis[N]={0};
    ds[s]=0;
    pq.push({0, s});
    while(!pq.empty())
    {
        pair<long long,int> x = pq.top();
        pq.pop();
        if(vis[x.second]==1)continue;
        vis[x.second]=1;
        for(auto i: graph[x.second])
        {
            if(ds[i.first] > x.first+i.second)
            {
                pq.push({x.first+i.second, i.first});
                ds[i.first]=x.first+i.second;
            }
        }
    }

    for(int i = 0; i <= n; i++)
    {
        dt[i]= 1e18;
        vis[i]=0;
    }
    dt[t]=0;

    pq.push({0, t});
    while(!pq.empty())
    {
        pair<long long,int> x = pq.top();
        pq.pop();
        if(vis[x.second]==1)continue;
        vis[x.second]=1;
        for(auto i: graph[x.second])
        {
            if(dt[i.first] > x.first+i.second)
            {
                pq.push({x.first+i.second, i.first});
                dt[i.first]=x.first+i.second;
            }
        }
    }


    for(int i = 0; i <= n; i++)
    {
        dv[i]= 1e18;
        vis[i]=0;
    }
    dv[v]=0;
    pq.push({0, v});

    while(!pq.empty())
    {
        pair<long long,int> x = pq.top();
        pq.pop();
        if(vis[x.second]==1)continue;
        vis[x.second]=1;
        for(auto i: graph[x.second])
        {
            if(dv[i.first] > x.first+i.second)
            {
                pq.push({x.first+i.second, i.first});
                dv[i.first]=x.first+i.second;
            }
        }
    }

    for(int i = 0; i <= n; i++)
    {
        du[i]= 1e18;
        vis[i]=0;
    }
    du[u]=0;
    pq.push({0, u});

    while(!pq.empty())
    {
        pair<long long,int> x = pq.top();
        pq.pop();
        if(vis[x.second]==1)continue;
        vis[x.second]=1;
        for(auto i: graph[x.second])
        {
            if(du[i.first] > x.first+i.second)
            {
                pq.push({x.first+i.second, i.first});
                du[i.first]=x.first+i.second;
            }
        }
    }

   // long long rezu=1e18, rezv= 1e18;
    for(int i = 1; i <= n; i++)
    {
        if(dt[i]+ds[i]==ds[t])
        {
            mark[i]=1;
        }
    }

    for(int i = 1; i <= n; i++)
    {
        if(!mark[i])continue;
        for(auto j: graph[i])
        {
            if(mark[j.first] && (ds[j.first]==ds[i]+j.second))
            {
                opt[i].push_back(j.first);
            }
        }

        ve.push_back({ds[i], i});
    }
    sort(ve.begin(), ve.end(), greater<pair<long long, int>>());
    long long rez=1e18;
    for(auto i: ve)
    {
        minU[i.second]=du[i.second];
        minV[i.second]=dv[i.second];
        for(auto j: opt[i.second])
        {
            minU[i.second]=min(minU[i.second], minU[j]);
            minV[i.second]=min(minV[i.second], minV[j]);
        }
        rez = min(rez, min(du[i.second]+minV[i.second],dv[i.second]+minU[i.second]));
    }





    cout << min(dv[u], rez) << "\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...