Submission #1166334

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

const int N = 1e5 + 5;
const int INF = 1e18;

int n, m;
int s, t, u, v;
vector<pair<int, int>> graph[N];
map<pair<int, int>, bool> is_on_shortest_path;

// Find shortest path from src to all nodes
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() {
    // Get shortest paths
    vector<int> dist_from_s = dijkstra(s);
    vector<int> dist_from_t = dijkstra(t);
    
    int shortest_s_to_t = dist_from_s[t];
    
    // Mark edges on any shortest path from s to t
    for (int i = 1; i <= n; i++) {
        for (auto edge : graph[i]) {
            int j = edge.first;
            int weight = edge.second;
            
            // Check if this edge is part of any shortest path from s to t
            if (dist_from_s[i] + weight + dist_from_t[j] == shortest_s_to_t ||
                dist_from_s[j] + weight + dist_from_t[i] == shortest_s_to_t) {
                // Store both edge directions
                if (i < j) {
                    is_on_shortest_path[{i, j}] = true;
                } else {
                    is_on_shortest_path[{j, i}] = true;
                }
            }
        }
    }
    
    // Find shortest path from u to v with free edges
    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 edge : graph[node]) {
            int next = edge.first;
            int weight = edge.second;
            
            // Create a canonical edge representation (smaller node first)
            pair<int, int> edge_key = (node < next) ? make_pair(node, next) : make_pair(next, node);
            
            // If edge is on shortest path, cost is 0, otherwise it's the original weight
            int cost = is_on_shortest_path.count(edge_key) ? 0 : weight;
            
            if (dist[node] + cost < dist[next]) {
                dist[next] = dist[node] + cost;
                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...