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 : Lăng Trọng Đạt
* created: 2023-08-26
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int int64_t
#define mp make_pair
#define f first
#define s second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
const int MAXN = 1e5 + 5;
const int INF = 1e18;
vector<pii> adj[MAXN];
int far[MAXN];
int n, m, x, y, s, t;
int a, b, w;
int res = INF;
set<pii> bought;
void truy_vet() {
int endd = t, start = far[t]; // both endpoint of railway
while (start) {
bought.insert({start, endd});
bought.insert({endd, start});
endd = start;
start = far[start];
}
}
int dijkstra(int start, int target, bool truy) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
vector<int> d(n + 1, INF); // minimum distance from start to node i
pq.push({0, start});
d[start] = 0;
while (pq.size()) {
auto[dist, v] = pq.top(); pq.pop();
db(dist, v, d[v])
if (dist != d[v]) continue;
if (v == target) {
db(res, dist, target, pq.size())
if (!truy) return dist;
truy_vet();
res = min(res, dijkstra(x, y, 0));
continue;
}
for (auto[u, w] : adj[v]) {
// db(u, v, w, dist, truy)
if (!truy && bought.count({u, v})) w = 0;
if (d[u] > w + dist or (truy && d[u] == w + dist)) {
d[u] = w + dist;
pq.push({d[u], u});
if (truy) far[u] = v;
}
}
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("hi.inp", "r")) {
freopen("hi.inp", "r", stdin);
// freopen("hi.out", "w", stdout);
}
cin >> n >> m >> s >> t >> x >> y;
while (cin >> a >> b >> w) {
adj[a].pb({b, w});
adj[b].pb({a, w});
}
dijkstra(s, t, true);
cout << res;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int64_t dijkstra(int64_t, int64_t, bool)':
commuter_pass.cpp:43:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
43 | auto[dist, v] = pq.top(); pq.pop();
| ^
commuter_pass.cpp:53:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | for (auto[u, w] : adj[v]) {
| ^
commuter_pass.cpp:38:52: warning: control reaches end of non-void function [-Wreturn-type]
38 | priority_queue<pii, vector<pii>, greater<pii>> pq;
| ^~
commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:68:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | freopen("hi.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |