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 <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define ar array<int, 3>
bool ckmin(int& a, int b){return b < a ? a = b, 1 : 0;}
bool ckmax(int& a, int b){return b > a ? a = b, 1 : 0;}
const int N = 2e5 + 5, mod = 1e9 + 7, INF = 1e18;
vector<ar> a[N];
vector<int> e[N];
int dp[N], dp2[N][3], dp3[N], val[N], vis[N], dp4[N][3];
map<pii, int> mp, mp2;
signed main(){
ios_base::sync_with_stdio(0), cin.tie(0);
int n, m, s, t, u, v; cin >> n >> m >> s >> t >> u >> v;
priority_queue<pii, vector<pii>, greater<pii>> pq;
for(int i = 1; i <= m; i++) {
int x, y, z; cin >> x >> y >> z;
a[x].pb({y, z, i});
a[y].pb({x, z, i});
}
for(int i = 1; i <= n; i++) dp[i] = dp3[i] = INF;
for(int i = 1; i <= n; i++) for(int j = 0; j < 3; j++) dp2[i][j] = dp4[i][j] = INF;
pq.push({0, s});
dp[s] = 0;
while(!pq.empty()) {
pii p = pq.top(); pq.pop();
if(p.fi > dp[p.se]) continue;
for(auto i : a[p.se]) {
if(dp[i[0]] > p.fi + i[1]) {
dp[i[0]] = p.fi + i[1];
pq.push({dp[i[0]], i[0]});
}
}
}
int mn = dp[t];
queue<int> q;
q.push(t);
while(!q.empty()) {
int p = q.front(); q.pop();
for(auto i : a[p]) {
if(dp[i[0]] + i[1] == dp[p]) {
if(!val[i[2]]) {
mp[{i[0], p}] = 1;
mp2[{p, i[0]}] = 1;
val[i[2]] = 1;
q.push(i[0]);
}
}
}
}
priority_queue<ar, vector<ar>, greater<ar>> pqq;
pqq.push({0, u, 0});
dp2[u][0] = 0;
while(!pqq.empty()) {
ar p = pqq.top(); pqq.pop();
if(p[0] > dp2[p[1]][p[2]]) continue;
for(auto i : a[p[1]]) {
if(!p[2] && mp[{p[1], i[0]}] && dp2[i[0]][1] > p[0]) {
dp2[i[0]][1] = p[0];
pqq.push({dp2[i[0]][1], i[0], 1});
}
if(!p[2] && dp2[i[0]][0] > p[0] + i[1]) {
dp2[i[0]][0] = p[0] + i[1];
pqq.push({dp2[i[0]][0], i[0], 0});
}
if(p[2] == 1 && mp[{p[1], i[0]}] && dp2[i[0]][1] > p[0]) {
dp2[i[0]][1] = p[0];
pqq.push({p[0], i[0], 1});
}
if(p[2] == 1 && !mp[{p[1], i[0]}] && dp2[i[0]][2] > p[0] + i[1]) {
dp2[i[0]][2] = p[0] + i[1];
pqq.push({dp2[i[0]][2], i[0], 2});
}
if(p[2] == 2 && dp2[i[0]][2] > p[0] + i[1]) {
dp2[i[0]][2] = p[0] + i[1];
pqq.push({dp2[i[0]][2], i[0], 2});
}
}
}
pqq.push({0, u, 0});
dp4[u][0] = 0;
while(!pqq.empty()) {
ar p = pqq.top(); pqq.pop();
if(p[0] > dp4[p[1]][p[2]]) continue;
for(auto i : a[p[1]]) {
if(!p[2] && mp2[{p[1], i[0]}] && dp4[i[0]][1] > p[0]) {
dp4[i[0]][1] = p[0];
pqq.push({dp4[i[0]][1], i[0], 1});
}
if(!p[2] && dp4[i[0]][0] > p[0] + i[1]) {
dp4[i[0]][0] = p[0] + i[1];
pqq.push({dp4[i[0]][0], i[0], 0});
}
if(p[2] == 1 && mp2[{p[1], i[0]}] && dp4[i[0]][1] > p[0]) {
dp4[i[0]][1] = p[0];
pqq.push({p[0], i[0], 1});
}
if(p[2] == 1 && !mp2[{p[1], i[0]}] && dp4[i[0]][2] > p[0] + i[1]) {
dp4[i[0]][2] = p[0] + i[1];
pqq.push({dp4[i[0]][2], i[0], 2});
}
if(p[2] == 2 && dp4[i[0]][2] > p[0] + i[1]) {
dp4[i[0]][2] = p[0] + i[1];
pqq.push({dp4[i[0]][2], i[0], 2});
}
}
}
pq.push({0, v});
dp3[v] = 0;
while(!pq.empty()) {
pii p = pq.top(); pq.pop();
if(p.fi > dp3[p.se]) continue;
for(auto i : a[p.se]) {
if(dp3[i[0]] > p.fi + i[1]) {
dp3[i[0]] = p.fi + i[1];
pq.push({dp3[i[0]], i[0]});
}
}
}
int ans = INF;
for(int i = 1; i <= n; i++) {
int lol = INF;
for(int j = 0; j < 3; j++) lol = min(lol, min(dp2[i][j], dp4[i][j]));
ans = min(ans, dp3[i] + lol);
}
cout << ans << endl ;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:40:9: warning: unused variable 'mn' [-Wunused-variable]
40 | int mn = dp[t];
| ^~
# | 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... |