最短路径

图(1):最短路径

蹲街弑〆低调 提交于 2019-11-28 12:26:17
Dijkstra算法 1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible. Input Specification: Each input file contains one test case. For each test case, the first line

最短路径之Bellman-Ford算法——动态规划

删除回忆录丶 提交于 2019-11-28 08:18:09
文章目录 1.算法原理 2. 算法流程 3.Bellman-Ford算法的实现 4.单源单目的地的特殊写法 参考资料 Bellman-Ford算法 主要针对 带有负值的单源最短路径问题 ,当有向图带有其权小于0的边的时候,不能使用 迪杰斯特拉算法 ,但是只要不是带负权的回路,我们依然可以使用 Bellman-Ford算法 。 1.算法原理 Bellman-Ford算法 的核心思想是 动态规划 ,即我们需要定义 子问题的状态 和 动态规划递归式 。 讨论前提 如果图中共有 n n n 个顶点,则所有的最短路径最多只有 n − 1 n-1 n − 1 条边。 如果一条路径具有 n n n 条以上的边,则一定有环路(参考图的最小生成树性质)。 而由于环路的权值都不小于0,则去掉环路后的路径会更短,所以 两个连通的顶点之间不存在含有环路的最短路径,且最多有n-1条边 。 动态规划公式 所以,我们像迪杰斯特拉算法一样关注 最短路径的长度 , 令 d ( v , k ) d(v,k) d ( v , k ) 表示源点 s s s 到顶点 v v v 且最多含有 k k k 条边的最短路径,于是 d ( v , n − 1 ) d(v,n-1) d ( v , n − 1 ) 就是我们的目标。 首先,对于 k = 0 k=0 k = 0 有, d ( v , 0 ) = { 0 , v = s

bfs(最短路径)

本秂侑毒 提交于 2019-11-28 05:04:49
http://poj.org/problem?id=3278 Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 146388 Accepted: 44997 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer John has two modes of transportation: walking and teleporting. * Walking: FJ can move from any point X to the points X - 1 or X + 1 in a single minute * Teleporting: FJ can move from any point X to the point 2 × X in a

最短路径

余生颓废 提交于 2019-11-28 04:14:10
1:Bellman-Ford 算法 计算复杂度为 NM,只能计算单源,但是可以检测负值回环 操作流程: 1:选取所有边,以该边来进行松弛操作 2:循环n-1次 function [dist, pre]=bellmanford_ShortestPath(w,s) %% initilization n=size(w,1); dist=ones(1,n)*inf; pre=zeros(1,n)*NaN; dist(s)=0;%% 进行松弛操作 进行n-1 次,每次遍历所有边 for k=1:n-1 for i=1:n for j=1:n if dist(j)>dist(i)+w(i,j) dist(j)=dist(i)+w(i,j); pre(j)=i; end end end end%% 回环确定 for i=1:n for j=1:n if w(i,j)~=inf if (dist(j)>dist(i)+w(i,j)) error('negetive weight circut') end end end end   2:Floyd 算法 该算法最简单,但是可以计算多源路径,复杂度为 N*N*N function [D,path]=Floyd_ShortestPath(w) n=size(w,1); D=w; path=zeros(n,n); for k=1:n for i=1:n

AcWing 黑暗城堡

孤人 提交于 2019-11-28 01:44:42
AcWing 黑暗城堡 Description 在顺利攻破Lord lsp的防线之后,lqr一行人来到了Lord lsp的城堡下方。 Lord lsp黑化之后虽然拥有了强大的超能力,能够用意念力制造建筑物,但是智商水平却没怎么增加。 现在lqr已经搞清楚黑暗城堡有N个房间,M条可以制造的双向通道,以及每条通道的长度。 lqr深知Lord lsp的想法,为了避免每次都要琢磨两个房间之间的最短路径,Lord lsp一定会把城堡修建成 树形 的。 但是,为了尽量提高自己的移动效率,Lord lsp一定会使得城堡满足下面的条件: 设 D[i] 为如果所有的通道都被修建,第 i 号房间与第1号房间的最短路径长度;而 S[i] 为实际修建的树形城堡中第 i 号房间与第1号房间的路径长度;要求对于所有整数 i,有 S[i]=D[i] 成立。 为了打败Lord lsp,lqr想知道有多少种不同的城堡修建方案。 你需要输出答案对 2 ^ 31–1 取模之后的结果。 Input 第一行有两个整数 N 和 M。 之后 M 行,每行三个整数X,Y 和L,表示可以修建 X 和 Y 之间的一条长度为 L 的通道。 Output 一个整数,表示答案对 2 ^ 31–1 取模之后的结果。 Sample Input 3 3 1 2 2 1 3 1 2 3 1 Sample Output 2 题解: 题解来自mzx

最短路径(巧妙的矩阵交换)

混江龙づ霸主 提交于 2019-11-28 01:26:00
Silver Cow Party Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35984 Accepted: 16104 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1.. N is going to attend the big cow party to be held at farm # X (1 ≤ X ≤ N ). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires T i (1 ≤ T i ≤ 100) units of time to traverse. Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow's return route might be different

[USACO09DEC]牛收费路径Cow Toll Paths(floyd、加路径上最大点权值的最短路径)

谁都会走 提交于 2019-11-27 22:56:43
https://www.luogu.org/problem/P2966 题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has set up a series of tolls that the cows will pay when they traverse the cowpaths throughout the farm. The cows move from any of the N (1 <= N <= 250) pastures conveniently numbered 1..N to any other pasture over a set of M (1 <= M <= 10,000) bidirectional cowpaths that connect pairs of different pastures A_j and B_j (1 <= A_j <= N; 1 <= B_j <= N). FJ has assigned a toll L_j (1 <= L_j <= 100,000) to the path connecting pastures A_j and B_j. While there may be

Dijkstra迪杰斯特拉算法

让人想犯罪 __ 提交于 2019-11-27 22:17:12
迪杰斯特拉算法是用于求解图的单元最短路径问题,即某个源点到达图中其余顶点的最短路径,其核心思想是每次从剩余未归入路径的顶点中找到一个到达当前路径距离最短的顶点,将其归入路径中,共执行n-1次。该算法需要三个辅助数组,s[ ]数组用来标记各个顶点有没有被归入当前路径中,dist[ ]数组用于表示当前源点到达各个顶点的最短路径长度,path[ ]数组用来存储该顶点在最短路径中的前驱结点。 #include<stdio.h> //迪杰斯特拉算法,求带权图中某个源点到到达其余各个顶点的最短路劲,其需要三个辅助数组,s[i]=1表示 //该顶点已经求得最短路径,0表示未求得;dist[]数组用来存储当前源点到达各个顶点的最短路径;path[] //数组存储源点到达该顶点的最短路径中其前驱结点 void Dijkstra(Mgraph G,int v,int dist[],int path[]) { int[MAXSIZE]; int i,j,min,u; //初始化各个数组 for(i = 0;i < G.vexnum;++i){ dist[i] = G.edges[v][i]; //dist[]初始时存储各个顶点到达源点的带权路径长度 s[i] = 0; //一开始各个顶点均未规划进入路径 if(dist[i] < 65535) path[i] = v; //如果顶点与源点有弧

最短路径算法--A*算法实现

岁酱吖の 提交于 2019-11-27 15:48:54
最短路径算法--A*算法实现 定义 算法介绍 算法原理 算法实现 实验对比 定义 A*算法是最短路径算法中的一种启发式算法。 去看 原文 算法介绍 A*算法是一个目标导向的算法。为什么这么说了,这是因为A*在Dijkstra算法的基础上利用了一个启发式函数,该函数被定义为某个节点到目标点的距离(欧式距离、曼哈顿距离等),可以表示为h(u,t),其中u是任意一个节点,t是目标点,所以A*算法的每个节点的优先值为f(u)=dist(s,u)+h(u,t)。这个启发式函数的作用就是能够让那些靠近目标点的节点先被访问到,从而驱动搜索方向朝目标点方向推进。就是这个小小的改进就可以让算法的效率有一定提升。 它的搜索空间是这样的: 算法原理 A*算法的基本步骤和Dijkstra算法是一样的,也需要一个优先队列,唯一不一样的便是这个启发值,当你把启发值设为0时,也就是Dijkstra算法。 算法实现 伪代码 这是松弛函数,红色框中便是加入了欧式距离启发值。 实验对比 测试部分比较了单向Dijkstra算法、双向Dijkstra算法以及A*算法的效率。随机从武汉路网中取了100对OD,分别记录每对OD最短路径计算的运行时间以及扩展节点数量。 从图中,我们可以很清楚的看到A*的运行时间还是要优于Dijkstra算法的,搜索空间A*算法也是比Dijkstra算法要小,但是A

最短路径(Dijkstra)

不羁岁月 提交于 2019-11-27 15:12:23
最短路径问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 43967 Accepted Submission(s): 12599 Problem Description 给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 Input 输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。 (1<n<=1000, 0<m<100000, s != t) Output 输出 一行有两个数, 最短距离及其花费。 Sample Input 3 2 1 2 5 6 2 3 4 5 1 3 0 0 Sample Output 9 11 Source 浙大计算机研究生复试上机考试-2010年 这道题用floyd算法会超时 两个权值的Dijkstra。。 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include