이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
typedef long long ll;
typedef long double ld;
const ll SZ = 1e5 + 100, INF = 1e9 * 1e9 + 100;
vector<vector<pair<ll, ll>>> gr;
ll n, m, s, t, u, v;
ll dist[SZ][2], d[SZ][3];
ll used[SZ][2], us[SZ][3];
void djikstra(int v, int ind) {
priority_queue<pair<ll, ll>> st;
for (int i = 0; i < n; i++) {
dist[i][ind] = INF;
}
dist[v][ind] = 0;
for (int i = 0; i < n; i++) st.push({ -dist[i][ind], i });
while (!st.empty()) {
pair<ll, ll> v = (st.top());
st.pop();
if (used[v.second][ind]) continue;
used[v.second][ind] = 1;
v.first = -v.first;
for (auto to : gr[v.second]) {
if (dist[to.first][ind] > v.first + to.second) {
dist[to.first][ind] = v.first + to.second;
st.push({ -dist[to.first][ind], to.first });
}
}
}
}
ll find_ans(int u, int v) {
priority_queue<tuple<ll, ll, ll>> st;
for (int i = 0; i < n; i++) {
for (int k = 0; k < 3; k++) {
d[i][k] = INF;
us[i][k] = 0;
}
}
d[v][0] = 0;
for (int i = 0; i < n; i++) {
for (int k = 0; k < 3; k++) st.push({ -d[i][k], i, k });
}
while (!st.empty()) {
tuple<ll, ll, ll> v = (st.top());
st.pop();
ll vrt = get<1>(v), k = get<2>(v), dst = get<0>(v);
if (us[vrt][k]) continue;
us[vrt][k] = 1;
dst = -dst;
for (auto to : gr[vrt]) {
if (k == 1) {
if (d[to.first][k + 1] > dst + to.second) {
d[to.first][k + 1] = dst + to.second;
st.push({ -d[to.first][k + 1], to.first, k + 1 });
}
} else {
if (d[to.first][k] > dst + to.second) {
d[to.first][k] = dst + to.second;
st.push({ -d[to.first][k], to.first, k });
}
}
int s2 = vrt, t2 = to.first;
if (dist[s2][0] + dist[t2][1] + to.second == dist[t][0]) {
if (k == 0 || k == 1) {
if (d[to.first][1] > dst) {
d[to.first][1] = dst;
st.push({ -d[to.first][1], to.first, 1 });
}
}
}
}
}
return min(min(d[u][0], d[u][1]), d[u][2]);
}
int main() {
fastInp;
cin >> n >> m;
cin >> s >> t;
cin >> u >> v;
s--; t--; u--; v--;
gr.resize(n);
for (int i = 0; i < m; i++) {
ll u, v, c;
cin >> u >> v >> c;
u--; v--;
gr[u].push_back({ v, c });
gr[v].push_back({ u, c });
}
djikstra(s, 0);
djikstra(t, 1);
ll a = find_ans(u, v);
ll b = find_ans(v, u);
cout << min(a, b);
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... |