ds-多源最短路径
题目描述
一个有向图中有 个顶点和 条单向路径 , 试求任意两地点间最短距离。
Input Format
第一行两个正整数 和 ,分别代表点的数量和路径的数量。 0< <20, 0<= <50
之后 行每行有三个整数 分别表示 路径 的起点和终点序号和路径长度。顶点编号从0开始,0<=i,j<n,0<k
Output Format
顶点之间最短路径矩阵(n*n),-1代表不连通。
每一行的最后一个数字后面没有空格。
样例输入输出
样例1
输入:
3 5
0 1 6
0 2 13
1 0 10
1 2 4
2 0 5
输出:
0 6 10
9 0 4
5 11 0
样例2
输入:
6 0
输出:
0 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1
-1 -1 0 -1 -1 -1
-1 -1 -1 0 -1 -1
-1 -1 -1 -1 0 -1
-1 -1 -1 -1 -1 0
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
const int INF = numeric_limits<int>::max();
void floydWarshall(vector<vector<int>>& graph, int n) {
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (graph[i][k] != INF && graph[k][j] != INF) {
graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
}
}
}
}
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> graph(n, vector<int>(n, INF));
for (int i = 0; i < n; ++i) {
graph[i][i] = 0;
}
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
graph[u][v] = w;
}
floydWarshall(graph, n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (graph[i][j] == INF) {
cout << -1;
} else {
cout << graph[i][j];
}
if (j != n - 1) {
cout << " ";
}
}
cout << endl;
}
return 0;
}
上述代码使用弗洛伊德算法(Floyd-Warshall算法)来求解有向图中任意两顶点之间的最短路径。
- 首先,从输入中读取顶点数
n和边数m,并初始化一个n x n的二维数组graph来表示图,对角线元素初始化为0,其余元素初始化为无穷大。 - 然后,根据输入的边信息,将每条边的权重赋值给
graph数组。 - 接着,调用
floydWarshall函数,通过三重循环来更新graph数组,使得graph[i][j]表示顶点i到顶点j的最短路径长度。 - 最后,遍历
graph数组,输出最短路径矩阵,若两顶点之间不连通,则输出 -1。
Dijkstra单源最短路径
题目描述
一个有向图中有 个顶点和 条单向路径 , 请使用Dijkstra算法,求出某一个顶点到其他顶点的最短路径。
Input Format
第一行两个正整数 和 ,分别代表点的数量和路径的数量。 1< <20, 0<= <50
之后 行每行有三个整数 分别表示 路径 的起点、终点序号和路径长度。顶点编号从1开始,0<i,j<=n,0<k<100
源点编号p ,0<p<=n
Output Format
Ⅰ.Dijkstra计算过程;
Ⅱ.输出给定点v到其他各点的路径,以及最短距离。
注意输出格式。橘色点代表空格。CRLF代表换行。
按照Dijkstra算法获取最短路径顺序进行输出,如果没有路径则输出“No Path to ”【顶点编号,升序排列,中间使用空格隔开】
样例输入输出
样例1
输入:
8 10
1 6 10
1 5 5
6 3 1
6 5 2
3 4 4
4 1 7
4 3 6
5 6 3
5 3 9
5 4 2
1
输出:
No.1 : 1 -> 5 , d = 5
No.2 : 1 -> 5 -> 4 , d = 7
No.3 : 1 -> 5 -> 6 , d = 8
No.4 : 1 -> 5 -> 6 -> 3 , d = 9
No.5 : No Path to 2 7 8
问题理解
我们需要实现Dijkstra算法来计算给定有向图中从源点到其他所有顶点的最短路径。具体要求包括:
-
输入格式:
• 第一行:顶点数n和边数m。• 接下来m行:每条边的起点、终点和权重。
• 最后一行:源点编号p。
-
输出格式:
• Dijkstra算法的计算过程。• 从源点到其他各顶点的路径及其最短距离。
• 如果没有路径,则输出“No Path to”以及无法到达的顶点编号(升序排列)。
解决思路
-
图的表示:使用邻接表或邻接矩阵存储图。由于顶点数n较小(<20),邻接矩阵更为直观。
-
Dijkstra算法:
• 初始化:设置源点到自身的距离为0,其他顶点距离为无穷大。• 每次选择当前距离最小的未处理顶点,更新其邻接顶点的距离。
• 使用优先队列(最小堆)优化选择最小距离顶点的过程。
-
路径记录:维护一个前驱数组,记录每个顶点的前驱顶点,以便回溯路径。
-
输出格式:
• 按照Dijkstra算法处理顶点的顺序输出路径和距离。• 对于无法到达的顶点,统一输出。
代码实现
#include <iostream>
#include <vector>
#include <queue>
#include <climits>
#include <algorithm>
#include <iomanip>
using namespace std;
const int INF = INT_MAX;
struct Edge {
int to;
int weight;
Edge(int t, int w) : to(t), weight(w) {}
};
struct Vertex {
int id;
int distance;
Vertex(int i, int d) : id(i), distance(d) {}
bool operator>(const Vertex& other) const {
return distance > other.distance;
}
};
void dijkstra(const vector<vector<Edge>>& graph, int source, vector<int>& dist, vector<int>& prev) {
int n = graph.size();
dist.assign(n, INF);
prev.assign(n, -1);
dist[source] = 0;
priority_queue<Vertex, vector<Vertex>, greater<Vertex>> pq;
pq.push(Vertex(source, 0));
while (!pq.empty()) {
Vertex current = pq.top();
pq.pop();
int u = current.id;
if (current.distance > dist[u]) continue;
for (const Edge& edge : graph[u]) {
int v = edge.to;
int weight = edge.weight;
if (dist[v] > dist[u] + weight) {
dist[v] = dist[u] + weight;
prev[v] = u;
pq.push(Vertex(v, dist[v]));
}
}
}
}
void printPath(int v, const vector<int>& prev) {
if (prev[v] == -1) {
cout << v;
return;
}
printPath(prev[v], prev);
cout << " -> " << v;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<Edge>> graph(n + 1); // 顶点编号从1开始
for (int i = 0; i < m; ++i) {
int u, v, w;
cin >> u >> v >> w;
graph[u].emplace_back(v, w);
}
int source;
cin >> source;
vector<int> dist, prev;
dijkstra(graph, source, dist, prev);
vector<bool> visited(n + 1, false);
vector<int> order;
for (int i = 1; i <= n; ++i) {
if (dist[i] != INF) {
order.push_back(i);
}
}
sort(order.begin(), order.end(), [&dist](int a, int b) {
return dist[a] < dist[b];
});
int count = 1;
for (int v : order) {
if (v == source) continue;
cout << "No." << count++ << " : ";
printPath(v, prev);
cout << " , d = " << dist[v] << endl;
}
vector<int> noPath;
for (int v = 1; v <= n; ++v) {
if (dist[v] == INF && v != source) {
noPath.push_back(v);
}
}
if (!noPath.empty()) {
cout << "No." << count << " : No Path to ";
sort(noPath.begin(), noPath.end());
for (size_t i = 0; i < noPath.size(); ++i) {
if (i != 0) cout << " ";
cout << noPath[i];
}
cout << endl;
}
return 0;
}
代码解释
-
数据结构:
•Edge:表示有向边的终点和权重。•
Vertex:用于优先队列,包含顶点编号和当前距离,重载>运算符以实现最小堆。 -
Dijkstra算法:
• 初始化距离数组dist和前驱数组prev。• 使用优先队列选择当前距离最小的顶点,更新邻接顶点的距离和前驱。
• 如果发现更短的路径,更新距离并加入队列。
-
路径输出:
•printPath:递归打印从源点到目标顶点的路径。• 主函数中,先处理所有可达顶点,按距离排序后输出路径和距离。
• 最后处理不可达顶点,升序排列后输出。
-
输入输出:
• 读取顶点数、边数及边信息,构建邻接表。• 读取源点,调用Dijkstra算法计算最短路径。
• 按要求格式输出结果。
示例验证
输入:
8 10
1 6 10
1 5 5
6 3 1
6 5 2
3 4 4
4 1 7
4 3 6
5 6 3
5 3 9
5 4 2
1
输出:
No.1 : 1 -> 5 , d = 5
No.2 : 1 -> 5 -> 4 , d = 7
No.3 : 1 -> 5 -> 6 , d = 8
No.4 : 1 -> 5 -> 6 -> 3 , d = 9
No.5 : No Path to 2 7 8
解释:
• 从顶点1出发,最短路径依次为:
• 1→5(距离5)
• 1→5→4(距离7)
• 1→5→6(距离8)
• 1→5→6→3(距离9)
• 顶点2、7、8无法到达,输出“No Path to 2 7 8”。

787

被折叠的 条评论
为什么被折叠?



