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;
const int MAXN = 2e5 + 100;
const int INF = 1e9 + 7;
vector<pair<int, int>> a[MAXN];
void dijkstra(int s, vector<long long> &d){
priority_queue<pair<int,int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
d[s] = 0;
while (!pq.empty()){
int u = pq.top().second;
pq.pop();
for (auto x: a[u]){
int v = x.first;
if (d[v] > d[u] + x.second){
d[v] = d[u] + x.second;
pq.push({d[v], v});
}
}
}
}
vector<long long> d1, d2, d3, d4;
vector<int> f[MAXN];
int n, m;
int s, t;
long long dp[MAXN][2];
int indegree[MAXN];
bool vis[MAXN];
void dfs(int u, int p){
vis[u] = 1;
if (u == t) return;
for (auto x: a[u]){
int v = x.first;
if (vis[v]) continue;
int w = x.second;
if (d1[u] + d2[v] + x.second == d1[t]){
f[u].push_back(v);
indegree[v]++;
}
dfs(v, u);
}
}
int u, v;
int rk[MAXN];
vector<int> topo;
void build(){
queue<int> q;
for (int u = 1; u <= n; u++){
if (f[u].empty()) continue;
if (!indegree[u]) q.push(u);
}
while (!q.empty()){
int u = q.front();
q.pop();
topo.push_back(u);
for (int v: f[u]){
indegree[v]--;
if (!indegree[v]) q.push(v);
}
}
}
int main(){
//freopen("BUS.inp", "r", stdin);
//freopen("BUS.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
cin >> s >> t >> u >> v;
d1.assign(n + 1, INF);
d2.assign(n + 1, INF);
d3.assign(n + 1, INF);
d4.assign(n + 1, INF);
for (int i = 1; i <= m; i++){
int x, y, z;
cin >> x >> y >> z;
a[x].push_back({y, z});
a[y].push_back({x, z});
}
dijkstra(s, d1);
dijkstra(t, d2);
dijkstra(u, d3);
dijkstra(v, d4);
dfs(s, -1);
for (int i = 1; i <= n; i++){
dp[i][0] = dp[i][1] = 1e18;
}
build();
long long ans = 1e18;
for (int u: topo){
dp[u][0] = min(dp[u][0], d3[u]);
dp[u][1] = min(dp[u][1], d4[u]);
ans = min(ans, dp[u][0] + d4[u]);
ans = min(ans, dp[u][1] + d3[u]);
for (int v: f[u]){
dp[v][0] = min(dp[v][0], dp[u][0]);
dp[v][1] = min(dp[v][1], dp[u][1]);
}
}
cout << ans;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'void dfs(int, int)':
commuter_pass.cpp:35:14: warning: unused variable 'w' [-Wunused-variable]
35 | int w = x.second;
| ^
# | 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... |