Submission #1093545

#TimeUsernameProblemLanguageResultExecution timeMemory
1093545anhthiCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
296 ms30440 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define ll long long
#define mp(x, y) make_pair(x, y)
#define sz(v) ((int) (v).size())
#define all(v) (v).begin(), (v).end()
#define MASK(i) (1LL << (i))
#define BIT(x, y) (((x) >> (y)) & 1)
#define pb push_back
#define max_rng *max_element
#define min_rng *min_element
#define rep(i, n) for(int i = 1, _n = (n); i <= _n; ++i)
#define forn(i, a, b) for(int i = (a), _b = (b); i <= _b; ++i)
#define ford(i, a, b) for(int i = (a), _b = (b); i >= _b; --i)

template <class X, class Y>
    inline bool maximize(X &x, Y y) {
        return (x < y ? x = y, true : false);
    }

template <class X, class Y>
    inline bool minimize(X &x, Y y) {
        return (x > y ? x = y, true : false);
    }

template <class X>
    inline void compress(vector<X> &a) {
        sort(all(a));
        a.resize(unique(all(a)) - a.begin());
    }

int ctz(ll x) { return __builtin_ctzll(x); }
int lg(ll x) { return 63 - __builtin_clzll(x); }
int popcount(ll x) { return __builtin_popcount(x); }

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) {
    return l + abs((ll) rng()) % (r - l + 1);
}

const ll oo = (ll) 1e17;
const int inf = (ll) 2e9 + 7, mod = (ll) 1e9 + 7;

const int mxn = 2e5 + 5, mxm = 3e2 + 5;
const int LG = 18, BASE = 311, BLOCK = 350;

void add(int &x, int y) { x += y; if (x >= mod) x -= mod; }
void sub(int &x, int y) { x -= y; if (x < 0) x += mod; }

int n, m;
int s, t, u, v;

struct Edge {
    int u, v, w;
} e[mxn];
vector<pair<int, int>> g[mxn];

vector<int> dag[mxn];
ll dist_u[mxn], dist_v[mxn];

void calc(int s, ll dp[]) {
    fill(dp, dp + n + 1, oo);

    priority_queue<pair<ll, int>> pq;

    dp[s] = 0;
    pq.push(mp(0, s));

    while (!pq.empty()) {
        pair<ll, int> top = pq.top(); pq.pop();
        int u = top.se; ll dist = -top.fi;

        if (dp[u] != dist) continue;

        for (pair<int, int> i : g[u]) {
            ll d = dist + e[i.second].w;
            if (minimize(dp[i.first], d)) {
                pq.push(mp(-d, i.first));
            }
        }
    }

    return;
}
ll f[mxn];
struct Path {
    ll dist;
    int u, par;
    bool operator < (const Path &p) const {
        return dist > p.dist;
    }
};
array<ll, 2> dp[mxn];
ll dijkstra(int s, int t) {

    memset(f, 0x3f, sizeof f);

    rep(i, n) {
        dp[i][0] = dist_u[i];
        dp[i][1] = dist_v[i];
    }

    priority_queue<array<ll, 2>> pq;

    f[s] = 0;
    pq.push({0, s});

    while (sz(pq)) {
        array<ll, 2> top = pq.top(); pq.pop();
        int u = top[1];
        ll dist = -top[0];

        if (f[u] != dist) continue;

        for (pair<int, int> i : g[u]) {

            int v = i.first;
            ll d = dist + e[i.second].w;

            if (f[v] < d) continue;

            if (min(dist_u[v], dp[u][0]) + min(dist_v[v], dp[u][1])
                < dp[v][0] + dp[v][1]) {
                dp[v][0] = min(dist_u[v], dp[u][0]);
                dp[v][1] = min(dist_v[v], dp[u][1]);
            }

            if (minimize(f[v], d)) {
                dp[v][0] = min(dist_u[v], dp[u][0]);
                dp[v][1] = min(dist_v[v], dp[u][1]);
                pq.push({-d, v});
            }

        }

    }

    return dp[t][0] + dp[t][1];
}

int main(void) {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define TASK "cquery"
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }

    cin >> n >> m;
    cin >> s >> t >> u >> v;

    rep(i, m) {
        int u, v, w;
        cin >> u >> v >> w;
        e[i] = {u, v, w};
        g[u].pb(mp(v, i));
        g[v].pb(mp(u, i));
    }

    calc(u, dist_u);
    calc(v, dist_v);

    ll ans = dist_u[v];
    minimize(ans, dijkstra(s, t));
    minimize(ans, dijkstra(t, s));

    cout << ans;


    return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  152 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:153:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  153 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...