제출 #406501

#제출 시각아이디문제언어결과실행 시간메모리
406501FalconCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
375 ms27460 KiB
#include <bits/stdc++.h> #ifdef DEBUG #include "debug.hpp" #endif using namespace std; #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it) #define rep(i, N) for(int i = 0; i < (N); ++i) #define rrep(i, N) for(int i = (N) - 1; i >= 0; --i) #define rep1(i, N) for(int i = 1; i <= (N); ++i) #define rep2(i, s, e) for(int i = (s); i <= (e); ++i) #ifdef DEBUG #define debug(x...) { \ ++dbg::depth; \ string dbg_vals = dbg::to_string(x); \ --dbg::depth; \ dbg::fprint(__func__, __LINE__, #x, dbg_vals); \ } #define light_debug(x) { \ dbg::light = true; \ dbg::dout << __func__ << ":" << __LINE__; \ dbg::dout << " " << #x << " = " << x << endl; \ dbg::light = false; \ } #else #define debug(x...) 42 #define light_debug(x) 42 #endif using ll = long long; template<typename T> inline T& ckmin(T& a, T b) { return a = a > b ? b : a; } template<typename T> inline T& ckmax(T& a, T b) { return a = a < b ? b : a; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int N, M, S, T, U, V; cin >> N >> M >> S >> T >> U >> V; --S, --T, --U, --V; vector<vector<pair<int, long long>>> adj(N); rep(i, M) { int u, v; long long w; cin >> u >> v >> w; --u, --v; adj[u].push_back({v, w}), adj[v].push_back({u, w}); } auto dijkstra = [&](int s) -> vector<long long> { vector<long long> dist(N, -1); priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq; pq.push({0, s}); while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(~dist[u]) continue; dist[u] = d; for(auto [v, w]: adj[u]) if(dist[v] == -1) pq.push({d + w, v}); } return dist; }; vector<long long> dist_u = dijkstra(U), dist_v = dijkstra(V); auto dijkstra2 = [&]() -> long long { struct dist_t { long long d_s, d_u, d_v; bool operator >(const dist_t& d) const { return d_s > d.d_s || (d_s == d.d_s && d_u + d_v > d.d_u + d.d_v); } }; auto add_edge = [&](dist_t d, int u, int v, long long w) -> dist_t { long long m_u = min({dist_u[u], dist_u[v], d.d_u}); long long m_v = min({dist_v[u], dist_v[v], d.d_v}); return {d.d_s += w, m_u, m_v}; }; auto comp = [](const pair<dist_t, int>& a, const pair<dist_t, int>& b) { if(a.first > b.first) return true; if(b.first > a.first) return false; return a.second > b.second; }; vector<dist_t> dist(N); vector<bool> vis(N); priority_queue<pair<dist_t, int>, vector<pair<dist_t, int>>, decltype(comp)> pq(comp); pq.push({dist_t{0, dist_u[S], dist_v[S]}, S}); while(!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if(vis[u]) continue; vis[u] = true; dist[u] = d; for(auto [v, w]: adj[u]) if(!vis[v]) pq.push({add_edge(d, u, v, w), v}); } return dist[T].d_u + dist[T].d_v; }; cout << min(dijkstra2(), dist_u[V]) << '\n'; #ifdef DEBUG dbg::dout << "\nExecution time: " << clock() * 1000 / CLOCKS_PER_SEC << "ms" << endl; #endif 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...