이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
int n, m;
int s, t, u, v;
const int MXN = 100005;
vector<pair<int, int>> adj[MXN];
int distu[MXN], distv[MXN];
int dists[MXN], distt[MXN];
int dist[MXN];
bool access[MXN];
int result;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> proc;
void src(int no) {
for (int i=1; i<=n; i++) {
dist[i] = LLONG_MAX;
}
proc.push({0, no});
while (!proc.empty()) {
pair<int, int> tr = proc.top();
proc.pop();
int di = tr.first, no = tr.second;
if (dist[no] < di) continue;
dist[no] = di;
for (auto x: adj[no]) {
int ot = x.first;
int od = x.second + di;
if (dist[ot] > od) {
dist[ot] = od;
proc.push({od, ot});
}
}
}
}
int running_v[MXN];
int running_u[MXN];
bool byd(int a, int b) {
return dist[a] < dist[b];
}
void go(int from, int to) {
src(from);
vector<int> proc;
for (int i=1; i<=n; i++) proc.push_back(i);
sort(proc.begin(), proc.end(), byd);
for (auto no: proc) {
running_v[no] = distv[no];
running_u[no] = distu[no];
for (auto x: adj[no]) {
int ot = x.first;
int od = x.second;
if (dist[ot] + od == dist[no]) {
running_u[no] = min(running_u[no], running_u[ot]);
running_v[no] = min(running_v[no], running_v[ot]);
}
}
if (access[no]) {
result = min(result, running_u[no] + distv[no]);
result = min(result, distu[no] + running_v[no]);
}
}
}
signed main() {
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
for (int i=0; i<m; i++) {
int a, b, w;
cin >> a >> b >> w;
adj[a].push_back({b, w});
adj[b].push_back({a, w});
}
src(u);
for (int i=1; i<=n; i++) distu[i] = dist[i];
src(v);
for (int i=1; i<=n; i++) distv[i] = dist[i];
result = distu[v];
src(s);
for (int i=1; i<=n; i++) dists[i] = dist[i];
src(t);
for (int i=1; i<=n; i++) distt[i] = dist[i];
for (int i=1; i<=n; i++) {
if (dists[i] + distt[i] == dists[t]) {
access[i] = true;
}
}
go(s, t);
go(t, s);
cout << result << endl;
}
# | 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... |