이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
int a[4];
bool vis[100005];
ll dist[4][100005], b[4][100005];
vector<pair<int, ll>> adj[100005];
priority_queue<pair<ll, int>> pq;
void bfs(int type) {
pq.push({0, a[type]});
while (!pq.empty()) {
int x = pq.top().se;
ll d = -pq.top().fi;
pq.pop();
if (dist[type][x] != d) continue;
for (auto u : adj[x]) if (d + u.se < dist[type][u.fi]) dist[type][u.fi] = d + u.se, pq.push({-(d + u.se), u.fi});
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < 4; i++) cin >> a[i];
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= n; j++) {
if (j != a[i]) dist[i][j] = 1e18;
b[i][j] = 1e18;
}
}
for (int i = 0; i < m; i++) {
int x, y;
ll w;
cin >> x >> y >> w;
adj[x].push_back({y, w});
adj[y].push_back({x, w});
}
for (int i = 0; i < 4; i++) bfs(i);
ll ans = dist[2][a[3]];
b[0][a[0]] = dist[0][a[2]], b[1][a[0]] = dist[0][a[3]], b[2][a[0]] = dist[0][a[2]], b[3][a[0]] = dist[0][a[3]];
vis[a[0]] = 1;
pq.push({0, a[0]});
while (!pq.empty()) {
int x = pq.top().se;
pq.pop();
ans = min(ans, min(b[0][x] + b[1][x], b[2][x] + b[3][x]));
for (auto u : adj[x]) {
if (dist[0][x] + u.se + dist[1][u.fi] != dist[0][a[1]]) continue;
ll t1 = min(b[0][x], dist[2][u.fi]), t2 = min(b[1][x], dist[3][u.fi]), t3 = min(b[2][x], dist[2][u.fi]), t4 = min(b[3][x], dist[3][u.fi]);
if (b[0][u.fi] > t1) b[0][u.fi] = t1, b[1][u.fi] = t2;
else if (b[0][u.fi] == t1) b[1][u.fi] = min(b[1][u.fi], t2);
if (b[2][u.fi] > t3) b[2][u.fi] = t3, b[3][u.fi] = t4;
else if (b[2][u.fi] == t3) b[3][u.fi] = min(b[3][u.fi], t4);
if (!vis[u.fi]) pq.push({-dist[0][u.fi], u.fi});
vis[u.fi] = 1;
}
}
cout << ans << "\n";
}
# | 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... |