dijkstra

dijkstra堆优化板子

不羁岁月 提交于 2019-12-02 11:32:58
咕咕咕 、 #include<queue> #include<cstdio> #include<cstring> #include<algorithm> #define mk make_pair #define ll long long using namespace std; inline ll read()//快读 { ll sum = 0,p = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') p = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { (sum *= 10) += ch - '0'; ch = getchar(); } return sum * p; } const int N=100005; const int M=2e5+5; int n,m,s; int cnt,head[N]; ll dis[N]; bool vis[N]; struct edge { int nxt,to; ll wei; }e[M]; priority_queue< pair<ll,int> > q; void add(int x,int y,ll z) { e[++cnt].nxt=head[x]; e[cnt].to=y; e[cnt

FileReader is already defined in this compilation unit error Java

▼魔方 西西 提交于 2019-12-02 10:12:58
So I'm working on reading in a ".txt" file to use it to implement Dijkstra's algorithm, but every time I try to compile it gives me a "FileReader is already defined in this compilation unit" error while highlighting where I imported it in the beginning. If I take this out, however, it throws a constructor error when I'm trying to read in the file that it's of the wrong type. What am I missing here?? Here is my code: import java.io.BufferedReader; import java.io.File; //import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class

How to use boost::graph dijkstra's algorithm if vertex properties are pointers?

别说谁变了你拦得住时间么 提交于 2019-12-02 09:53:05
问题 I use boost graph to manage graphs and I need to make a maxmin tree. Now I'm trying to use boost dijkstra's algorithm, but I use a pointer to my class as a vertex property instead of using typedef property<vertex_index_t, int> my_prop , and I can't change it now. So how can I create predecessor_map and distance_map for my graph? My code looks like this (and these predecessor and distance maps don't work): struct LinkStruct {...}; class Node {...}; typedef Node* NodePtr; typedef adjacency_list

849. Dijkstra求最短路 I

匆匆过客 提交于 2019-12-02 07:38:49
给定一个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<algorithm> #include<cstring> using namespace std; //500个点 1e5条边,所以用邻接矩阵来写 const int N = 510; int n,m; int g[N][N];//邻接矩阵 int dist[N];//dijkstra的距离,表示从1到n的最短距离,当前的最短距离 bool st[N];//表示每个点的最短路是否确定 //边权为正所以不可能存在自环,重边的话只用保留两条边长度最短的那条边 int dijkstra(){ //首先初始化距离,初始化为正无穷 memset(dist,0x3f,sizeof dist);

dijkstra

六月ゝ 毕业季﹏ 提交于 2019-12-02 06:55:10
这是一个用来求没有负边权的最短路径算法,复杂度是n^3,经过优先队列优化则是n^2. 算法思想:首先用前向星存储图,用一个node(需要重载运算符)类的priority_queue来存储被松弛的点(vis[i]==0)的的信息,dis[]数组存放当前到达这个点的最短路。其次进行扫描,看堆顶,也就是当前dis[]最小的点,先将其出队,再看其所连的点是否可以进行松弛操作,如果可以松弛,那么更新,并加入优先队列,当队列为空的时候结束。 代码 1 #include<bits/stdc++.h> 2 #define maxn 1000005 3 #define maxm 2100000 4 using namespace std; 5 inline int read() 6 { 7 int x=0,k=1; char c=getchar(); 8 while(c<'0'||c>'9'){if(c=='-')k=-1;c=getchar();} 9 while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar(); 10 return x*k; 11 } 12 struct edge{ 13 int next; 14 int w; 15 int v; 16 }e[maxm]; 17 int n,m,tot=0,s; 18 int head[maxn

Shortest path in a maze with health loss

你。 提交于 2019-12-02 02:41:49
Suppose you have a dungeon, represented by a 2D matrix. You have a start point S (x1,y1) and an end point E (x2, y2). Along the way, some cells have a number in them, which subtract from your health score. Other cells are obstacles that you can't go through. You start with 5 health points, and you need to find the shortest path from S to E where you don't die on the way. I know that Dijikstra is used to find shortest paths. But in this case the shortest path might be one in which you die along the way. How do you find the shortest path where you don't die? Note that there is no advantage to

How to reconstruct paths from a multi-path Dijkstra?

こ雲淡風輕ζ 提交于 2019-12-02 01:15:26
问题 I am currently writing a PHP library for graphs. I have already implemented a single-path Dijkstra's algorithm successfully, but now struggle with implementing a multi-path version at the path reconstruction stage. Take following graph: This graph, for the sake of simplicity, has only paths from vertex A to J, going through multiple other vertices, which are all equal in cost, that is, the edge weight for each path add up to 10. The modified Dijkstra correctly produces the following output

How to reconstruct paths from a multi-path Dijkstra?

倖福魔咒の 提交于 2019-12-01 20:30:55
I am currently writing a PHP library for graphs. I have already implemented a single-path Dijkstra's algorithm successfully, but now struggle with implementing a multi-path version at the path reconstruction stage. Take following graph: This graph, for the sake of simplicity, has only paths from vertex A to J, going through multiple other vertices, which are all equal in cost, that is, the edge weight for each path add up to 10. The modified Dijkstra correctly produces the following output (which is the array $this->prev ): Array ( [A] => [B] => Array ( [0] => A ) [C] => Array ( [0] => A ) [D]

POJ - 2253 - Frogger = Dijkstra

人盡茶涼 提交于 2019-12-01 20:03:25
http://poj.org/problem?id=2253 题意:给一些点,完全图,边权是欧几里得距离,最小化从1号点到2号点的路径中的最大边权。 显然的Dijkstra乱搞题,以前还有用Prim改的,虽然Prim和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; double G[205][205]; double dis[205]; bool vis[205]; priority_queue<pair<double, int> > pq; void dijkstra(int s, int n) { while(!pq.empty()) pq.pop(); for(int i = 1; i <= n; ++i) { dis[i] = 1e18; vis[i] = 0; } dis[s] = 0; pq.push({-dis[s], s}); while

CUDA dijkstra's algorithm [closed]

◇◆丶佛笑我妖孽 提交于 2019-12-01 20:02:53
Has anybody implemented a CUDA parallelization version of Dijkstra's Algorithm for a given sparse matrix (cuSPARSE) graph, and for source, and target node, find the minimal K path? I really need it to solve a general graph I'll be constructing. Vincent 来源: https://stackoverflow.com/questions/15099054/cuda-dijkstras-algorithm