最短路
yyyyyuanxian
超爱吃曲奇
展开
-
POJ3268-- Silver Cow Party dijkstra优先队列求解
题目链接 题意 给n个点,m条有向边,在n个点里选一个点x,求所有点到x以及x到所有点的最短距离之和的最大值。 思路 dijkstra算法求的是一个点到所有点的最短距离,题目除此之外还有求所有点到一个点的最短距离。这一步处理很妙,只要把边的方向取反,再用一次dijkstra求的就是所有点到一个点的最短距离。所以我们用两次dijkstra就可以了。 代码 #include<iostream> #include<vector> #include<queue> using na原创 2021-01-16 20:22:05 · 59 阅读 · 0 评论 -
任意两点间的最短路问题Floyd-Walshall算法
复杂度为O(V³) int d[MAX_V][MAX_V];//d[u][v]表示边e=(u,v)的权值(不存在时设为INF,不过d[i][i]=0) int V;//顶点数 void walshall_floyd(){ for(int k=0;k<V;k++){ for(int i=0;i<V;i++){ for(int j=0;j<V;j++){ d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } } } } ..原创 2021-01-14 20:49:28 · 102 阅读 · 0 评论 -
Bellman-Ford算法
复杂度为O(|V||E|) //从顶点from指向顶点to的权值为cost的边 struct edge ( int from, to, cost; }. edge es [MAX_E]; //边 int d MAX_V ;//最短距离 int V, E;// V是顶点数,E是边数 //求解从顶点出发到所有点的最短距离 void shortest_path(int s){ for (int i =0; i < V; i++) a[i] =INF; a[s]=0: while (true)原创 2021-01-14 20:41:11 · 47 阅读 · 0 评论 -
dijkstra邻接矩阵和堆模板
邻接矩阵 复杂度O(|V|²) #include<bits/stdc++.h> using namespace std; const int MAX_V=2005; const int INF=9999999; int cost[MAX_V][MAX_V];//cost[u][v]表示边(u,v)的权值(不存在这条边时设为INF) int d[MAX_V];//顶点s出发的最短距离 bool used[MAX_V];//已经使用过的图 int V;//顶点数 //求从起点s出发到各个顶点原创 2021-01-14 19:54:25 · 86 阅读 · 0 评论