This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* Author : Nguyen Tuan Vu
* Created : 26.11.2022
**/
#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#define MASK(x) ((1)<<(x))
#define BIT(x, i) (((x)>>(i))&(1))
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define db(val) "["#val" = "<<(val)<<"] "
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
using namespace std;
mt19937 jdg(chrono::steady_clock::now().time_since_epoch().count());
int Rand(int l, int r) {return l + jdg() % (r - l + 1);}
void file(){
#define TASK "TASK"
if(fopen(TASK".in", "r")) {
freopen(TASK".in", "r", stdin);
freopen(TASK".out", "w", stdout);
}
}
const int N = 1e5 + 5;
int n, m, st[2], en[2];
long long dp[2][N], f[N][2][2], ans;
vector <pair <int, int>> adj[N];
void dijkstra(int x, long long dp[]) {
FOR(i, 1, n) dp[i] = 1e18 + 7;
priority_queue <pair <long long, int>, vector <pair <long long, int>>, greater <pair <long long, int>>> heap;
heap.push({dp[x] = 0, x});
while (heap.size()) {
auto [du, u] = heap.top();
heap.pop();
if (du != dp[u]) continue;
for (auto v : adj[u]) if (minimize(dp[v.second], dp[u] + v.first)) {
heap.push({dp[v.second], v.second});
}
}
}
bool check(int u, int w, int v, int type) {
if (type == 0)
return (dp[0][u] + w + dp[1][v] == dp[0][en[0]]);
return (dp[0][v] + w + dp[1][u] == dp[0][en[0]]);
}
void dijkstra2(int mask) {
FOR(i, 1, n) REP(j, 2) REP(k, 2) f[i][j][k] = 1e18 + 7;
#define iii pair <long long, tuple <int, int, int>>
priority_queue <iii, vector <iii>, greater <iii>> heap;
heap.push({f[st[1]][0][0] = 0, {st[1], 0, 0}});
//cout << check(1, 1, 2) << '\n';
while (heap.size()) {
auto [du, tmp] = heap.top();
auto [u, type, type2] = tmp;
heap.pop();
if (du != f[u][type][type2]) continue;
//cout << u << ' ' << type << ' ' << type2 << ' ' << f[u][type][type2] << '\n';
for (auto v : adj[u]) {
if (type == 0) {
if (minimize(f[v.second][type][type2], f[u][type][type2] + v.first)) {
heap.push({f[v.second][type][type2], {v.second, type, type2}});
}
if (check(u, v.first, v.second, mask)) {
if (minimize(f[v.second][1][1], f[u][type][type2])) {
heap.push({f[v.second][1][1], {v.second, 1, 1}});
}
}
}
else {
if (type2 == 0) {
if (minimize(f[v.second][type][type2], f[u][type][type2] + v.first)) {
heap.push({f[v.second][type][type2], {v.second, type, type2}});
}
}
else {
if (minimize(f[v.second][type][0], f[u][type][type2] + v.first)) {
heap.push({f[v.second][type][0], {v.second, type, 0}});
}
if (check(u, v.first, v.second, mask)) {
if (minimize(f[v.second][1][1], f[u][type][type2])) {
heap.push({f[v.second][1][1], {v.second, 1, 1}});
}
}
}
}
}
}
minimize(ans, min({f[en[1]][0][0], f[en[1]][1][0], f[en[1]][1][1]}));
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
file();
ans = 1e18 + 7;
cin >> n >> m;
cin >> st[0] >> en[0];
cin >> st[1] >> en[1];
FOR(i, 1, m) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({w, v});
adj[v].push_back({w, u});
}
dijkstra(st[0], dp[0]);
dijkstra(en[0], dp[1]);
//FOR(i, 1, n) cout << dp[0][i] << ' ' << dp[1][i] << '\n';
dijkstra2(0);
dijkstra2(1);
cout << ans;
cerr << "Time elapsed: " << TIME << " s.\n";
return 0;
}
/*
==================================================+
INPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
--------------------------------------------------|
==================================================+
*/
Compilation message (stderr)
commuter_pass.cpp: In function 'void file()':
commuter_pass.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(TASK".in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |