C/C++最短路径

文章介绍了如何运用Dijkstra算法解决单源最短路径问题,并在给定的无向连通图中判断所有顶点是否联通。程序使用C++编写,通过构建优先队列进行节点的遍历和距离更新。
摘要由CSDN通过智能技术生成

单源最短路径Dijkstra算法

  1. 定义一个数组dist[],存储当前最短路径 :dist[s]=0,其他dist[i]=+∞【INT_MAX]
  2. 遍历起点的所有的边,更新dist
  3. 下一个节点的选择,dist[i]最小且未访问过【用一个小根堆】维护
  4. 循环2-3,得到最终的dist

题目——还是畅通工程
描述:
给定一个无向连通图和其中的所有的边,判断这个图的所有顶点是否是联通的
输入:
每组数据的第一行是两个整数n和m(0<=n<=1000),n表示图的顶点数目,m表示图中边的数目,随后有m行数据,每行有两个值x和y(0<x,y<=n),表示顶点x和y相邻,顶点的编号从1开始计数。输入不保证这些边是否重复。
输出:
对每组输入数据,若所有顶点都是联通的,就输出"YES",否则输出"NO"

#include<cstdio>
#include<vector>
#include<climits>
#include<queue>
#define N 300
using namespace std;
struct Edge {
	int y;
	int weight;
};
struct Node {
	int dist;
	int x;
};
bool operator<(Node lhs, Node rhs) {
	return lhs.dist > rhs.dist;
}
vector<Edge>graph[300];
int dijkstra(int s, int t, int n) {
	int dist[N];
	bool isvisit[N];
	for (int i = 0; i < n; i++) {
		dist[i] = INT_MAX;
		isvisit[i] = false;
	}
	priority_queue<Node>pqueue;//第一次访问到某个节点的时候,再把他放在优先队列中
	//第一个入队的节点是起点
	dist[s] = 0;
	Node node;
	node.dist = dist[s];
	node.x = s;
	pqueue.push(node);
	while (pqueue.empty() == false) {
		int x = pqueue.top().x;
		pqueue.pop();
		if (isvisit[x] == true) {
			continue;
		}
		isvisit[x] = true;
		for (int i = 0; i < graph[x].size(); i++) {
			//以x起点的所有的关联边
			int y=graph[x][i].y;
			int weight = graph[x][i].weight;
			//如果起点到y的距离大于先到x再到y的距离
			if (dist[y] > dist[x] + weight) {
				dist[y] = dist[x] + weight;
				node.dist = dist[y];
				node.x = y;
				pqueue.push(node);
			}
		}

	}
	if (dist[t] != INT_MAX) {
		return dist[t];
	}
	else {
		return -1;
	}

}
int main() {
	int n, m;
	while (scanf_s("%d%d", &n, &m) != EOF) {
		for (int i = 0; i < n; i++) {//清理每个链中的残留
			graph[i].clear();
		}
		for (int i = 0; i < m; i++) {
			int x, y, weight;
			scanf_s("%d%d%d", &x, &y, &weight);
			//将边的信息填入链表
			Edge edge;
			edge.y = y;
			edge.weight = weight;
			graph[x].push_back(edge);
			edge.y = x;
			graph[y].push_back(edge);
		}
		int s, t;

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
迪杰斯特拉(Dijkstra)算法是一种用于解带权有向图中单源最短路径算法。下面是一个使用C语言实现的简单示例: ```c #include <stdio.h> #include <limits.h> #define V 9 // 图的顶数 // 找到距离数组dist[]中最小值的顶索引 int minDistance(int dist[], bool visited[]) { int min = INT_MAX, minIndex; for (int v = 0; v < V; v++) { if (visited[v] == false && dist[v] <= min) { min = dist[v]; minIndex = v; } } return minIndex; } // 打印最短路径结果 void printSolution(int dist[]) { printf("顶\t距离\n"); for (int i = 0; i < V; i++) { printf("%d\t%d\n", i, dist[i]); } } void dijkstra(int graph[V][V], int src) { int dist[V]; // 存储最短路径 bool visited[V]; // 标记顶是否被访问过 // 初始化距离数组和访问标记数组 for (int i = 0; i < V; i++) { dist[i] = INT_MAX; visited[i] = false; } dist[src] = 0; // 源顶到自身的距离为0 for (int count = 0; count < V - 1; count++) { int u = minDistance(dist, visited); visited[u] = true; for (int v = 0; v < V; v++) { // 更新距离数组 if (!visited[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] + graph[u][v] < dist[v]) { dist[v] = dist[u] + graph[u][v]; } } } printSolution(dist); } int main() { int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0}, {4, 0, 8, 0, 0, 0, 0, 11, 0}, {0, 8, 0, 7, 0, 4, 0, 0, 2}, {0, 0, 7, 0, 9, 14, 0, 0, 0}, {0, 0, 0, 9, 0, 10, 0, 0, 0}, {0, 0, 4, 14, 10, 0, 2, 0, 0}, {0, 0, 0, 0, 0, 2, 0, 1, 6}, {8, 11, 0, 0, 0, 0, 1, 0, 7}, {0, 0, 2, 0, 0, 0, 6, 7, 0}}; dijkstra(graph, 0); return 0; } ``` 以上示例演示了如何使用迪杰斯特拉算法找到带权有向图中从源顶到其他顶最短路径。在上述示例中,我们使用了一个9个顶的图,并使用邻接矩阵来表示图的连接关系。你可以根据自己的需调整图的大小和边的权重。运行程序后,将输出每个顶到源顶的最短距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值