이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define f first
#define s second
const int INF = 2e9;
const int maxN = 1e5;
int N, M, S, T, U, V;
ll distS[maxN], distT[maxN], distU[maxN], distV[maxN], minU[maxN], minV[maxN];
int parent[maxN];
vector<pair<int, int>> adj[maxN];
void dijkstra(int s, ll (&dist)[maxN]) {
for (int i = 0; i < N; i++) {
dist[i] = LLONG_MAX;
}
dist[s] = 0;
priority_queue<pair<ll, int>> q;
q.push({0, s});
while(!q.empty()) {
ll cdist = -q.top().first;
int node = q.top().second;
q.pop();
if (cdist != dist[node]) continue;
for (pair<int, int> i : adj[node]) {
if (cdist + i.second < dist[i.first]) {
dist[i.first] = cdist + i.second;
q.push({-dist[i.first], i.first});
}
}
}
}
void dijkstra2(int s, ll (&dist)[maxN]) {
for (int i = 0; i < N; i++) {
dist[i] = LLONG_MAX;
}
dist[s] = 0;
priority_queue<pair<ll, int>> q;
q.push({0, s});
minU[s] = distU[s];
minV[s] = distV[s];
while(!q.empty()) {
ll cdist = -q.top().first;
int node = q.top().second;
q.pop();
if (cdist != dist[node]) continue;
for (pair<int, int> i : adj[node]) {
if (cdist + i.second < dist[i.first]) {
parent[i.first] = node;
dist[i.first] = cdist + i.second;
minU[i.first] = min(minU[node], distU[i.first]);
minV[i.first] = min(minV[node], distV[i.first]);
q.push({-dist[i.first], i.first});
}
else if (cdist + i.second == dist[i.first]) {
if (minU[node] + minV[node] < minU[i.first] + minV[i.first]) {
parent[i.first] = node;
minU[i.first] = min(minU[node], distU[i.first]);
minV[i.first] = min(minV[node], distV[i.first]);
}
}
}
}
}
int main() {
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
// freopen("pump.in", "r", stdin);
// freopen("pump.out", "w", stdout);
cin >> N >> M >> S >> T >> U >> V;
S--; T--; U--; V--;
for (int i = 0; i < M; i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a-1].pb({b-1, w});
adj[b-1].pb({a-1, w});
}
dijkstra(U, distU);
dijkstra(V, distV);
dijkstra2(S, distS);
vector<int> path;
int curr = T;
while (curr != S) {
curr = parent[curr];
path.pb(curr);
}
reverse(path.begin(), path.end());
ll pathToU, pathToV;
pathToU = pathToV = LLONG_MAX;
for (int i : path) {
pathToU = min(pathToU, distU[i]);
pathToV = min(pathToV, distV[i]);
}
cout << min(pathToU + pathToV, distU[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... |