# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1087579 | MinhKien | Commuter Pass (JOI18_commuter_pass) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define int long long
const int N = 1e5 + 10;
#define ll long long
#define li pair <ll, int>
int n, m, a, b, c, d;
int x, y;
ll k;
vector <li> v[N];
ll disA[N], disB[N], disC[N], disD[N];
void dijkstra(int s, ll dis[]) {
fill_n(dis, N, 4e18);
dis[s] = 0;
priority_queue < li, vector <li>, greater <li> > q;
q.push(li(0, s));
while (!q.empty()) {
li p = q.top(); q.pop();
int u = p.second;
ll w = p.first;
if (dis[u] < w) continue;
for (li z: v[u]) {
if (z.first + w < dis[z.second]) {
dis[z.second] = z.first + w;
q.push(li(dis[z.second], z.second));
}
}
}
}
void pre() {
dijkstra(a, disA);
dijkstra(b, disB);
dijkstra(c, disC);
dijkstra(d, disD);
}
ll dp[2][N], best = 4e18;
bool vis[N];
void dfs(int s) {
vis[s] = true;
dp[0][s] = dp[1][s] = 4e18;
for (li i: v[s]) {
int z = i.second;
if (disA[z] + disB[z] + z.first != disA[b]) continue;
if (!vis[z]) {
dfs(z);
}
dp[0][s] = min(dp[0][s], dp[0][z]);
dp[1][s] = min(dp[1][s], dp[1][z]);
}
best = min(best, disC[s] + dp[1][s]);
best = min(best, dp[0][s] + disD[s]);
dp[0][s] = min(dp[0][s], disC[s]);
dp[1][s] = min(dp[1][s], disD[s]);
}
void solve() {
best = disC[d];
dfs(a);
cout << best << "\n";
}
signed main() {
if (fopen("bus.inp", "r")) {
freopen("bus.inp", "r", stdin);
freopen("bus.out", "w", stdout);
}
cin.tie(nullptr);
cout.tie(nullptr);
ios_base::sync_with_stdio(false);
cin >> n >> m >> a >> b >> c >> d;
for (int i = 1; i <= m; ++i) {
cin >> x >> y >> k;
v[x].push_back(li(k, y));
v[y].push_back(li(k, x));
}
pre();
solve();
return 0;
}