이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 100100;
vector<pair<int, int>> g[N];
void dkstr(int u, vector<ll> &d) {
fill(d.begin(), d.end(), 1e15);
d[u] = 0;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> s;
s.push({0, u});
while(s.size()) {
int v = s.top().second;
ll f = s.top().first;
s.pop();
if(d[v] != f) continue;
for(auto to : g[v]) {
if(d[to.first] > d[v] + to.second) {
d[to.first] = d[v] + to.second;
s.push({d[to.first], to.first});
}
}
}
}
int main() {
int n, m, s, t, u, v; cin >> n >> m;
cin >> s >> t >> u >> v;
for(int i = 1; i <= m; i++) {
int x, y, w; cin >> x >> y >> w;
g[x].emplace_back(y, w);
g[y].emplace_back(x, w);
}
vector<ll> fs(n + 1), ft(n + 1), fu(n + 1), fv(n + 1);
dkstr(s, fs);
dkstr(t, ft);
dkstr(u, fu);
dkstr(v, fv);
ll ans = fu[v];
for(int it = 0; it < 2; it++) {
vector<ll> mn(n + 1, 1e15), f(n + 1, 1e15);
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> d;
d.push({0, s});
f[s] = 0;
mn[s] = fu[s];
while(d.size()) {
int x = d.top().second;
ll y = d.top().first;
d.pop();
if(f[x] != y) continue;
ans = min(ans, mn[x] + fv[x]);
for(auto to : g[x]) {
if(fs[to.first] + ft[to.first] == fs[t]) {
mn[to.first] = min(mn[to.first], min(mn[x], fu[to.first]));
if(f[to.first] > to.second + y) {
f[to.first] = to.second + y;
d.push({f[to.first], to.first});
}
}
}
}
swap(fu, fv);
}
cout << ans;
}
# | 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... |