dijkstra

模板 - 单源最短路Dijkstra

半腔热情 提交于 2019-12-01 19:48:43
int dis[105]; bool vis[105]; priority_queue<pair<int, int> > pq; void dijkstra(int s, int n) { while(!pq.empty()) pq.pop(); for(int i = 1; i <= n; ++i) { dis[i] = 0x3f3f3f3f; vis[i] = 0; } dis[s] = 0; pq.push({-dis[s], s}); while(!pq.empty()) { int u = pq.top().second; pq.pop(); if(vis[u]) continue; vis[u] = 1; for(int i = 0; i < G[u].size(); ++i) { int v = G[u][i].first, w = G[u][i].second; if(!vis[v] && dis[v] > dis[u] + w) { dis[v] = dis[u] + w; pq.push({-dis[v], v}); } } } } 来源: https://www.cnblogs.com/Inko/p/11715302.html

POJ - 1062 - 昂贵的聘礼 = Dijkstra

匆匆过客 提交于 2019-12-01 19:47:05
http://poj.org/problem?id=1062 题意:有100个物品,每个物品有一个价格值,一个地位值,和他可以用别的物品来补差价换。求换到1号物品的最小代价。 意思就是给一个图,支付起点的点权,然后走边权走到1号点,求最小的代价,其中路上经过的地位值的差不能超过题目的限制。 最暴力的做法,枚举地位的差,每次在反图上跑一次Dijkstra就好。这样貌似进行了多余的拷贝。 #include<algorithm> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<map> #include<set> #include<stack> #include<string> #include<queue> #include<vector> using namespace std; typedef long long ll; vector<pair<int, int> >G0[105]; vector<pair<int, int> >G[105]; struct Item { int id; int st; int val; } item[105]; int dis[105]; bool vis[105]; priority_queue<pair<int, int> > pq;

Find the shortest path with the least number of edges

旧城冷巷雨未停 提交于 2019-12-01 17:58:29
I need to modify Dijkstra's algorithm so that if there are several shortest paths I need to find the one with minimum number of edges on the path. I've been stuck on how to use Dijkstra's method to find multiple shortest paths, how do you do that? doesn't it always output only 1 shortest path? pseudo code or any general direction would be very helpful. Instead of assigning every node with the distance from source you can assign the number of edges traversed so far also. So each node will have (distance,edges) instead of distance only. Everything else works as usual and for every terminal node

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

☆樱花仙子☆ 提交于 2019-12-01 17:44:13
# 最短路径算法 Dijkstra # 输入:含权有向图 G=(V,E),V={1,2,3...n} # 输出:G中顶点 1 到各个顶点地最短距离 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

Dijkstra's algorithm: What to do if there are two or more nodes with minimal weight?

泪湿孤枕 提交于 2019-12-01 16:51:49
In Dijkstra's algorithm what should I do if there are two or more nodes with minimal weights at a point in the algorithm? In wikipedia: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm at step no. 6, it says "Set the unvisited node marked with the smallest tentative distance as the next 'current node' and go back to step 3." What if there are two or more nodes with "smallest tentative distance". Can anyone help me with the algorithm? Short Answer Just pick either. Unless you have another heuristic to work with, you can't tell which would be better to pick. Bit More Explaination Think about

Dijkstra's algorithm: What to do if there are two or more nodes with minimal weight?

风流意气都作罢 提交于 2019-12-01 15:26:43
问题 In Dijkstra's algorithm what should I do if there are two or more nodes with minimal weights at a point in the algorithm? In wikipedia: http://en.wikipedia.org/wiki/Dijkstra%27s_algorithm at step no. 6, it says "Set the unvisited node marked with the smallest tentative distance as the next 'current node' and go back to step 3." What if there are two or more nodes with "smallest tentative distance". Can anyone help me with the algorithm? 回答1: Short Answer Just pick either. Unless you have

Find total of second variable related to the distance of route from get.shortest.paths()

亡梦爱人 提交于 2019-12-01 12:17:41
I found the following question ( Find distance of route from get.shortest.paths() ) very helpful, but would like to take it one step further. I have added one column to the data frame and I would like to get the "total distance" related to the min newcost path. Below the igraph / R code that I used. df2 = rbind(c(234,235,21.6,75), c(234,326,11.0,35), c(235,241,14.5,78), c(326,241,8.2,98), c(241,245,15.3,75), c(234,245,38.46,65)) df2 = as.data.frame(df2) names(df2) = c("start_id","end_id","newcost","distance") df2 require(igraph) g2 <- graph.data.frame(df2, directed=FALSE) tkplot(g2) (tmp2 =

Find total of second variable related to the distance of route from get.shortest.paths()

元气小坏坏 提交于 2019-12-01 11:54:06
问题 I found the following question ( Find distance of route from get.shortest.paths() ) very helpful, but would like to take it one step further. I have added one column to the data frame and I would like to get the "total distance" related to the min newcost path. Below the igraph / R code that I used. df2 = rbind(c(234,235,21.6,75), c(234,326,11.0,35), c(235,241,14.5,78), c(326,241,8.2,98), c(241,245,15.3,75), c(234,245,38.46,65)) df2 = as.data.frame(df2) names(df2) = c("start_id","end_id",

Dijkstra's algorithm with back tracking?

女生的网名这么多〃 提交于 2019-12-01 11:18:00
In a related thread , it was suggested that I impliment Dijkstra's algorithm for finding the shortest path on a graph. Looking at this .gif of the algorithm from wikipedia: What if the path 1,3,6,5 turned out to be very low cost? For example, the weight on 3-6 was 1, and the weight on 6-5 was 2? Dijkstra's algorithm would not consider this path because it only looks one step ahead; it skipped node 3. Is it acceptable to specify a parameter that makes the algorithm look 2,3,4...n steps ahead before choosing each node? I realize that this could potentially blow up computational time, but as long

Shortest path in “two-graph” with limited number of changes

对着背影说爱祢 提交于 2019-12-01 11:05:52
问题 Let's say we have two directed and positive-weighted graphs on one set of vertices (first graph represents for example rail-roads and the second one - bus lanes; vertices are bus stops or rail-road stations or both). We need to find the shortest path from A to B, but we can't change the type of transport more than N times. I was trying to modify the Dijkstra's algorithm, but it's working only on a few "not-so-mean-and-complicated" graphs and I think I need to try something different. How to