이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
ll MAX = 1e14 * 2;
int N, M, S, T, U, V;
vector<vector<pair<int,ll>>> adj;
vector<vector<int>> trajects;
vector<bool> processed;
vector<ll> memo;
ll dp (int pos) {
if (pos == T) return memo[pos] = 0;
if (memo[pos] != -1) return memo[pos];
processed[pos] = true;
ll ans = MAX + 1;
for (auto x : adj[pos]) {
if (!processed[x.fi]) {
ans = min(ans, dp(x.fi) + x.se);
}
}
processed[pos] = false;
if (ans <= MAX) return memo[pos] = ans;
else return ans;
}
void res(int pos, ll cost, vector<int> *current) {
if (pos == T) {
return;
}
vector<int> copy = *current;
processed[pos] = true;
bool already = false;
for (auto x : adj[pos]) {
if (!processed[x.fi]) {
if (memo[x.fi] + x.se + cost == memo[S]) {
if (already) {
trajects.push_back({});
trajects.back() = copy;
trajects.back().push_back(x.fi);
res(x.fi, cost + x.se, &trajects.back());
}
else {
current->push_back(x.fi);
res(x.fi, cost + x.se, current);
already = true;
}
}
}
}
processed[pos] = false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> N >> M >> S >> T >> U >> V;
S--, T--, U--, V--;
adj.resize(N); memo.resize(N, -1); processed.resize(N, false);
for (int i = 0; i < M; i++) {
int a, b; ll c; cin >> a >> b >> c;
a--, b--;
adj[a].push_back({b,c});
adj[b].push_back({a,c});
}
dp(S);
trajects.push_back({S});
res(S, 0, &trajects.back());
vector<ll> distU(N, MAX), distV(N, MAX);
priority_queue < pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll,int>>> q;
q.push({0,U}); distU[U] = 0;
while (!q.empty()) {
int x = q.top().se; q.pop();
if (processed[x]) continue;
processed[x] = true;
for (auto w : adj[x]) {
if (distU[w.fi] > distU[x] + w.se) {
distU[w.fi] = distU[x] + w.se;
q.push({distU[w.fi], w.fi});
}
}
}
processed.clear(); processed.resize(N, false);
q.push({0, V}); distV[V] = 0;
while (!q.empty()) {
int x = q.top().se; q.pop();
if (processed[x]) continue;
processed[x] = true;
for (auto w : adj[x]) {
if (distV[w.fi] > distV[x] + w.se) {
distV[w.fi] = distV[x] + w.se;
q.push({distV[w.fi], w.fi});
}
}
}
ll ans = distU[V];
for (auto x : trajects) {
ll miniU = MAX, miniV = MAX;
for (int y : x) {
miniU = min(miniU, distU[y]);
miniV = min(miniV, distV[y]);
ans = min(ans, miniV + miniU);
}
}
cout << ans << '\n';
return 0;
}
# | 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... |