Submission #1265182

#TimeUsernameProblemLanguageResultExecution timeMemory
1265182andyoCommuter Pass (JOI18_commuter_pass)C++20
0 / 100
2095 ms19380 KiB
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

typedef long long ll;
typedef pair<ll, int> pli;

const ll INF = 1e18;

int main() {
    int n, m;
    cin >> n >> m;
    
    int s, t, u, v;
    cin >> s >> t;
    cin >> u >> v;
    
    vector<vector<pair<int, ll>>> graph(n + 1);
    
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }
    
    // Compute shortest paths
    auto dijkstra = [&](int start, vector<ll>& dist) {
        dist.assign(n + 1, INF);
        dist[start] = 0;
        priority_queue<pli, vector<pli>, greater<pli>> pq;
        pq.push({0, start});
        
        while (!pq.empty()) {
            auto [d, node] = pq.top(); pq.pop();
            
            if (d > dist[node]) continue;
            
            for (auto [next, cost] : graph[node]) {
                if (dist[node] + cost < dist[next]) {
                    dist[next] = dist[node] + cost;
                    pq.push({dist[next], next});
                }
            }
        }
    };
    
    vector<ll> dist_s(n + 1), dist_t(n + 1), dist_u(n + 1), dist_v(n + 1);
    dijkstra(s, dist_s);
    dijkstra(t, dist_t);
    dijkstra(u, dist_u);
    dijkstra(v, dist_v);
    
    ll answer = dist_u[v]; // Direct cost without commuter pass
    
    // For each pair of nodes, consider them as part of the commuter pass route
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            // Skip if unreachable
            if (dist_s[i] == INF || dist_t[j] == INF) continue;
            
            // Cost of commuter pass S→i→j→T
            ll pass_cost = dist_s[i] + dist_t[j];
            
            // Cost from U to V when going through i and j
            // (assuming those edges are covered by the pass)
            if (dist_u[i] != INF && dist_v[j] != INF) {
                answer = min(answer, dist_u[i] + dist_v[j]);
            }
            
            // Also consider the reverse direction
            if (dist_u[j] != INF && dist_v[i] != INF) {
                answer = min(answer, dist_u[j] + dist_v[i]);
            }
        }
    }
    
    cout << answer << endl;
    
    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...