Submission #679437

#TimeUsernameProblemLanguageResultExecution timeMemory
679437kussssoCommuter Pass (JOI18_commuter_pass)C++17
0 / 100
6 ms5076 KiB
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
const int N = 1e5 + 5;
const ll inf = 1e18;
typedef pair<int, ll> edge;
#define fi first
#define se second

struct Node {
    ll fu;
    int u;
    bool hu, hv;
    bool operator < (const Node& oth) const {
        return fu > oth.fu;
    }
};

int n, m;
int S, T, U, V;
ll f[N][2][2], ans;
vector<edge> g[N];
vector<int> pa[N];
vector<ll> du, dv, ds, dt;

vector<ll> Dij (int st) {
    for (int i = 1; i <= n; i++) pa[i].clear();
    vector<ll> d(n + 1, inf);
    d[st] = 0;
    priority_queue<pair<ll, int>> pq;
    pq.push({0, st});
    while (!pq.empty()) {
        ll du = -pq.top().fi;
        int u = pq.top().se;
        pq.pop();
        if (d[u] != du) continue;
        for (auto e : g[u]) {
            int v = e.fi;
            ll w = e.se;
            if (d[v] > d[u] + w) {
                d[v] = d[u] + w;
                pq.push({-d[v], v});
                pa[v] = {u};
            }
            else if (d[v] == d[u] + w) {
                pa[v].push_back(u);
            }
        }
    }
    return d;
}

ll dp1[N], dp2[N];
bool vis[N];

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    freopen("vl.inp", "r", stdin);
    freopen("vl.out", "w", stdout);

    cin >> n >> m >> S >> T >> 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});
    }
    du = Dij(U);
    dv = Dij(V);
    ds = Dij(S);
    dt = Dij(T);

    ans = du[V];

    for (int i = 0; i <= n; i++) {
        for (int j = 0; j < 2; j++) {
            for (int x = 0; x < 2; x++) {
                f[i][j][x] = inf;
            }
        }
    }

    priority_queue<Node> pq;
    pq.push({0, S, 0, 0});
    f[S][0][0] = 0;

    while (!pq.empty()) {
        auto [fu, u, hu, hv] = pq.top(); pq.pop();
        if (fu != f[u][hu][hv]) continue;
        bool nhu = hu | (u == U);
        bool nhv = hv | (u == V);
        if (!nhu) {
            if (f[u][1][nhv] > fu + du[u]) {
                f[u][1][nhv] = fu + du[u];
                pq.push({f[u][1][nhv], u, 1, nhv});
            }
        }
        if (!nhv) {
            if (f[u][nhu][1] > fu + dv[u]) {
                f[u][nhu][1] = fu + dv[u];
                pq.push({f[u][nhu][1], u, nhu, 1});
            }
        }
        if (!nhu && !nhv) {
            if (f[u][1][1] > fu + du[u] + dv[u]) {
                f[u][1][1] = fu + du[u] + dv[u];
                pq.push({f[u][1][1], u, 1, 1});
            }
        }
        for (int v : pa[u]) {
            if (f[v][nhu][nhv] > fu) {
                f[v][nhu][nhv] = fu;
                pq.push({f[v][nhu][nhv], v, nhu, nhv});
            }
        }
    }
    ans = min(ans, f[T][1][1]);

    cout << ans;

    return 0;
}

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:61:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   61 |     freopen("vl.inp", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:62:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   62 |     freopen("vl.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...