#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define ld long double
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int NMAX = 1e5;
const ll INF = 1e18;
struct PQElement {
int node, parent;
ll d;
bool operator < (const PQElement &other) const {
return other.d < d;
}
};
int n, m, s, t, x, y;
ll answer;
vector<pair<int, int>> g[NMAX + 1];
vector<int> g1[NMAX + 1];
int indegree[NMAX + 1];
ll dp[NMAX + 1];
ll d[4][NMAX + 1];
void Dijkstra(int start, int which_start) {
for(int i = 1; i <= n; i++) {
d[which_start][i] = INF;
}
priority_queue<PQElement> pq;
pq.push({start, 0, 0});
while(!pq.empty()) {
auto [node, parent, curent_d] = pq.top();
pq.pop();
if(d[which_start][node] == INF) {
d[which_start][node] = curent_d;
for(auto [next_node, edge_d] : g[node]) {
pq.push({next_node, node, curent_d + edge_d});
}
}
}
}
void CreateDAG(int type) {
for(int i = 1; i <= n; i++) {
g1[i].clear();
indegree[i] = 0;
}
for(int i = 1; i <= n; i++) {
for(auto [j, edge_d] : g[i]) {
if(type == 0 && d[2][i] + edge_d + d[3][j] == d[2][t]) {
g1[i].push_back(j);
indegree[j]++;
}
else if(type == 1 && d[3][i] + edge_d + d[2][j] == d[2][t]) {
g1[i].push_back(j);
indegree[j]++;
}
}
}
}
void SolveDP() {
for(int i = 1; i <= n; i++) {
dp[i] = d[0][i];
}
queue<int> q;
for(int i = 1; i <= n; i++) {
if(!indegree[i]) {
q.push(i);
}
}
while(!q.empty()) {
int node = q.front();
q.pop();
answer = min(answer, dp[node] + d[1][node]);
for(int next_node : g1[node]) {
indegree[next_node]--;
dp[next_node] = min(dp[next_node], dp[node]);
if(!indegree[next_node]) {
q.push(next_node);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> s >> t >> x >> y;
for(int i = 1; i <= m; i++) {
int a, b, c;
cin >> a >> b >> c;
g[a].emplace_back(b, c);
g[b].emplace_back(a, c);
}
Dijkstra(x, 0);
Dijkstra(y, 1);
Dijkstra(s, 2);
Dijkstra(t, 3);
answer = INF;
CreateDAG(0);
SolveDP();
CreateDAG(1);
SolveDP();
cout << answer << '\n';
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... |