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>
typedef long long int ll;
#define INF ll(1e18 + 7)
#define N (ll)1e5 + 5
using namespace std;
#define int ll
int n, m, s, t, u2, v, g, h, z, g2, h2, ans;
vector<pair<int, int>> v2[N];
bool used[N];
int ds[N], du[N], dv[N];
pair<int, int> d1[N], d2[N];
void bfs(){
memset(used, 0, sizeof(used));
priority_queue<pair<int, int>> q;
q.push({0, s});
pair<int, int> p;
while(!q.empty()){
p = q.top();
q.pop();
if(used[p.second])continue;
used[p.second] = 1;
for(auto u : v2[p.second]){
if(!used[u.first] && ds[u.first] > ds[p.second] + u.second){
ds[u.first] = ds[p.second] + u.second;
q.push({-ds[u.first], u.first});
}
}
}
}
void bfs2(){
memset(used, 0, sizeof(used));
priority_queue<pair<int, int>> q;
q.push({0, u2});
pair<int, int> p;
while(!q.empty()){
p = q.top();
q.pop();
if(used[p.second])continue;
used[p.second] = 1;
for(auto u : v2[p.second]){
if(!used[u.first] && du[u.first] > du[p.second] + u.second){
du[u.first] = du[p.second] + u.second;
q.push({-du[u.first], u.first});
}
}
}
}
void bfs3(){
memset(used, 0, sizeof(used));
priority_queue<pair<int, int>> q;
q.push({0, v});
pair<int, int> p;
while(!q.empty()){
p = q.top();
q.pop();
if(used[p.second])continue;
used[p.second] = 1;
for(auto u : v2[p.second]){
if(!used[u.first] && dv[u.first] > dv[p.second] + u.second){
dv[u.first] = dv[p.second] + u.second;
q.push({-dv[u.first], u.first});
}
}
}
}
bool visited[N];
int dp[2][N];
void dijkstra2(ll start, ll end) {
fill(dp[0], dp[0] + 100001, LLONG_MAX / 2);
fill(dp[1], dp[1] + 100001, LLONG_MAX / 2);
fill(visited, visited + 100001, false);
priority_queue<pair<ll, pair<ll, ll>>> pq;
pq.push({0, {start, 0}});
dp[0][0] = dp[1][0] = LLONG_MAX/ 2;
while (!pq.empty()) {
ll c, node, par;
pair<ll, ll> p;
tie(c, p) = pq.top();
tie(node, par) = p;
pq.pop();
if (!visited[node]) {
visited[node] = true;
ds[node] = -c;
dp[0][node] = min(du[node], dp[0][par]);
dp[1][node] = min(dv[node], dp[1][par]);
for (auto i : v2[node]) pq.push({c - i.second, {i.first, node}});
} else if (-c == ds[node]) {
if (min(du[node], dp[0][par]) + min(dv[node], dp[1][par]) <= dp[0][node] + dp[1][node]) {
dp[0][node] = min(du[node], dp[0][par]);
dp[1][node] = min(dv[node], dp[1][par]);
}
}
}
ans = min(ans, dp[0][end] + dp[1][end]);
}
void solve(){
cin >> n >> m >> s >> t >> u2 >> v;
for(int i=0; i<m; i++){
cin >> g >> h >> z;
v2[g].push_back({h, z});
v2[h].push_back({g, z});
}
fill(ds, ds+n+1, INF);
fill(du, du+n+1, INF);
fill(dv, dv+n+1, INF);
fill(d1, d1 + n + 1, make_pair(INF, INF));
fill(d2, d2 + n + 1, make_pair(INF, INF));
ds[s] = du[u2] = dv[v] = 0;
bfs();
bfs2();
bfs3();
ans = du[v];
for(int i=1; i<=n; i++){
d1[i] = d2[i] = {du[i], dv[i]};
}
int t1, t2, t3, t4;
dijkstra2(s, t);
dijkstra2(t, s);
cout<<ans;
}
signed main(){
//freopen("shortcut.in", "r", stdin);
//freopen("shortcut.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
int T = 1;
//cin>>T;
while (T--){
solve();
}
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void solve()':
commuter_pass.cpp:126:9: warning: unused variable 't1' [-Wunused-variable]
126 | int t1, t2, t3, t4;
| ^~
commuter_pass.cpp:126:13: warning: unused variable 't2' [-Wunused-variable]
126 | int t1, t2, t3, t4;
| ^~
commuter_pass.cpp:126:17: warning: unused variable 't3' [-Wunused-variable]
126 | int t1, t2, t3, t4;
| ^~
commuter_pass.cpp:126:21: warning: unused variable 't4' [-Wunused-variable]
126 | int t1, t2, t3, t4;
| ^~
# | 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... |