이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e16;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 1e5;
int n, m, s, t, u, v;
vector<array<int, 2>> g[N];
vi du(N, inf), dv(N, inf), dpu(N, inf), dpv(N, inf), dis(N, inf);
int ans = inf;
void dijkstra(int src, vi &distance) {
distance[src] = 0;
priority_queue<pii, vector<pii>, greater<pii>> q;
q.push({0, src});
while (!q.empty()) {
int dist = q.top().f, src = q.top().s; q.pop();
for (auto ch : g[src]) {
if (ch[1]+dist < distance[ch[0]]) {
distance[ch[0]] = ch[1]+dist;
q.push({distance[ch[0]], ch[0]});
}
}
}
}
void dijkstra2(int src, int end) {
dpu = vi(n, inf); dpv = vi(n, inf);
priority_queue<array<int, 3>, vector<array<int, 3>>, greater<array<int, 3>>> q;
q.push({0, src, src});
vi vis(n, 0);
while (!q.empty()) {
int dist = q.top()[0], src = q.top()[1], par = q.top()[2]; q.pop();
if (!vis[src]) {
vis[src] = true;
dis[src] = dist;
dpu[src] = min(dpu[par], du[src]);
dpv[src] = min(dpv[par], dv[src]);
for (auto ch : g[src]) q.push({dist+ch[1], ch[0], src});
}
else if (dis[src] == dist) {
if (min(dpu[par], du[src]) + min(dpv[par], dv[src]) <= dpu[src]+dpv[src]) {
dpu[src] = min(dpu[par], du[src]);
dpv[src] = min(dpv[par], dv[src]);
}
}
}
ans = min(ans, dpu[end]+dpv[end]);
}
void sol() {
cin >> n >> m >> s >> t >> u >> v; --s; --t; --u; --v;
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
g[--a].pb({--b, c});
g[b].pb({a, c});
}
dijkstra(u, du);
dijkstra(v, dv);
ans = du[v];
dijkstra2(s, t);
cout << ans << endl;
}
signed main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
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... |