C++ 实现加权有向图中指定边数路径的最小成本算法
#include
// 定义边 struct Edge { int to, cost; Edge(int t, int c): to(t), cost(c) {} };
// 定义邻接表
typedef vector<vector
// Dijkstra算法
int dijkstra(const AdjList& graph, int source, int destination, int m) {
int n = graph.size();
vector
int main() { int n, e; cin >> n >> e; AdjList graph(n); for (int i = 0; i < e; i++) { int u, v, w; cin >> u >> v >> w; graph[u].push_back(Edge(v, w)); } int source, destination, m; cin >> source >> destination >> m; int ans = dijkstra(graph, source, destination, m); if (ans == INT_MAX) { cout << 2147483647 << endl; } else { cout << ans << endl; } return 0; }
原文地址: https://www.cveoy.top/t/topic/m1Et 著作权归作者所有。请勿转载和采集!