Submission #1166364

#TimeUsernameProblemLanguageResultExecution timeMemory
1166364merciless_lassieCommuter Pass (JOI18_commuter_pass)C++20
31 / 100
166 ms29772 KiB
#include "bits/stdc++.h"
using namespace std;
#define int long long

const int INF = 1e18;

int n, m;
int s, t, u, v;
vector<pair<int, int>> graph[100005];

// Dijkstra's algorithm to compute shortest paths from a source
vector<int> dijkstra(int src) {
    vector<int> dist(n + 1, INF);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    
    dist[src] = 0;
    pq.push({0, src});
    
    while (!pq.empty()) {
        int d = pq.top().first;
        int node = pq.top().second;
        pq.pop();
        
        if (d > dist[node]) continue;
        
        for (auto edge : graph[node]) {
            int next = edge.first;
            int weight = edge.second;
            
            if (dist[node] + weight < dist[next]) {
                dist[next] = dist[node] + weight;
                pq.push({dist[next], next});
            }
        }
    }
    
    return dist;
}

int solve() {
    // Compute shortest paths from S and T
    vector<int> dist_s = dijkstra(s);
    vector<int> dist_t = dijkstra(t);
    
    int shortest_s_to_t = dist_s[t]; // Shortest distance from S to T
    
    // Build a new graph where edges on any shortest path from S to T have cost 0
    vector<pair<int, int>> new_graph[100005];
    
    for (int i = 1; i <= n; i++) {
        for (auto [next, weight] : graph[i]) {
            // Check if this edge (i -> next) is on any shortest path from S to T
            bool on_shortest_path = (dist_s[i] + weight + dist_t[next] == shortest_s_to_t) || 
                                    (dist_s[next] + weight + dist_t[i] == shortest_s_to_t);
            
            // Add the edge to the new graph with appropriate cost
            new_graph[i].push_back({next, on_shortest_path ? 0 : weight});
        }
    }
    
    // Run Dijkstra from U to V on the new graph
    vector<int> dist(n + 1, INF);
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    
    dist[u] = 0;
    pq.push({0, u});
    
    while (!pq.empty()) {
        int d = pq.top().first;
        int node = pq.top().second;
        pq.pop();
        
        if (d > dist[node]) continue;
        
        for (auto [next, weight] : new_graph[node]) {
            if (dist[node] + weight < dist[next]) {
                dist[next] = dist[node] + weight;
                pq.push({dist[next], next});
            }
        }
    }
    
    return dist[v];
}

signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n >> m;
    cin >> s >> t >> u >> v;
    
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        graph[a].push_back({b, c});
        graph[b].push_back({a, c});
    }
    
    cout << solve() << 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...