标签:最短路 ons nbsp include clu const 算法 mes ble
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544
给出一个 $n$ 点 $m$ 边的带权无向图,找出结点 $1$ 到结点 $n$ 的路径最小权。($n \le 100, m \le 10000$)
$n$ 的范围较小,可以用 $O_{(n^3)}$ 的 $floyd$,算法的整体思想是枚举中转点和起始端点。
假设两点间存在一条最小权路径,那么该路径上的结点一定会被作为中转点从小到大依次枚举,路径的权值即得以更新。
#include <bits/stdc++.h> using namespace std; const int N = 110; const int INF = 1e9; int n, m; int dis[N][N]; void floyd() { for (int k = 1; k <= n; ++k) { for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } } cout << dis[1][n] << "\n"; } int main() { while (cin >> n >> m and (n or m)) { fill(*dis, *dis + N * N, INF); for (int i = 0; i < m; ++i) { int u, v, l; cin >> u >> v >> l; dis[u][v] = dis[v][u] = l; } floyd(); } }
标签:最短路 ons nbsp include clu const 算法 mes ble
原文地址:https://www.cnblogs.com/Kanoon/p/13382964.html