Submission #1265180

#TimeUsernameProblemLanguageResultExecution timeMemory
1265180andyoCommuter Pass (JOI18_commuter_pass)C++20
31 / 100
334 ms33928 KiB
#include <iostream> #include <vector> #include <queue> #include <algorithm> 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; // Store graph edges vector<vector<pair<int, ll>>> graph(n + 1); vector<tuple<int, int, ll>> railways(m); for (int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; railways[i] = {a, b, c}; graph[a].push_back({b, c}); graph[b].push_back({a, c}); } // Dijkstra function to compute shortest distances 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, weight] : graph[node]) { if (dist[node] + weight < dist[next]) { dist[next] = dist[node] + weight; pq.push({dist[next], next}); } } } }; // Compute distances from S to all nodes vector<ll> dist_from_s(n + 1); dijkstra(s, dist_from_s); // Compute distances from all nodes to T vector<ll> dist_to_t(n + 1); dijkstra(t, dist_to_t); // Find shortest path distance from S to T ll min_st_dist = dist_from_s[t]; // Create a new graph where railways on min-cost S-T paths have cost 0 vector<vector<pair<int, ll>>> modified_graph(n + 1); for (auto [a, b, c] : railways) { // Check if this railway is on some minimum cost path from S to T bool on_min_path = (dist_from_s[a] + c + dist_to_t[b] == min_st_dist) || (dist_from_s[b] + c + dist_to_t[a] == min_st_dist); // If on minimum path, cost is 0; otherwise, original cost ll new_cost = on_min_path ? 0 : c; modified_graph[a].push_back({b, new_cost}); modified_graph[b].push_back({a, new_cost}); } // Find shortest path from U to V in the modified graph vector<ll> dist_from_u(n + 1, INF); dist_from_u[u] = 0; priority_queue<pli, vector<pli>, greater<pli>> pq; pq.push({0, u}); while (!pq.empty()) { auto [d, node] = pq.top(); pq.pop(); if (d > dist_from_u[node]) continue; for (auto [next, weight] : modified_graph[node]) { if (dist_from_u[node] + weight < dist_from_u[next]) { dist_from_u[next] = dist_from_u[node] + weight; pq.push({dist_from_u[next], next}); } } } // Output the minimum cost from U to V cout << dist_from_u[v] << 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...