This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://oj.uz/problem/view/JOI18_commuter_pass
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1000 * 1000 * 1000;
const ll LINF = (ll)INF * INF;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define msub(a, b) ((mod + ((a) - (b)) % mod) % mod)
#define mdiv(a, b) ((a) * poww((b), mod - 2) % mod)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 200100
#define MAXM 200100
int n, m, s, t, u, v;
vector<int> adj[MAXN];
int efrom[MAXM], eto[MAXM], ew[MAXM];
ll sdist[MAXN], vdist[MAXN], udist[MAXN];
int ver[MAXN];
ll dp[MAXN];
ll udp[MAXN], vdp[MAXN];
priority_queue<pll, vector<pll>, greater<pll>> pq;
inline void dij(int r, ll dist[MAXN])
{
fill(dist, dist + n + 10, LINF);
dist[r] = 0;
FOR(i, 1, n + 1)
{
pq.push(mp(dist[i], i));
}
while (!pq.empty())
{
pll p = pq.top();
pq.pop();
int v = p.sc;
if (p.fr != dist[v])
{
continue;
}
for (int e : adj[v])
{
int u = efrom[e] ^ eto[e] ^ v;
ll nw = dist[v] + ew[e];
if (nw < dist[u])
{
dist[u] = nw;
pq.push(mp(dist[u], u));
}
}
}
}
bool cmp(int a, int b)
{
return sdist[a] < sdist[b];
}
int32_t main(void)
{
scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v);
FOR(i, 0, m)
{
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
adj[x].pb(i);
adj[y].pb(i);
efrom[i] = x;
eto[i] = y;
ew[i] = w;
}
dij(s, sdist);
dij(u, udist);
dij(v, vdist);
iota(ver + 1, ver + n + 1, 1);
sort(ver + 1, ver + n + 1, cmp);
FOR(i, 1, n + 1)
{
int x = ver[i];
udp[x] = udist[x];
vdp[x] = vdist[x];
dp[x] = vdist[x] + udist[x];
for (int e : adj[x])
{
int y = efrom[e] ^ eto[e] ^ x;
if (sdist[y] + ew[e] == sdist[x])
{
udp[x] = min(udp[x], udp[y]);
vdp[x] = min(vdp[x], vdp[y]);
dp[x] = min(dp[x], dp[y]);
dp[x] = min(dp[x], min(udp[y] + vdist[x], vdp[y] + udist[x]));
}
}
}
ll ans = udist[v];
ans = min(ans, dp[t]);
printf("%lld\n", ans);
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int32_t main()':
commuter_pass.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
80 | scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:84:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
84 | scanf("%d%d%d", &x, &y, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |