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>
/// #pragma GCC optimize ("Ofast")
/// #pragma GCC target ("avx2")
using namespace std;
using ll = long long;
using ii = pair<ll, ll>;
using vi = vector<int>;
using db = double;
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define lb lower_bound
#define ub upper_bound
const ll INF = LONG_LONG_MAX;
int n, m, s, t, from, to;
vector<array<ll, 2>> adj[100005];
ll dist[100005][2], best;
void dijkstra(int src, int c) {
for(int l = 0; l < n; l++)
dist[l][c] = INF;
priority_queue<ii, vector<ii>, greater<ii>> pq;
pq.push({0, src}); dist[src][c] = 0; vector<bool> vis(n, 0);
while(!pq.empty()) {
int a = pq.top().ss; ll w = pq.top().ff; pq.pop();
if(dist[a][c] < w || vis[a]) continue;
vis[a] = 1;
for(auto u : adj[a]) {
ll len = w + u[1];
if(vis[u[0]] || dist[u[0]][c] < len) continue;
dist[u[0]][c] = len;
pq.push({len, u[0]});
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> s >> t >> from >> to;
for(int l = 0; l < m; l++) {
int u, v; ll w; cin >> u >> v >> w; u--; v--;
adj[u].pb({v, w}); adj[v].pb({u, w});
}
s--; t--; from--; to--;
dijkstra(s, 0); dijkstra(t, 1); best = dist[t][0];
priority_queue<ii, vector<ii>, greater<ii>> pq;
vector<ll> dis(n, INF); pq.push({0, from}); dis[from] = 0;
while(!pq.empty()) {
int a = pq.top().ss; ll w = pq.top().ff; pq.pop();
if(dis[a] < w) continue;
for(auto u : adj[a]) {
ll len = u[1] + w; bool ok = 0;
if(dist[a][0] + u[1] + dist[u[0]][1] == best) ok = 1;
if(dist[a][1] + u[1] + dist[u[0]][0] == best) ok = 1;
if(ok) len = w;
if(dis[u[0]] <= len) continue;
dis[u[0]] = len; pq.push({len, u[0]});
}
}
cout << dis[to] << "\n";
}
/**
8 8
5 7
6 8
1 2 2
2 3 3
3 4 4
1 4 1
1 5 5
2 6 6
3 7 7
4 8 8
**/
# | 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... |