#include <bits/stdc++.h>
#define Longgggg ios_base::sync_with_stdio(0); cin.tie(0);
#define ll long long
#define endl '\n'
using namespace std;
// Bitwise
#define MASK(i) (1LL << (i))
#define BIT(x, i) (1LL & ((x) >> (i)))
#define ON(x, i) ((x) | MASK(i))
#define OFF(x, i) ((x) & ~MASK(i))
#define LASTBIT(mask) ((mask) & -(mask))
// Other
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define mod(x, k) ((((x) % (k)) + (k)) % (k))
#define all(x) begin(x), end(x)
#define fi first
#define se second
#define IN "A.in"
#define OUT "A.out"
#define DEBUG "debug.out"
const int INF = (int) 1e9+5;
const ll LINF = (ll) 1e18;
const ll MOD = (ll) 1e9+7;
const int mxN = 200005;
int n, m, S, T, U, V;
vector <pair <ll, ll>> adj[mxN];
vector <ll> d(mxN), par(mxN);
set <pair <ll, ll>> path;
// Chỉ dành cho N, M nhỏ!
void solve() {
cin >> n >> m >> S >> T >> U >> V;
vector<vector<pair<int, int>>> adj(n+1);
vector<tuple<int,int,int>> edges;
for (int i = 0; i < m; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
edges.emplace_back(u, v, w);
}
// 1. Dijkstra S->T
vector<ll> ds(n+1, LINF), par(n+1, -1);
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<>> pq;
ds[S]=0; pq.push({0, S});
while(!pq.empty()) {
auto [d,u] = pq.top(); pq.pop();
if (d > ds[u]) continue;
for (auto [v, w] : adj[u]) {
if (ds[v] > ds[u]+w) {
ds[v] = ds[u]+w;
par[v] = u;
pq.push({ds[v], v});
}
}
}
ll minST = ds[T];
// 2. Build DAG of all shortest paths
vector<vector<pair<int,int>>> dag(n+1);
for (auto [u,v,w] : edges) {
if (ds[u] + w == ds[v]) dag[u].push_back({v, w});
if (ds[v] + w == ds[u]) dag[v].push_back({u, w});
}
// 3. DFS all path from S->T, for each, build set of free edge and Dijkstra U->V
ll ans = LINF;
vector<pair<int,int>> path;
set<pair<int,int>> used;
function<void(int)> dfs = [&](int u) {
if (u == T) {
// Mark path
set<pair<int,int>> free;
for (auto [x, y] : path)
free.insert({min(x, y), max(x, y)});
// Dijkstra U->V
vector<ll> d(n+1, LINF);
priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<>> pq2;
d[U] = 0; pq2.push({0, U});
while (!pq2.empty()) {
auto [du, uu] = pq2.top(); pq2.pop();
if (du > d[uu]) continue;
for (auto [vv, ww] : adj[uu]) {
ll cost = free.count({min(uu, vv), max(uu, vv)}) ? 0 : ww;
if (d[vv] > d[uu] + cost) {
d[vv] = d[uu] + cost;
pq2.push({d[vv], vv});
}
}
}
ans = min(ans, d[V]);
return;
}
for (auto [v, w] : dag[u]) {
if (used.count({u, v})) continue;
used.insert({u, v});
path.push_back({u, v});
dfs(v);
path.pop_back();
used.erase({u, v});
}
};
dfs(S);
cout << ans << '\n';
}
signed main() {
if (fopen(IN, "r")) {
freopen(IN, "r", stdin);
freopen(OUT, "w", stdout);
freopen(DEBUG, "w", stderr);
}
Longgggg
ll t = 1;
// cin >> t;
while (t--) solve();
return 0;
}
Compilation message (stderr)
commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | freopen(IN, "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
commuter_pass.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen(OUT, "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~
commuter_pass.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen(DEBUG, "w", stderr);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
# | 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... |