이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fi first
#define se second
#define int long long
using namespace std;
typedef pair<int, int> pii;
const long long OO = 1e18;
const int INF = 1e9;
const int MX = 200005;
int n, m, S, T, U, V;
vector<pii> G[MX];
void nhap(){
cin >> n >> m;
cin >> S >> T;
cin >> U >> V;
for(int i=1; i<=m; i++){
int u, v, c;
cin >> u >> v >> c;
G[u].push_back({v, c});
G[v].push_back({u, c});
}
}
void dijkstra(int source, vector<int> &d){
d[source] = 0;
priority_queue<pair<int, int>> Q;
Q.push({0, source});
while(Q.size()){
int du, u;
tie(du, u) = Q.top();
du = -du;
Q.pop();
if(du!=d[u]) continue;
for(pii &tmp: G[u]){
int v, c;
tie(v, c) = tmp;
if(d[v]>du+c){
d[v] = du + c;
Q.push({-d[v], v});
}
}
}
}
void sub1(){
vector<int> dS(n+5, OO);
vector<int> dT(n+5, OO);
vector<int> dV(n+5, OO);
dijkstra(S, dS);
dijkstra(T, dT);
dijkstra(V, dV);
int ans = dS[V];
for(int i=1; i<=n; i++){
if(dS[i] + dT[i] == dS[T]){
ans = min(ans, dV[i]);
}
}
cout << ans;
}
vector<int> rG[MX];
void sub3(){
vector<int> dS(n+5, OO);
vector<int> dT(n+5, OO);
vector<int> dU(n+5, OO);
vector<int> dV(n+5, OO);
/// thieu S
dijkstra(T, dT);
dijkstra(U, dU);
dijkstra(V, dV);
dS[S] = 0;
priority_queue<pair<int, int>> Q;
Q.push({0, S});
while(Q.size()){
int du, u;
tie(du, u) = Q.top();
du = -du;
Q.pop();
if(du!=dS[u]) continue;
for(pii &tmp: G[u]){
int v, c;
tie(v, c) = tmp;
if(dS[v]>du+c){
dS[v] = du + c;
Q.push({-dS[v], v});
rG[v].clear();
rG[v].push_back(u);
}
else if(dS[v]==du+c) rG[v].push_back(u);
}
}
int ans = dU[V];
vector<int> d(n+5, OO);
d[T] = dU[T];
Q.push({-d[T], T});
while(Q.size()){
int du, u;
tie(du, u) = Q.top();
du = -du;
Q.pop();
if(du!=d[u]) continue;
ans = min(ans, du + dV[u]);
for(int v: rG[u]){
if(d[v]>min(du, dU[v])){
d[v] = min(du, dU[v]);
Q.push({-d[v], v});
}
}
}
d = vector<int>(n+5, OO);
d[T] = dV[T];
Q.push({-d[T], T});
while(Q.size()){
int du, u;
tie(du, u) = Q.top();
du = -du;
Q.pop();
if(du!=d[u]) continue;
ans = min(ans, du + dU[u]);
for(int v: rG[u]){
if(d[v]>min(du, dV[v])){
d[v] = min(du, dV[v]);
Q.push({-d[v], v});
}
}
}
cout << ans;
}
void solve(){
if(S==U){
sub1();
return;
}
sub3();
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
nhap();
solve();
return 0;
}
/**
11 12
1 8
11 2
1 2 9
1 4 8
2 3 7
3 10 3
3 11 3
4 5 5
4 6 1
4 10 5
5 7 1
7 11 0
7 8 5
7 9 8
**/
# | 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... |