1 条题解
-
0
最小花费(Minimum Cost) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <queue> #include <iomanip> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; if (!(cin >> n >> m)) return 0; vector<vector<pair<int, double>>> adj(n + 1); for (int i = 0; i < m; ++i) { int u, v, z; cin >> u >> v >> z; double rate = 1.0 - z / 100.0; adj[u].push_back({v, rate}); adj[v].push_back({u, rate}); } int s, t; cin >> s >> t; vector<double> best(n + 1, 0.0); priority_queue<pair<double, int>> pq; best[s] = 1.0; pq.push({1.0, s}); while (!pq.empty()) { auto [d, u] = pq.top(); pq.pop(); if (d < best[u] - 1e-14) continue; if (u == t) break; for (auto& edge : adj[u]) { int v = edge.first; double nd = d * edge.second; if (nd > best[v] + 1e-14) { best[v] = nd; pq.push({nd, v}); } } } cout << fixed << setprecision(8) << (100.0 / best[t]) << "\n"; return 0; }
- 1
信息
- ID
- 8777
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者