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 long long INF = 1e18/3;
vector<pair<int, int>> a[MAXN];
void dijkstra(int s, vector<long long> &d){
priority_queue<pair<long long,int>, vector<pair<long long, int>>, greater<pair<long long, 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];
int u, v;
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);
}
}
}
pair<pair<int, int>, int> edge[MAXN];;
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});
edge[i] = {{x, y}, z};
}
dijkstra(s, d1);
dijkstra(t, d2);
dijkstra(u, d3);
dijkstra(v, d4);
for (int i = 1; i <= m; i++){
int x = edge[i].first.first;
int y = edge[i].first.second;
if (d1[x] + edge[i].second + d2[y] == d1[t]){
f[x].push_back(y);
indegree[y]++;
}
else if (d1[y] + edge[i].second + d2[x] == d1[t]){
f[y].push_back(x);
indegree[x]++;
}
}
for (int i = 1; i <= n; i++){
dp[i][0] = dp[i][1] = 1e18;
}
build();
long long ans = d3[v];
// reverse(topo.begin(), topo.end());
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;
}
# | 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... |