Submission #1083283

#TimeUsernameProblemLanguageResultExecution timeMemory
1083283DaciejMaciejCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
227 ms24048 KiB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>
 
using namespace std;
// using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;
 
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
 
typedef vector<char> VC;
typedef vector<vector<char>> VVC;
typedef vector<bool> VB;
typedef vector<vector<bool>> VVB;
 
typedef vector<double> VD;
typedef vector<vector<double>> VVD;
 
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<pair<int, int>> VPI;
typedef vector<pair<long long, long long>> VPL;
 
#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define kwad(a) ((a) * (a))
 
// zero indexed
template <class T>
struct segtree
{
    const T def = 0;
    int n;
    vector<T> seg;
 
    segtree(int _size) : n(_size), seg(2 * _size, def) {}
 
    T merge(T a, T b)
    {
        return max(a, b);
    }
    void update(int pos, T val)
    {
        for (seg[pos += n] = val; pos /= 2;)
        {
            seg[pos] = merge(seg[pos * 2], seg[pos * 2 + 1]);
        }
    }
    T query(int l, int r)
    { // get [l, r)
        T a = def, b = def;
        for (l += n, r += n; l < r; l /= 2, r /= 2)
        {
            if (l % 2)
                a = merge(a, seg[l++]);
            if (r % 2)
                b = merge(b, seg[--r]);
        }
        return merge(a, b);
    }
};
 
const ll MOD = 1e9 + 7;
 
template <class T>
void maxi(T &a, T b)
{
    a = max(a, b);
}
 
template <class T>
void mini(T &a, T b)
{
    a = min(a, b);
}
 
template <class T>
void addi(T &a, T b)
{
    a = (a + b) % MOD;
}
 
template <class T>
void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}
 
template <class T>
T add(T a, T b)
{
    return (a + b) % MOD;
}
 
template <class T>
T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}
 
template <class T>
T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}
 
ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }
 
    return res;
}
 
const int INF = 1e9;
const int MAX_N = 1e5 + 2;
vector<VPL> graph(MAX_N);
 
ll cost[3][MAX_N];
ll dp[2][MAX_N];
 
// 0 - from u
// 1 - from v
// 2 - from s
 
void djikstra1(int start, int i)
{
    priority_queue<PLL, vector<PLL>, greater<PLL>> pq;
    fill(cost[i], cost[i] + MAX_N, LLONG_MAX / 3);
 
    pq.push({0, start});
    cost[i][start] = 0;
 
    while (!pq.empty())
    {
        auto [dist, node] = pq.top();
        pq.pop();
        for (auto [next, edge] : graph[node])
        {
            if (dist + edge < cost[i][next])
            {
                cost[i][next] = dist + edge;
                pq.push({cost[i][next], next});
            }
        }
    }
}
 
 
ll ans;
 
void djikstra2(int start, int end)
{
    priority_queue<PLL, vector<PLL>, greater<PLL>> pq;
 
    fill(dp[0], dp[0] + MAX_N, LLONG_MAX / 3);
    fill(dp[1], dp[1] + MAX_N, LLONG_MAX / 3);
 
    fill(cost[2], cost[2] + MAX_N, LLONG_MAX / 3);
 
    pq.push({0, start});
    cost[2][start] = 0;
 
    dp[0][start] = cost[0][start];
    dp[1][start] = cost[1][start];
 
    while (!pq.empty())
    {
        auto [dist, node] = pq.top();
        pq.pop();

        for (auto [next, edge] : graph[node])
        {
            ll would_be = dist + edge;
             
            if (would_be < cost[2][next])
            {
                cost[2][next] =would_be;
                 
                dp[0][next] = min(dp[0][node], cost[0][next]);
                dp[1][next] = min(dp[1][node], cost[1][next]);

                pq.push({cost[2][next], next});
            }
            else if (would_be == cost[2][next] &&
                     min(dp[0][node], cost[0][next]) + min(dp[1][node], cost[1][next]) <= dp[0][next] + dp[1][next])
            {
                dp[0][next] = min(dp[0][node], cost[0][next]);
                dp[1][next] = min(dp[1][node], cost[1][next]);
            }
        }
    }
 
    mini(ans, dp[0][end] + dp[1][end]);
}
 
void solve()
{
    int n, m, s, t, u, v;
    cin >> n >> m >> s >> t >> u >> v;
 
    FOR(i, 0, m)
    {
        int a, b, c;
        cin >> a >> b >> c;
 
        graph[a].PB({b, c});
        graph[b].PB({a, c});
    }
 
    djikstra1(u, 0);
    djikstra1(v, 1);
 
    ans = cost[0][v];
 
    djikstra2(s, t);
 
    djikstra2(t, s);
 
    cout << ans << LINE;
   
}
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int t = 1;
    // cin >> t;
 
    while (t--)
        solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...