이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int maxn = 1e5 + 1;
int n, m, S, T, U, V;
ll dist[2][maxn], dist2[4][maxn];
vector <pair <int, ll>> adj[maxn];
set <pair <ll, int>> s;
set <pair <ll, pair <int, int>>> s2;
void dijkstra (int src, int x){
fill(dist[x], dist[x] + n + 1, 1e15);
dist[x][src] = 0;
s.insert({dist[x][src], src});
for (int i = 0; i < n; i++){
int v = (*s.begin()).second;
s.erase(s.begin());
for (auto [u, w] : adj[v]){
if (dist[x][u] <= dist[x][v] + w)
continue;
s.erase({dist[x][u], u});
dist[x][u] = dist[x][v] + w;
s.insert({dist[x][u], u});
}
if (!s.size())
break;
}
}
void dijkstra2 (){
for (int i = 0; i < 4; i++)
fill(dist2[i], dist2[i] + n + 1, 1e15);
dist2[0][U] = 0;
s2.insert({dist2[0][U], {U, 0}});
for (int i = 0; i < 4 * n; i++){
int v = (*s2.begin()).second.first, st = (*s2.begin()).second.second;
s2.erase(s2.begin());
for (auto [u, w] : adj[v]){
if (dist[0][v] + dist[1][u] + w == dist[0][T] && st != 3 && st != 2){
if (dist2[1][u] > dist2[st][v] + w){
s2.erase({dist2[1][u], {u, 1}});
dist2[1][u] = dist2[st][v];
s2.insert({dist2[1][u], {u, 1}});
}
}
else if (dist[0][u] + dist[1][v] + w == dist[0][T] && st != 3 && st != 1){
if (dist2[1][u] > dist2[st][v] + w){
s2.erase({dist2[2][u], {u, 2}});
dist2[2][u] = dist2[st][v];
s2.insert({dist2[2][u], {u, 2}});
}
if (dist2[st][u] > dist2[st][v] + w){
s2.erase({dist2[st][u], {u, st}});
dist2[st][u] = dist2[st][v] + w;
s2.insert({dist2[st][u], {u, st}});
}
}
else if (st == 1 || st == 2){
if (dist2[3][u] > dist2[st][v] + w){
s2.erase({dist2[3][u], {u, 3}});
dist2[3][u] = dist2[st][v] + w;
s2.insert({dist2[3][u], {u, 3}});
}
}
else if (dist2[st][u] > dist2[st][v] + w){
s2.erase({dist2[st][u], {u, st}});
dist2[st][u] = dist2[st][v] + w;
s2.insert({dist2[st][u], {u, st}});
}
}
if (!s2.size())
break;
}
}
int main (){
ios_base::sync_with_stdio(0);
cin >> n >> m >> S >> T >> U >> V;
for (int x, y, z, i = 0; i < m; i++){
cin >> x >> y >> z;
adj[x].pb({y, z});
adj[y].pb({x, z});
}
dijkstra(S, 0);
dijkstra(T, 1);
dijkstra2();
cout << min(dist2[0][V], min(dist2[1][V], min(dist2[2][V], dist2[3][V])));
}
# | 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... |