dijkstra

POJ 2502(dijkstra+建图)

匿名 (未验证) 提交于 2019-12-02 23:55:01
题目:输入x1,y1,x2,y2表示起点和终点的坐标。接下来输入地铁线 0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1 起点(0,0),终点(10000,1000)第一条地铁线是(0,200)--(5000,200)--(7000,200)第二条是--------------------------(自己看)文件结束输入哦主角是可以从任意点走到任意点的,速度是10000/60,坐地铁的话不用等,速度是40000/60;地铁是可以在中间站下车的例如(0,200)这样的点。双向的,而且两个相邻中间站的中间站之间是直线问:从起点到终点的最快时间思路:建图很重要,主角可以从起点走到终点,也可以从地铁的中间站上车。 要注意,假如有个地铁线路,我或许从第二个站台下车,直接走到第四个站台更快,所以当存地铁线路时候,当前站台(如果不是第一个)和上一个站台建立速度为40000/60的地铁线,而和上上个站台或许更上个站台建立速度为10000/60的路。注意double #include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map>

Dijkstra堆优化模板

匿名 (未验证) 提交于 2019-12-02 23:49:02
1 priority_queue< pair<ll,ll> >q; 2 void SPFA(ll top) 3 { 4 memset(bian,0,sizeof(bian)); 5 dis[top]=0;q.push(make_pair(0,top));bian[top]=1; 6 while(!q.empty()) 7 { 8 ll x=q.top().second;q.pop(); 9 for(ll i=head[x];i;i=e[i].n) 10 { 11 ll to=e[i].to; 12 if(dis[to]>dis[x]+e[i].w) 13 { 14 dis[to]=dis[x]+e[i].w; 15 if(bian[to]==0) 16 { 17 q.push(make_pair(-dis[to],to)); 18 bian[to]=1; 19 } 20 } 21 } 22 } 23 }

Dijkstra算法求最短路

匿名 (未验证) 提交于 2019-12-02 23:48:02
给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值。 请你求出1号点到n号点的最短距离,如果无法从1号点走到n号点,则输出-1。 输入格式 第一行包含整数n和m。 接下来m行每行包含三个整数x,y,z,表示点x和点y之间存在一条有向边,边长为z。 输出格式 输出一个整数,表示1号点到n号点的最短距离。 如果路径不存在,则输出-1。 数据范围 1 ≤ n ≤ 500 1≤n≤500, 1 ≤ m ≤ 10 5 1≤m≤105, 图中涉及边长均不超过10000。 输入样例: 3 3 1 2 2 2 3 1 1 3 4 输出样例: 3 #include<iostream> #include<cstring> #include<algorithm> using namespace std; const int N=510; int dis[N],g[N][N],m,n; bool st[N]; int dijkstra(){ memset(dis,0x3f,sizeof(dis)); dis[1]=0; for(int i=0;i<n;i++){ int t=-1; for(int j=1;j<=n;j++){ if(!st[j]&&(t==-1||dis[t]>dis[j])) t=j; } st[t]=true; for(int j=1;j<=n;j++){ dis

Why use Dijkstra's algorithm instead of Best (Cheapest) First Search?

左心房为你撑大大i 提交于 2019-12-02 23:38:41
From what I have read so far. The Best First Search seems faster in terms of finding the shortest path to the goal because Dijkstra's algorithm has to relax all the nodes as it traverses the graph. What makes Dijkstra's algorithm better than Best First Search? EDIT : Your edit clarifies you are interested in Best-First Search , and not BFS . Best-First Search is actually an informed algorithm , which expands the most promising node first. Very similar to the well known A* algorithm (actually A* is a specific best-first search algorithm). Dijkstra is uninformed algorithm - it should be used

dijkstra之zkw线段树优化

匿名 (未验证) 提交于 2019-12-02 23:32:01
其实特别好理解,我们只要写一个数据结构(线段树)支持一下操作: 1.插入一个数 \(x\) 。 2.查询当前数据结构中最小的数的插入编号。 3.删除插入编号为 \(x\) 的数。 第一眼看成可持久化了 其实就是一个单点修改,区间(全局)查询的线段树。 zkw线段树在普通线段树的基础上进行了优化(卡常神器)。 我们记录每一个点在线段树中叶子节点的编号。这样修改的时候就不用递归下去找了,直接一个while循环pushup上来就完事。 #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdlib> #include<cctype> #include<vector> #include<stack> #include<queue> using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db

Dijkstra算法python实现

匿名 (未验证) 提交于 2019-12-02 22:54:36
Dijkstra算法(迪科斯彻算法、迪杰斯特拉算法): 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法。是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题。迪杰斯特拉算法主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止。 提出者: 艾兹格・W・迪科斯彻 (Edsger Wybe Dijkstra,1930年5月11日~2002年8月6日)荷兰人。 计算机科学家,毕业就职于荷兰Leiden大学,早年钻研物理及数学,而后转为计算学。曾在1972年获得过素有计算机科学界的诺贝尔奖之称的图灵奖,之后,他还获得过1974年 AFIPS Harry Goode Memorial Award、1989年ACM SIGCSE计算机科学教育教学杰出贡献奖、以及2002年ACM PODC最具影响力论文奖。 Dijkstra算法python实现: import heapq import sys class Graph: def __init__(self): self.vertices = {} def add_vertex(self, name, edges): self.vertices[name] = edges def get_shortest_path(self, startpoint, endpoint):

最短路径问题 Dijkstra ――Python实现

匿名 (未验证) 提交于 2019-12-02 22:51:30
1 class Vertex: 2 #顶点类 3 def __init__(self,vid,outList): 4 self.vid = vid #出边 5 self.outList = outList #出边指向的顶点id的列表,也可以理解为邻接表 6 self.know = False #默认为假 7 self.dist = float('inf') #s到该点的距离,默认为无穷大 8 self.prev = 0 #上一个顶点的id,默认为0 9 def __eq__(self, other): 10 if isinstance(other, self.__class__): 11 return self.vid == other.vid 12 else: 13 return False 14 def __hash__(self): 15 return hash(self.vid) 1 #创建顶点对象 2 v1=Vertex(1,[2,3]) 3 v2=Vertex(2,[3,4]) 4 v3=Vertex(3,[5]) 5 v4=Vertex(4,[3,5,6]) 6 v5=Vertex(5,[6]) 7 v6=Vertex(6,[]) 8 9 #存储边的权值 10 edges = dict() 11 def add_edge(front,back,value): 12

Dijkstra算法的Java实现

匿名 (未验证) 提交于 2019-12-02 21:52:03
package main.java; 2 3 import main.java.utils.GraphUtil; 4 5 import java.util.ArrayDeque; 6 import java.util.List; 7 import java.util.Queue; 8 9 15 public class DijkstraTest { 16 17 //邻接矩阵的表示 18 public final static double[][] GRAPH_DISTANCE = GraphUtil.getDijkstraGraph(); 19 20 //起点到某节点的临时最短距离 21 public static double distance[] = new double[GRAPH_DISTANCE.length]; 22 23 //某节点的前驱节点 24 public static int pre[] = new int[GRAPH_DISTANCE.length]; 25 26 static int originIndex = 0, toIndex = 4; 27 28 29 public static void main(String[] args) { 30 31 init(); 32 findDijkstraShortestPath(); 33 } 34 35 /*

Dijkstra算法的Java实现

匿名 (未验证) 提交于 2019-12-02 21:52:03
1 package main.java; 2 3 import main.java.utils.GraphUtil; 4 5 import java.util.ArrayDeque; 6 import java.util.List; 7 import java.util.Queue; 8 9 10 /** 11 * @Tme 2019/9/12 10:40 12 * @Author chenhaisheng 13 * @Email:ecjutsbs@foxmail.com 14 */ 15 public class DijkstraTest { 16 17 //邻接矩阵的表示 18 public final static double[][] GRAPH_DISTANCE = GraphUtil.getDijkstraGraph(); 19 20 //起点到某节点的临时最短距离 21 public static double distance[] = new double[GRAPH_DISTANCE.length]; 22 23 //某节点的前驱节点 24 public static int pre[] = new int[GRAPH_DISTANCE.length]; 25 26 static int originIndex = 0, toIndex = 4; 27 28 29

How do you solve the 15-puzzle with A-Star or Dijkstra's Algorithm?

筅森魡賤 提交于 2019-12-02 20:44:35
I've read in one of my AI books that popular algorithms (A-Star, Dijkstra) for path-finding in simulation or games is also used to solve the well-known "15-puzzle". Can anyone give me some pointers on how I would reduce the 15-puzzle to a graph of nodes and edges so that I could apply one of these algorithms? If I were to treat each node in the graph as a game state then wouldn't that tree become quite large? Or is that just the way to do it? A good heuristic for A-Star with the 15 puzzle is the number of squares that are in the wrong location. Because you need at least 1 move per square that