이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
#define ff first
#define ss second
const ll inf = 1e18;
const int N = 1e5 + 9;
int n, m, S, T, U, V;
vector<pii> g[N];
vector<int> G[N];
ll dist[2][N];
void dijkstra(int u, ll dist[]) {
fill(dist + 1, dist + n + 1, inf);
priority_queue<pli> q;
q.emplace(dist[u] = 0, u);
while (!q.empty()) {
ll d = -q.top().ff; u = q.top().ss; q.pop();
if (d != dist[u]) continue;
for (pii v : g[u]) {
if (dist[v.ff] > d + v.ss) {
dist[v.ff] = d + v.ss;
q.emplace(-dist[v.ff], v.ff);
}
}
}
}
bool vis[N];
ll arr[2][N], ans(inf);
void dfs(int u) {
vis[u] = true;
arr[0][u] = dist[0][u]; // S->u
arr[1][u] = dist[1][u]; // u->T
for (int v : G[u]) {
if (!vis[v]) dfs(v);
arr[0][u] = min(arr[0][u], arr[0][v]);
arr[1][u] = min(arr[1][u], arr[1][v]);
}
ans = min({ ans, arr[0][u] + dist[1][u], dist[0][u] + arr[1][u] });
}
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m >> S >> T >> U >> V;
for (int i = 0, u, v, w; i < m; ++i) {
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
dijkstra(S, dist[0]);
dijkstra(T, dist[1]);
for (int u = 1; u <= n; ++u)
for (pii v : g[u])
if (dist[0][u] + v.ss + dist[1][v.ff] == dist[0][T])
G[u].push_back(v.ff);
dijkstra(U, dist[0]);
dijkstra(V, dist[1]);
for (int i = 1; i <= n; ++i)
if (!vis[i]) dfs(i);
cout << ans << '\n';
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
# | 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... |