이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
vector<ll> dijkstra(int s, vector<vector<pair<ll, ll>>> &adj){
int n = adj.size();
vector<ll> dist(n+1, INF);
dist[s] = 0;
vector<bool> vis(n+1, false);
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
pq.push({0, s});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(vis[u]){
continue;
}
vis[u] = true;
for(auto it : adj[u]){
int v = it.first;
ll w = it.second;
if(vis[v]) continue;
dist[v] = min(dist[v], d_u+w);
pq.push({dist[v], v});
}
}
return dist;
}
void solve(){
int n, m;
cin >> n >> m;
int S, T, U, V;
cin >> S >> T;
cin >> U >> V;
vector<vector<pair<ll, ll>>> adj(n+1), adj1, adj2;
for(int i = 1; i <= m; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
adj1 = adj;
adj2 = adj;
vector<ll> ds = dijkstra(S, adj), dt = dijkstra(T, adj);
for(int u = 1; u <= n; u++){
if(ds[u]+dt[u] != ds[T]) continue;
for(auto it : adj[u]){
int v = it.first;
ll w = it.second;
if(ds[v]+dt[v] != ds[T]) continue;
if(ds[u]+w == ds[v]){
adj1[u].push_back({v, 0});
}
if(dt[u]+w == dt[v]){
adj2[u].push_back({v, 0});
}
}
}
vector<ll> du1 = dijkstra(U, adj1), du2 = dijkstra(U, adj2), dv = dijkstra(V, adj);
ll ans = INF;
for(int i = 1; i <= n; i++){
ans = min(ans, du1[i]+dv[i]);
ans = min(ans, du2[i]+dv[i]);
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |