#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define pb push_back
#define mp make_pair
vector<int> dx = {0, 0, 1, -1};
vector<int> dy = {1, -1, 0, 0};
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
int n, m;
vector<pii> adj[maxn];
void dijkstra(int s, vector<ll> &dist)
{
dist[s] = 0;
priority_queue<pair<ll, int>> pq;
pq.push({0, s});
while (!pq.empty())
{
ll d = -pq.top().fi;
int u = pq.top().se;
pq.pop();
if (d != dist[u]) continue;
for (auto [v, w] : adj[u])
{
if (d + w < dist[v])
{
dist[v] = d + w;
pq.push({-dist[v], v});
}
}
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
//freopen(".INP", "r", stdin);
//freopen(".OUT", "w", stdout);
int s, t, u, v; cin >> n >> m >> s >> t >> u >> v;
vector<tuple<int, int, int>> roads;
for (int i = 1; i <= m; ++i)
{
int a, b, w; cin >> a >> b >> w;
adj[a].pb({b, w});
adj[b].pb({a, w});
roads.pb(make_tuple(a, b, w));
}
vector<ll> distS(n+1, INF), distT(n+1, INF), distU(n+1, INF), distV(n+1, INF);
dijkstra(s, distS); dijkstra(t, distT); dijkstra(u, distU); dijkstra(v, distV);
vector<vector<int>> dag(n+1);
vector<int> nodes;
for (auto [i, j, w] : roads)
{
if (distS[i] != INF && distT[j] != INF && distS[i] + distT[j] + w == distS[t]) dag[i].pb(j);
if (distS[j] != INF && distT[i] != INF && distS[j] + distT[i] + w == distS[t]) dag[j].pb(i);
}
for (int i = 1; i <= n; ++i) if (distS[i] + distT[i] == distS[t]) nodes.pb(i);
sort(all(nodes), [&](int a, int b) {return distS[a] < distS[b];});
ll ans = distU[v];
vector<ll> dp(n+1, INF);
vector<ll> dp2(n+1, INF);
for (auto i : nodes)
{
dp[i] = min(dp[i], distU[i]);
dp2[i] = min(dp2[i], distV[i]);
ans = min({ans, dp[i] + distV[i], dp2[i] + distU[i]});
for (auto j : dag[i])
{
dp[j] = min(dp[j], dp[i]);
dp2[j] = min(dp2[j], dp2[i]);
}
}
cout << ans;
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... |