铜牌题
[C++] 纯文本查看 复制代码 #include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll info = 4e18;
struct Edge { int u, v; ll w; };
using State = pair<ll, int>;
vector<ll> dijstra(int s, const vector<vector<pair<int, ll>>>& g) {
int n = g.size();
vector<ll> dist(n, info);
vector<bool> visd(n + 1);
priority_queue<State, vector<State>, greater<State>> pq;
dist[s] = 0;
pq.emplace(0, s);
while (!pq.empty()) {
auto [d, u] = pq.top();
pq.pop();
if (visd[u]) continue;
visd[u] = true;
for (auto [v, w] : g[u]) {
ll nd = max(d, w);
if (nd < dist[v]) {
dist[v] = nd;
pq.emplace(nd, v);
}
}
}
return dist;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<Edge> E(m);
vector<vector<pair<int, ll>>> g(n + 1);
for (int i = 0; i < m; ++i) {
int u, v; ll w; cin >> u >> v >> w;
E[i] = { u,v,w };
g[u].push_back({ v,w });
g[v].push_back({ u,w });
}
auto b1 = dijstra(1, g);
auto bn = dijstra(n, g);
ll ans = info;
for (auto& e : E) {
int u = e.u, v = e.v; ll w = e.w;
if (b1[u] <= w && bn[v] <= w)
ans = min(ans, w + max(b1[u], bn[v]));
if (b1[v] <= w && bn[u] <= w)
ans = min(ans, w + max(b1[v], bn[u]));
}
cout << ans << "\n";
return 0;
}
|