제출 #930340

#제출 시각아이디문제언어결과실행 시간메모리
930340fskaricaCommuter Pass (JOI18_commuter_pass)C++14
31 / 100
385 ms44608 KiB
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define fi first
#define se second
#define pii pair<ll, ll>

const ll MAX = 4e5 + 10;
ll n, m;
ll s, t;
ll u, v;
ll x, y, w, w2;
vector <pii> veze[MAX];
ll dis[MAX][2];
ll sol[MAX][2];
ll arr[MAX][2];
ll trm[MAX][2];
priority_queue <pii> q;

void distance(ll root, ll o) {
    priority_queue <pii> q;

    q.push({0, root});
    dis[root][o] = 0;

    while (!q.empty()) {
        w = -q.top().fi;
        x = q.top().se;
        q.pop();

        if (dis[x][o] != w) continue;

        for (auto e : veze[x]) {
            y = e.fi;
            w2 = w + e.se;

            if (dis[y][o] != -1 && dis[y][o] <= w2) continue;

            dis[y][o] = w2;
            q.push({-w2, y});
        }
    }
}

void bfs(ll root, int o) {
    priority_queue <pii> q;

    q.push({0, root});
    arr[root][o] = 0;

    while (!q.empty()) {
        w = -q.top().fi;
        x = q.top().se;
        q.pop();

        if (arr[x][o] != w) continue;

        for (auto e : veze[x]) {
            y = e.fi;
            w2 = w + e.se;

            if (arr[y][o] != -1 && arr[y][o] <= w2) continue;

            arr[y][o] = w2;
            q.push({-w2, y});
        }
    }
}

void tramvaj(ll root, int o) {
    queue <ll> q;

    q.push(root);
    trm[root][o] = 1;

    while (!q.empty()) {
        x = q.front();
        q.pop();

        for (auto e : veze[x]) {
            if (arr[e.fi][o] + e.se != arr[x][o]) continue;
            if (trm[e.fi][o]) continue;

            trm[e.fi][o] = 1;
            q.push(e.fi);
        }
    }
}

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

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

    memset(dis, -1, sizeof(dis));
    memset(arr, -1, sizeof(arr));

    for (ll i = 0; i < m; i++) {
        cin >> x >> y >> w;

        veze[x].push_back({y, w});
        veze[y].push_back({x, w});
    }

    distance(u, 0);
    distance(v, 1);

    bfs(s, 0);
    bfs(t, 1);

    tramvaj(t, 0);
    tramvaj(s, 1);

    ll rez = 1e18;
    for (int i = 0; i < 2; i++) {
        for (ll j = 1; j <= n; j++) {
            if (!trm[j][i]) continue;

            sol[j][0] = dis[j][0];
            sol[j][1] = dis[j][1];
        }

        for (int j = 1; j <= n; j++) q.push({-sol[j][0], j});

        while (!q.empty()) {
            w = -q.top().fi;
            x = q.top().se;
            q.pop();

            if (w != sol[x][0]) continue;

            for (auto e : veze[x]) {
                if (arr[e.fi][i] != arr[x][i] + e.se) continue;
                if (sol[x][0] >= sol[e.fi][0]) continue;

                sol[e.fi][0] = sol[x][0];
                q.push({sol[e.fi][0], e.fi});
            }
        }

        for (int j = 1; j <= n; j++) q.push({-sol[j][1], j});
        while (!q.empty()) {
            w = -q.top().fi;
            x = q.top().se;
            q.pop();

            if (w != sol[x][1]) continue;

            for (auto e : veze[x]) {
                if (arr[e.fi][1 - i] != arr[x][1 - i] + e.se) continue;
                if (sol[x][1] >= sol[e.fi][1]) continue;

                sol[e.fi][1] = sol[x][1];
                q.push({sol[e.fi][1], e.fi});
            }
        }

//        cout << rez << " || ";
        rez = min(rez, dis[v][0]);
//        cout << rez << " || ";
        for (ll j = 1; j <= n; j++) {
            if (!trm[j][i]) continue;

//            cout << j << " j " << sol[j][0] + sol[j][1] << " | ";
//
            rez = min(rez, sol[j][0] + sol[j][1]);
//            cout << rez << " | ";
        }
//        cout << "\n";


//        cout << "___________________________________________\n";
//        cout << rez << "\n";
//
//        cout << "U dis[" << u << "] = "; for (ll j = 1; j <= n; j++) cout << dis[j][0] << " "; cout << "\n";
//        cout << "V dis[" << v << "] = "; for (ll j = 1; j <= n; j++) cout << dis[j][1] << " "; cout << "\n";
//        cout << "\n";
//
//        cout << "bfs -> "; for (ll j = 1; j <= n; j++) cout << arr[j][i] << " "; cout << "\n";
//        cout << "trm -> "; for (ll j = 1; j <= n; j++) cout << trm[j][i] << " "; cout << "\n";
//        cout << "\n";
//
//        cout << "0 sol -> "; for (ll j = 1; j <= n; j++) cout << sol[j][0] << " "; cout << "\n";
//        cout << "1 sol -> "; for (ll j = 1; j <= n; j++) cout << sol[j][1] << " "; cout << "\n";
//        cout << "\n";
//
//        cout << "___________________________________________\n";
    }

    cout << rez << "\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...