#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, m, s, t, u, v;
void dfs(vector<vector<pair<int, int>>>& adj, int i, vector<bool>& visited, int& c, int cw, map<pair<int, int>, bool>& free, vector<int>& par) {
if (i == t and cw == c) {
int t = i;
while (par[t] != -1) {
free[make_pair(t, par[t])] = true;
t = par[t];
}
return;
}
visited[i] = true;
for (auto& [j, w] : adj[i]) {
if (visited[j] or cw + w > c) continue;
par[j] = i;
dfs(adj, j, visited, c, cw + w, free, par);
}
visited[i] = false;
}
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
cin >> n >> m >> s >> t >> u >> v;
s--;
t--;
u--;
v--;
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--;
b--;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
vector<int> dis(n, LLONG_MAX);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
pq.push({0, s});
dis[s] = 0;
while (!pq.empty()) {
auto [curw, curn] = pq.top();
pq.pop();
if (curw > dis[curn]) continue;
for (auto& [nextn, nextw] : adj[curn]) {
if (curw + nextw < dis[nextn]) {
dis[nextn] = curw + nextw;
pq.push({dis[nextn], nextn});
}
}
}
vector<bool> visited(n);
map<pair<int, int>, bool> free;
vector<int> par(n, -1);
dfs(adj, s, visited, dis[t], 0, free, par);
// for (auto& [ff, ss] : free) {
// cout << ff.first + 1 << ' ' << ff.second + 1 << '\n';
// }
vector<int> dis2(n, LLONG_MAX);
dis2[u] = 0;
pq.push({0, u});
while (!pq.empty()) {
auto [curw, curn] = pq.top();
pq.pop();
if (curw > dis2[curn]) continue;
for (auto& [nextn, nextw] : adj[curn]) {
if (free.find(make_pair(nextn, curn)) != free.end() or free.find(make_pair(curn, nextn)) != free.end()) {
if (curw < dis2[nextn]) {
dis2[nextn] = curw;
pq.push({dis2[nextn], nextn});
}
} else {
if (curw + nextw < dis2[nextn]) {
dis2[nextn] = curw + nextw;
pq.push({dis2[nextn], nextn});
}
}
}
}
cout << dis2[v];
}
# | 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... |