Submission #1180299

#TimeUsernameProblemLanguageResultExecution timeMemory
1180299Zbyszek99Commuter Pass (JOI18_commuter_pass)C++20
100 / 100
737 ms26408 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ff first
#define ss second
#define vl vector<long long>
#define vi vector<int>
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;

vector<pll> graph[100'001];
ll dist[100'001][4];
ll ans = 1e18;
bool odw[100'001];
bool odw_l[100'001][3];
ll dist_l[100'001][3];
int n,m;

void dij(int start, int type)
{
    rep2(i,1,n) odw[i] = 0;
    rep2(i,1,n) dist[i][type] = 1e18;
    priority_queue<pll> pq;
    pq.push({0,start});
    while(!pq.empty())
    {
        pll t = pq.top();
        pq.pop();
        if(odw[t.ss]) continue;
        odw[t.ss] = 1;
        dist[t.ss][type] = -t.ff;
        forall(it,graph[t.ss])
        {
            pq.push({t.ff-it.ss,it.ff});
        }
    }
}

void solve(int s, int t, int u, int v)
{
    dij(s,0);
    dij(t,1);
    dij(u,2);
    dij(v,3);
    rep2(i,1,n) rep(k,3)
    {
        odw_l[i][k] = 0;
        dist_l[i][k] = 1e18;
    }
    ans = min(ans,dist[u][3]);
    priority_queue<pair<ll,pii>> pq;
    pq.push({0,{s,0}});
    while(!pq.empty())
    {
        pair<ll,pii> t = pq.top();
        pq.pop();
        if(odw_l[t.ss.ff][t.ss.ss]) continue;
        odw_l[t.ss.ff][t.ss.ss] = 1;
        int vert = t.ss.ff;
        int l = t.ss.ss;
        ll odl = -t.ff;
        dist_l[vert][l] = odl;
      //  cout << vert << " " << l << " " << odl << " dij\n";
        if(l == 0)
        {
            pq.push({-odl - dist[vert][2],{vert,l+1}});
        }
        if(l == 1)
        {
            pq.push({-odl - dist[vert][3],{vert,l+1}});
        }
        forall(it,graph[vert])
        {
      //      cout << it.ff << " " << dist[vert][0] << " " << dist[it.ff][1] << " " << it.ss << " " << dist[s][1] << " edge\n";
            if(dist[vert][0] + dist[it.ff][1] + it.ss == dist[s][1])
            {
                pq.push({-odl,{it.ff,l}});
            }
        }
    }
    ans = min(ans,dist_l[t][2]);
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    //random();
    cin >> n >> m;
    int s,t,u,v;
    cin >> s >> t >> u >> v;
    rep(i,m)
    {
        int a,b,c;
        cin >> a >> b >> c;
        graph[a].pb({b,c});
        graph[b].pb({a,c});
    }
    solve(s,t,u,v);
    solve(s,t,v,u);
    cout << ans << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...