제출 #160405

#제출 시각아이디문제언어결과실행 시간메모리
160405godwindCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
735 ms26532 KiB
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <random>
#include <iomanip>
#include <bitset>
 
 
using namespace std;
 
template<typename T> void uin(T &a, T b) {
    if (b < a) {
        a = b;
    }
}
 
template<typename T> void uax(T &a, T b) {
    if (b > a) {
        a = b;
    }
}
 
#define int long long
#define left left228
#define right right228
#define prev prev228
#define list list228
#define mp make_pair
#define all(v) v.begin(), v.end()
#define forn(i, n) for (int i = 0; i < (int)n; ++i)
#define firn(i, n) for (int i = 1; i < (int)n; ++i)
#define x first
#define y second

const int N = 100 * 1000 + 228;
const int INF = 1e18 + 228;

pair<int, int> ds[N], dt[N];
int n, m, s, t, U, V;
vector< pair<int, int> > g[N];

vector<int> find(int a) {
    vector<int> d(n + 1);
    for (int i = 1; i <= n; ++i) {
        d[i] = INF;
    }
    d[a] = 0;
    priority_queue< pair<int, int> > q;
    q.push({0, a});
    pair<int, int> p;
    while (!q.empty()) {
        p = q.top();
        q.pop();
        int v = p.second;
        for (pair<int, int> go : g[v]) {
            int to = go.first, w = go.second;
            if (d[v] + w < d[to]) {
                d[to] = d[v] + w;
                q.push({-d[to], to});
            }
        }
    }
    return d;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    cin >> s >> t;
    cin >> U >> V;
    for (int i = 0; i < m; ++i) {
        int u, v, c;
        cin >> u >> v >> c;
        g[u].push_back({v, c});
        g[v].push_back({u, c});
    }
    vector<int> du = find(U);
    vector<int> dv = find(V);
    for (int i = 1; i <= n; ++i) {
        ds[i] = dt[i] = {INF, INF};
    }
    ds[s] = {0, dv[s]};
    priority_queue< pair< pair<int, int>, int> > q;
    q.push({{0, -dv[s]}, s});
    while (!q.empty()) {
        pair< pair<int, int> , int> p = q.top();
        q.pop();
        int v = p.second;
        for (pair<int, int> go : g[v]) {
            int to = go.first, w = go.second;
            if (ds[v].first + w < ds[to].first || (ds[v].first + w == ds[to].first && min(ds[v].second, dv[to]) < ds[to].second)) {
                ds[to].first = ds[v].first + w;
                ds[to].second = min(ds[v].second, dv[to]);
                q.push({{-ds[to].first, -ds[to].second}, to});
            }
        }
    }
    while (!q.empty()) {
        q.pop();
    }
    dt[t] = {0, dv[t]};
    q.push({{0, -dv[t]}, t});
    while (!q.empty()) {
        pair< pair<int, int> , int> p = q.top();
        q.pop();
        int v = p.second;
        for (pair<int, int> go : g[v]) {
            int to = go.first, w = go.second;
            if (dt[v].first + w < dt[to].first || (dt[v].first + w == dt[to].first && min(dt[v].second, dv[to]) < dt[to].second)) {
                dt[to].first = dt[v].first + w;
                dt[to].second = min(dt[v].second, dv[to]);
                q.push({{-dt[to].first, -dt[to].second}, to});
            }
        }
    }
    int res = du[V];
    for (int i = 1; i <= n; ++i) {
        if (ds[i].first + dt[i].first == ds[t].first) {
            int cur = du[i] + min(ds[i].second, dt[i].second);
            uin(res, cur);
        }
    }
    cout << res << '\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...