Submission #1141142

#TimeUsernameProblemLanguageResultExecution timeMemory
1141142buzdiCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
418 ms21284 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define ll long long #define ld long double using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; const int NMAX = 1e5; const ll INF = 1e18; struct PQElement { int node, parent; ll d; bool operator < (const PQElement &other) const { return other.d < d; } }; int n, m, s, t, x, y; ll answer; vector<pair<int, int>> g[NMAX + 1]; vector<int> g1[NMAX + 1]; int indegree[NMAX + 1]; ll dp[NMAX + 1]; ll d[4][NMAX + 1]; void Dijkstra(int start, int which_start) { for(int i = 1; i <= n; i++) { d[which_start][i] = INF; } priority_queue<PQElement> pq; pq.push({start, 0, 0}); while(!pq.empty()) { auto [node, parent, curent_d] = pq.top(); pq.pop(); if(d[which_start][node] == INF) { d[which_start][node] = curent_d; for(auto [next_node, edge_d] : g[node]) { pq.push({next_node, node, curent_d + edge_d}); } } } } // void DijkstraDP(int start, int finish) { // for(int i = 0; i <= n; i++) { // d[2][i] = INF; // dp[i] = INF; // } // priority_queue<PQElement> pq; // pq.push({start, 0, 0}); // d[2][start] = 0; // dp[start] = d[0][start]; // while(!pq.empty()) { // auto [node, parent, curent_d] = pq.top(); // pq.pop(); // answer = min(answer, dp[node] + d[1][node]); // if(node == finish) { // return; // } // if(d[2][node] != curent_d) { // continue; // } // for(auto [next_node, edge_d] : g[node]) { // int next_d = curent_d + edge_d; // if(next_d < d[2][next_node]) { // d[2][next_node] = next_d; // dp[next_node] = min(dp[node], d[0][next_node]); // pq.push({next_node, node, next_d}); // } // else if(next_d == d[2][next_node]) { // dp[next_node] = min(dp[next_node], dp[node]); // } // } // } // } void SolveDP0() { for(int i = 1; i <= n; i++) { indegree[i] = 0; } for(int i = 1; i <= n; i++) { for(auto [j, edge_d] : g[i]) { if(d[2][i] + edge_d + d[3][j] == d[2][t]) { g1[i].push_back(j); indegree[j]++; } } } for(int i = 1; i <= n; i++) { dp[i] = d[0][i]; } queue<int> q; for(int i = 1; i <= n; i++) { if(!indegree[i]) { q.push(i); } } while(!q.empty()) { int node = q.front(); q.pop(); answer = min(answer, dp[node] + d[1][node]); for(int next_node : g1[node]) { indegree[next_node]--; dp[next_node] = min(dp[next_node], dp[node]); if(!indegree[next_node]) { q.push(next_node); } } } } void SolveDP1() { for(int i = 1; i <= n; i++) { indegree[i] = 0; } for(int i = 1; i <= n; i++) { for(auto [j, edge_d] : g[i]) { if(d[3][i] + edge_d + d[2][j] == d[3][s]) { g1[i].push_back(j); indegree[j]++; } } } for(int i = 1; i <= n; i++) { dp[i] = d[0][i]; } queue<int> q; for(int i = 1; i <= n; i++) { if(!indegree[i]) { q.push(i); } } while(!q.empty()) { int node = q.front(); q.pop(); answer = min(answer, dp[node] + d[1][node]); for(int next_node : g1[node]) { indegree[next_node]--; dp[next_node] = min(dp[next_node], dp[node]); if(!indegree[next_node]) { q.push(next_node); } } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> s >> t >> x >> y; for(int i = 1; i <= m; i++) { int a, b, c; cin >> a >> b >> c; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } Dijkstra(x, 0); Dijkstra(y, 1); Dijkstra(s, 2); Dijkstra(t, 3); answer = INF; SolveDP0(); SolveDP1(); cout << answer << '\n'; 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...