Submission #1262914

#TimeUsernameProblemLanguageResultExecution timeMemory
1262914chikien2009Commuter Pass (JOI18_commuter_pass)C++20
100 / 100
196 ms15536 KiB
#include <bits/stdc++.h>

using namespace std;

void setup()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
}

int s, t, u, v, a, b, n, m;
bool check[100000];
long long d[6][100000], c, res = 1e18;
vector<pair<int, int>> g[100000];
priority_queue<pair<long long, int>> pq;

inline void Dijkstra(int sp, long long (&dist)[100000])
{
    fill_n(dist, 100000, 1e18);
    dist[sp] = 0;
    pq.push({-dist[sp], sp});
    while (!pq.empty())
    {
        c = -pq.top().first;
        a = pq.top().second;
        pq.pop();
        if (c != dist[a])
        {
            continue;
        }
        for (auto &i : g[a])
        {
            if (dist[i.first] > dist[a] + i.second)
            {
                dist[i.first] = dist[a] + i.second;
                pq.push({-dist[i.first], i.first});
            }
        }
    }
}

int main()
{
    setup();

    cin >> n >> m >> s >> t >> u >> v;
    s--;
    t--;
    u--;
    v--;
    while (m--)
    {
        cin >> a >> b >> c;
        g[a - 1].push_back({b - 1, c});
        g[b - 1].push_back({a - 1, c});
    }
    Dijkstra(s, d[0]);
    Dijkstra(t, d[1]);
    Dijkstra(u, d[2]);
    Dijkstra(v, d[3]);
    for (int i = 0; i < n; ++i)
    {
        pq.push({-d[0][i], i});
    }
    while (!pq.empty())
    {
        a = pq.top().second;
        pq.pop();
        check[a] = true;
        d[4][a] = d[2][a];
        d[5][a] = d[3][a];
        if (d[0][a] + d[1][a] == d[0][t])
        {
            for (auto &i : g[a])
            {
                if (d[0][i.first] + i.second == d[0][a] && check[i.first])
                {
                    d[4][a] = min(d[4][a], d[4][i.first]);
                    d[5][a] = min(d[5][a], d[5][i.first]);
                }
            }
        }
        res = min({res, d[4][a] + d[3][a], d[2][a] + d[5][a]});
    }
    cout << res;
    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...