이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
#define int 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;
}
vector<ll> dijkstra2(vector<ll> dist, vector<vector<pair<ll, ll>>> &adj){
int n = adj.size()-1;
priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
for(int i = 1; i <= n; i++) pq.push({dist[i], i});
while(!pq.empty()){
ll d_u = pq.top().first;
int u = pq.top().second;
pq.pop();
if(d_u != dist[u]) continue;
for(auto it : adj[u]){
int v = it.first;
ll w = it.second;
if(dist[u]+w < dist[v]){
dist[v] = dist[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(n+1), adj2(n+1);
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});
}
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, du2;
du1 = dijkstra(U, adj);
du1 = dijkstra2(du1, adj1);
du1 = dijkstra2(du1, adj);
du2 = dijkstra(U, adj);
du2 = dijkstra2(du2, adj2);
du2 = dijkstra2(du2, adj);
ll ans = INF;
ans = min(du1[V], du2[V]);
cout << ans << "\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("main.inp", "r", stdin);
//freopen("main.out", "w", stdout);
int t;
t = 1;
while(t--){
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... |