제출 #1347388

#제출 시각아이디문제언어결과실행 시간메모리
1347388thesentroCommuter Pass (JOI18_commuter_pass)C++20
100 / 100
283 ms29844 KiB
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long
ll mod = 1e9+7;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

ll binpow(ll a, ll b)
{
    ll res = 1;
    while (b>0)
    {
        if (b&1)
            res = (res*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return res;
}
ll gcd(ll x, ll y)
{
    if (y==0)
        return x;
    return gcd(y, x%y);
}
ll maxi = 202020;
vector<vector<pair<ll,ll>>>g(maxi);
vector<vector<ll>>adj(maxi);
vector<ll>dp1(maxi, LLONG_MAX), dp2(maxi, LLONG_MAX);
vector<ll>dist;
ll n,m,s,t,u,v;
vector<ll>du(maxi, LLONG_MAX), dv(maxi, LLONG_MAX);
vector<ll> dijkstra1(ll x)
{
    for (int i=1 ; i<=n ; i++) 
        dist[i] = dp1[i] = LLONG_MAX;
    dist[x] = 0;
    priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>>pq;
    pq.push({0, x});
    dp1[x] = du[x];
    while (!pq.empty())
    {
        auto it = pq.top();
        pq.pop();
        ll d = it.first;
        ll v = it.second;
        if (d!=dist[v]) continue;
        for (auto to:g[v])
        {
            ll i = to.first;
            ll w = to.second;
            if (dist[i]>dist[v]+w)
            {
                dist[i] = dist[v]+w;
                pq.push({dist[i], i});
                dp1[i] = min(du[i], dp1[v]);
            }
            if (dist[i]==dist[v]+w)
                dp1[i] = min({dp1[i], du[i], dp1[v]});
        }
    }
    return dist;
}
vector<ll> dijkstra2(ll x)
{
    for (int i=1 ; i<=n ; i++) 
        dist[i] = dp2[i] = LLONG_MAX;
    dist[x] = 0;
    priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>>pq;
    pq.push({0, x});
    dp2[x] = dv[x];
    while (!pq.empty())
    {
        auto it = pq.top();
        pq.pop();
        ll d = it.first;
        ll v = it.second;
        if (d!=dist[v]) continue;
        for (auto to:g[v])
        {
            ll i = to.first;
            ll w = to.second;
            if (dist[i]>dist[v]+w)
            {
                dist[i] = dist[v]+w;
                pq.push({dist[i], i});
                dp2[i] = min(dv[i], dp2[v]);
            }
            if (dist[i]==dist[v]+w)
                dp2[i] = min({dp2[i], dv[i], dp2[v]});
        }
    }
    return dist;
}
vector<ll>dijkstra(ll x)
{
    for (int i=1 ; i<=n ; i++) 
        dist[i] = LLONG_MAX;
    dist[x] = 0;
    priority_queue<pair<ll,ll>, vector<pair<ll,ll>>, greater<pair<ll,ll>>>pq;
    pq.push({0, x});
    while (!pq.empty())
    {
        auto it = pq.top();
        pq.pop();
        ll d = it.first;
        ll v = it.second;
        if (d!=dist[v]) continue;
        for (auto to:g[v])
        {
            ll i = to.first;
            ll w = to.second;
            if (dist[i]>dist[v]+w)
            {
                dist[i] = dist[v]+w;
                pq.push({dist[i], i});
            }
        }
    }
    return dist;
}
ll func(ll s, ll t)
{
    du = dijkstra(u);
    dv = dijkstra(v);
    // for (int i=1 ;i<=n ;i++)
    //     cout<<du[i]<<" ";
    // cout<<endl;
    // for (int i=1 ; i<=n ;i++)
    //     cout<<dv[i]<<" ";
    // cout<<endl;
    vector<ll>ds = dijkstra1(s);
    vector<ll>dt = dijkstra2(t);
    ll sp = ds[t];
    ll res = du[v];
    for (int i=1 ; i<=n ;i++)
    {
        if (ds[i]+dt[i]==sp)
            res = min(res, dp1[i]+dp2[i]);
    }
    return res;
}
void solve()
{
    cin>>n>>m>>s>>t>>u>>v;
    for (int i=1 ; i<=m ; i++)
    {
        ll x,y,z;
        cin>>x>>y>>z;
        g[x].push_back({y, z});
        g[y].push_back({x, z});
    }
    dist.assign(n+1, LLONG_MAX);
    cout<<min(func(s, t), func(t, s))<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ll tt = 1;
    // cin>>tt;
    while (tt--)
    {
        solve();
    }
    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...