2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Table of contents
The Floyd algorithm, also known as the Floyd-Warshall algorithm, is a dynamic programming algorithm used to solve the shortest path problem between all pairs of vertices in a graph. It is applicable to weighted directed graphs, can handle negative weight edges (but cannot handle graphs with negative weight cycles), and can detect whether there are negative weight cycles in the graph.
The core idea of Floyd's algorithm is dynamic programming, and its basic steps are as follows:
Implementations of Floyd's algorithm usually use a triple loop, where the outer loop traverses all intermediate vertices k, and the inner two loops traverse all vertex pairs (i, j) to check whether there is a shorter path.
Suppose we have a weighted graph G with n vertices, we can use a two-dimensional arraydist[][]
To store the shortest path length between each pair of vertices. Initially,dist[i][j]
is set to the weight of the directly connected edge in the graph. If there is no directly connected edge between i and j, thendist[i][j]
Set to infinity (indicating unreachable).
initialization: Create an n×n matrix D, where D[i][j] is initialized to the weight of the direct edge from i to j in the graph, or to infinity if there is no direct edge between i and j. The diagonal elements D[i][i] = 0.
Dynamic Programming: For each vertex k (as an intermediate vertex), update the shortest path between each pair of vertices i and j, and use k as the intermediate point to obtain a shorter path. The update formula is: D[i][j] = min(D[i][j], D[i][k] + D[k][j]). This means that if the path from i to j becomes shorter by using k as the intermediate vertex, update D[i][j].
Result Output: After n rounds of iterations, each element D[i][j] in the matrix D is the length of the shortest path from i to j.
343. Sorting - AcWing Question Bank
- #include <algorithm>
- #include <bits/stdc++.h>
- #include <iostream>
- using namespace std;
-
- #define PII pair<int, int>
- #define fi first
- #define se second
- #define endl 'n'
- map<int, int> mp;
-
- const int N = 210, mod = 1e9 + 7;
- int T, n, m, k;
- int a[N], dist[N][N];
- PII ans[N];
-
- void floyd(int x) // 以 x 为中转点,更新其他点。
- {
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- dist[i][j] = min(dist[i][j], dist[i][x] + dist[x][j]);
- }
-
- bool pd() // 判断是否所有点都已确定
- {
- for (int i = 1; i <= n; i++) {
- int cnt = 0;
- for (int j = 1; j <= n; j++)
- if (dist[i][j] != 1e9)
- cnt++; // 当前点能到达的点数
- ans[i] = {cnt, i};
- for (int j = 1; j <= n; j++)
- if (i != j && dist[j][i] != 1e9)
- cnt++; // 能到达当前点的点数
- if (cnt != n)
- return 0;
- }
- sort(ans + 1, ans + n + 1);
- return 1;
- }
-
- int main() {
- while (cin >> n >> m && n) {
- for (int i = 1; i <= n; i++)
- for (int j = 1; j <= n; j++)
- if (i != j)
- dist[i][j] = 1e9;
-
- int flag = 0;
- for (int i = 1; i <= m; i++) {
- char a, t, b;
- cin >> a >> t >> b;
- int x = a - 'A' + 1, y = b - 'A' + 1; // 现在要加一条从 y 到 x 的边
-
- if (!flag && dist[x][y] != 1e9) // 发现已经从x能到y,出现矛盾
- {
- printf("Inconsistency found after %d relations.n", i);
- flag = 1;
- }
-
- dist[y][x] = 1;
- floyd(x), floyd(y); // 分别以 x 和 y 为中转点更新其他点
- if (!flag && pd()) { // 发现所有点都已确定
- printf("Sorted sequence determined after %d relations: ", i);
- for (int i = 1; i <= n; i++)
- cout << char(ans[i].se + 'A' - 1);
- printf(".n");
- flag = 1;
- }
- }
- if (!flag)
- printf("Sorted sequence cannot be determined.n"); // 无法确定
- }
-
- return 0;
- }
dist
The two-dimensional array is initialized to infinity, indicating that there is no definite order relationship between inequalities by default.dist
Matrix, use Floyd's algorithm to update the shortest paths between all inequalities, which actually updates the "priority" or "sort" relationship.