제출 #854966

#제출 시각아이디문제언어결과실행 시간메모리
854966abysmalCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
317 ms25108 KiB
#include<iostream> #include<stdio.h> #include<stdint.h> #include<iomanip> #include<algorithm> #include<utility> #include<vector> #include<stack> #include<queue> #include<set> #include<map> #include<deque> #include<math.h> #include<assert.h> #include<string.h> using namespace std; const int64_t INF = (int64_t) 1e18 + 100; const int64_t mINF = (int64_t) 1e9 + 10; const int64_t MOD = 998244353; const int64_t MOD2 = 998244353; const int nbit = 30; const int ndig = 10; const int nchar = 26; const int p1 = 31; const int p2 = 53; const int D = 4; int dr[D] = {-1, 0, 1, 0}; int dc[D] = {0, 1, 0, -1}; const int Dk = 8; int drk[Dk] = {2, 2, -2, -2, 1, 1, -1, -1}; int dck[Dk] = {1, -1, 1, -1, 2, -2, 2, -2}; struct Node { int u; int64_t w; Node(int u_, int64_t w_) : u(u_), w(w_) {} bool operator < (const Node& o) const { return w > o.w; } }; struct Node2 { int u; int p; int64_t w; Node2(int u_, int p_, int64_t w_) : u(u_), p(p_), w(w_) {} bool operator < (const Node2& o) const { return w > o.w; } }; struct Edge { int v; int w; Edge(int v_, int w_) : v(v_), w(w_) {} }; struct Solution { int n; int m; int64_t ans; int st; int ed; vector<int64_t> dis1; vector<int64_t> dis2; vector<vector<Edge> > adj; Solution() {} void solve() { int s; int t; cin >> n >> m >> s >> t >> st >> ed; s--; t--; st--; ed--; adj.resize(n); for(int i = 0; i < m; i++) { int u; int v; int w; cin >> u >> v >> w; u--; v--; adj[u].push_back(Edge(v, w)); adj[v].push_back(Edge(u, w)); } dis1.resize(n, INF); dis2.resize(n, INF); Dijkstra(st, dis1); Dijkstra(ed, dis2); ans = dis1[ed]; Dijkstra2(s, t); Dijkstra2(t, s); cout << ans << "\n"; } void Dijkstra2(int s, int t) { vector<bool> vis(n, false); vector<int64_t> dpu(n, INF); vector<int64_t> dpv(n, INF); vector<int64_t> dis(n, INF); dis[s] = 0; priority_queue<Node2> pq; pq.push(Node2(s, -1, 0)); while(!pq.empty()) { Node2 x = pq.top(); pq.pop(); int u = x.u; if(!vis[u]) { vis[u] = true; dis[u] = x.w; dpu[u] = dis1[u]; if(x.p != -1) dpu[u] = min(dpu[u], dpu[x.p]); dpv[u] = dis2[u]; if(x.p != -1) dpv[u] = min(dpv[u], dpv[x.p]); for(int i = 0; i < (int) adj[u].size(); i++) { int v = adj[u][i].v; int w = adj[u][i].w; pq.push(Node2(v, u, x.w + w)); } } else if(vis[u] && dis[u] == x.w) { int64_t t1 = INF; int64_t t2 = INF; if(x.p != -1) t1 = dpu[x.p]; if(x.p != -1) t2 = dpv[x.p]; if(min(dis1[u], t1) + min(dis2[u], t2) <= dpu[u] + dpv[u]) { dpu[u] = min(dis1[u], t1); dpv[u] = min(dis2[u], t2); } } } ans = min(ans, dpu[t] + dpv[t]); } void Dijkstra(int s, vector<int64_t>& dis) { dis[s] = 0; priority_queue<Node> pq; pq.push(Node(s, 0)); while(!pq.empty()) { Node x = pq.top(); pq.pop(); int u = x.u; if(dis[u] != x.w) continue; for(int i = 0; i < (int) adj[u].size(); i++) { int v = adj[u][i].v; int w = adj[u][i].w; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; pq.push(Node(v, dis[v])); } } } } int modadd(int t1, int t2) { int res = t1 + t2; if(res >= MOD) res -= MOD; return res; } int modsub(int t1, int t2) { int res = t1 - t2; if(res < 0) res += MOD; return res; } int modmul(int t1, int t2) { int64_t res = 1LL * t1 * t2; return res % MOD; } int Abs(int t1) { if(t1 < 0) return -t1; return t1; } int MASK(int j) { return (1 << j); } bool BIT(int t1, int j) { return (t1 & MASK(j)); } }; void __setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); } int main() { __setup(); int t = 1; // cin >> t; for(int i = 1; i <= t; i++) { Solution().solve(); } 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...