제출 #860468

#제출 시각아이디문제언어결과실행 시간메모리
860468NursikCommuter Pass (JOI18_commuter_pass)C++14
0 / 100
2041 ms132016 KiB
#include <stdio.h>

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <random>
#include <chrono>
#include <queue>
#include <functional>
#include <iostream>
#include <queue>
//#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double

const ll maxn = 1e6 + 1, maxm = 1e7 + 1;
const ll mod = 1e9 + 7, mod2 = 998244353, inf = 1e9, block = 550, hb = 31, base = 1000050017;
const ld eps = 1e-9;

int n, m;
int s, t;
int ss, tt;
bool is[maxn];
ll ans = inf * inf, dp[maxn], dp2[maxn], dp3[maxn], dppd[maxn][4];
vector<pair<int, int>> g[maxn], g3[maxn], g4[maxn];
vector<int> g2[maxn];
void dfsis(int v){
    if (v == t){
        is[v] = 1;
    }
    for (auto to : g2[v]){
        dfsis(to);
        is[v] |= is[to];
    }
}

void dijkstra(){
    for (int i = 1; i <= n; ++i){
        dp[i] = inf * inf;
    }
    dp[s] = 0;
    set<pair<ll, int>> setik;
    setik.insert(mp(0, s));
    while (setik.size() > 0){
        pair<ll, int> cur = *setik.begin();
        setik.erase(cur);
        ll cost = cur.f, v = cur.s;
        if (dp[v] < cost){
            continue;
        }
        for (auto to : g[v]){
            int nv = to.f, w = to.s;
            if (dp[nv] > cost + w){
                dp[nv] = cost + w;
                setik.insert(mp(dp[nv], nv));
            }
        }
    }
    for (int i = 1; i <= n; ++i){
        for (auto to : g[i]){
            int nv = to.f, w = to.s;
            if (dp[i] > dp[nv]){
                continue;
            }
            if (dp[nv] == w + dp[i] && dp[i] < dp[nv]){
                g2[i].pb(nv);
            }
            else{
                g3[i].pb(mp(nv, w));
                g3[nv].pb(mp(i, w));
            }
        }
    }
    dfsis(s);

    for (int i = 1; i <= n; ++i){
        g2[i].clear();
        g3[i].clear();
    }
    for (int i = 1; i <= n; ++i){
        for (auto to : g[i]){
            int nv = to.f, w = to.s;
            if (dp[i] > dp[nv]){
                continue;
            }
            if (dp[nv] == w + dp[i] && dp[i] < dp[nv] && is[nv] == 1){
                g2[i].pb(nv);
                g4[i].pb(mp(nv, w));
            }
            else{
                g3[i].pb(mp(nv, w));
                g3[nv].pb(mp(i, w));
                //cout << 
            }
        }
    }
}
void dijkstra2(){
    for (int i = 1; i <= n; ++i){
        dp2[i] = inf * inf;
    }
    dp2[ss] = 0;
    set<pair<ll, int>> setik;
    setik.insert(mp(0, ss));
    while (setik.size() > 0){
        pair<ll, int> cur = *setik.begin();
        setik.erase(cur);
        ll cost = cur.f, v = cur.s;
        if (dp2[v] < cost){
            continue;
        }
      //  cout << v << " " << dp2[v] << '\n';
        for (auto to : g3[v]){
            int nv = to.f, w = to.s;
            if (dp2[nv] > cost + w){
                dp2[nv] = cost + w;
                setik.insert(mp(dp2[nv], nv));
            }
        }
    }
}
void dijkstra3(){
    for (int i = 1; i <= n; ++i){
        dp3[i] = inf * inf;
    }
    dp3[tt] = 0;
    set<pair<ll, int>> setik;
    setik.insert(mp(0, tt));
    while (setik.size() > 0){
        pair<ll, int> cur = *setik.begin();
        setik.erase(cur);
        ll cost = cur.f, v = cur.s;
        if (dp3[v] < cost){
            continue;
        }
        for (auto to : g3[v]){
            int nv = to.f, w = to.s;
            if (dp3[nv] > cost + w){
                dp3[nv] = cost + w;
                setik.insert(mp(dp3[nv], nv));
            }
        }
    }
}

void dfs(int v){
    dppd[v][0] = dp3[v];
    dppd[v][1] = dp2[v];
    dppd[v][2] = dp3[v];
    dppd[v][3] = dp2[v];
    if (v == t){
        is[v] = 1;
        return;
    }
    for (auto to : g4[v]){
        dfs(to.f);
        is[v] |= is[to.f];
        if (is[to.f] == 1){
            dppd[v][0] = min(dppd[v][0], dppd[to.f][0] + to.s);
            dppd[v][2] = min(dppd[v][2], dppd[to.f][0]);
            dppd[v][1] = min(dppd[v][1], dppd[to.f][1] + to.s);
            dppd[v][3] = min(dppd[v][3], dppd[to.f][3]);
        }
    }
    if (is[v] == 1){
        ans = min(ans, dppd[v][3] + dppd[v][0]);
        ans = min(ans, dppd[v][2] + dppd[v][1]);
    }
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    cin >> s >> t;
    cin >> ss >> tt;
    
    for (int i = 1; i <= m; ++i){
        int a, b, c;
        cin >> a >> b >> c;
        g[a].pb(mp(b, c));
        g[b].pb(mp(a, c));
    }
    dijkstra();
    dijkstra2();
   // exit(0);
    dijkstra3();
    dfs(s);
    //cout << "kek\n";
    //cout << dp2[2] << '\n';
    ans = min(ans, dp2[tt]);
    //cout << dp3[tt] << '\n';
    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...