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 <iostream>
#include <vector>
#include <queue>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
#define MAXN 100001
#define INF 4600000000000000000
ll n, m, s, t, u, v;
vector<vector<pll>> adj(MAXN);
vector<ll> distu(MAXN), distv(MAXN), dists(MAXN);
vector<vector<ll>> dp(2, vector<ll>(MAXN));
ll answer;
vector<bool> visited(MAXN, false);
void dijkstra1(ll start, vector<ll>& dist) {
visited = vector<bool>(MAXN, false);
priority_queue<pll, vector<pll>, greater<pll>> pq;
pq.push({0, start});
while (!pq.empty()) {
ll cdist = pq.top().first;
ll node = pq.top().second;
pq.pop();
if (!visited[node]) {
dist[node] = cdist;
visited[node] = true;
for (auto i : adj[node]) {
pq.push({cdist + i.second, i.first});
}
}
}
}
void dijkstra2(ll start, ll end) {
dp[0] = vector<ll>(MAXN, INF);
dp[1] = vector<ll>(MAXN, INF);
visited = vector<bool>(MAXN, false);
priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> pq;
pq.push({0, {start, 0}});
dp[0][0] = INF;
dp[1][0] = INF;
while (!pq.empty()) {
ll cdist = pq.top().first;
ll node = pq.top().second.first;
ll par = pq.top().second.second;
pq.pop();
if (!visited[node]) {
visited[node] = true;
dists[node] = cdist;
dp[0][node] = min(distu[node], dp[0][par]);
dp[1][node] = min(distv[node], dp[1][par]);
for (auto i : adj[node]) {
pq.push({cdist + i.second, {i.first, node}});
}
}
else if (cdist == dists[node]) {
if (min(distu[node], dp[0][par]) + min(distv[node], dp[1][par]) <= dp[0][node] + dp[1][node]) {
dp[0][node] = min(distu[node], dp[0][par]);
dp[1][node] = min(distv[node], dp[1][par]);
}
}
}
answer = min(answer, dp[0][end] + dp[1][end]);
}
int main() {
cin >> n >> m >> s >> t >> u >> v;
for (int i = 0; i < m; i++) {
ll a, b, cdist;
cin >> a >> b >> cdist;
adj[a].push_back({b, cdist});
adj[b].push_back({a, cdist});
}
dijkstra1(u, distu);
dijkstra1(v, distv);
answer = distu[v];
dijkstra2(s, t);
cout << answer << endl;
return 0;
}
# | 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... |