dijkstra队列优化链式前向星(C++、Java)
2017-10-13 21:40
393 查看
C++
Java
#include <iostream> #include <queue> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int maxm = 4010; //边 const int maxn = 1010; //点 const int inf = 0x3f3f3f3f; struct Edge { int to,v,next; } edge[maxm]; struct node { int num, val; //存编号 和距离 node(int _num=0, int _val=0):num(_num), val(_val) {} bool operator <(const node &tmp) const { return val > tmp.val; } }; int head[maxn]; int top; int n,m; int dis[maxn]; bool vis[maxn]; void init() { memset(head, -1, sizeof(head)); top = 0; } void addedge(int from, int to, int v) { edge[top].to = to; edge[top].v = v; edge[top].next = head[from]; head[from] = top++; } void dijkstra(int st) //起点 { priority_queue<node> que; memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[st] = 0; que.push(node(st, 0)); while(!que.empty()) { node p = que.top(); que.pop(); int nown = p.num; if(vis[nown]) continue; vis[nown] = true; for(int i=head[nown]; i!=-1; i=edge[i].next) { Edge e = edge[i]; if(dis[e.to] > dis[nown] + e.v && !vis[e.to]) { dis[e.to] = dis[nown] + e.v; que.push(node(e.to,dis[e.to])); } } } } int main() { return 0; }
Java
import java.util.*; class Edge { public int to; public int next; public double v; public Edge(int _to, int _next, double _v){ this.to = _to; this.next = _next; this.v = _v; } } class node { public int num; public double val; public node(int _num, double _val){ this.num = _num; this.val = _val; } } public class dijk { private int cnt = 0; private double[] dis; private boolean[] vis; private int[] head; private Edge[] edge; private Queue<node> pq; private Comparator<node> Order; public dijk(int _n, int _m) //n个点 m条边 { dis = new double[_n+1]; vis = new boolean[_n+1]; head = new int[_n+1]; edge = new Edge[_m+1]; Arrays.fill(head,-1); Arrays.fill(dis, 0x3f); Arrays.fill(vis, false); Order = new Comparator<node>() { public int compare(node a, node b) { if(a.val > b.val) return 1; else if(a.val > b.val) return -1; else return 0; } }; pq = new PriorityQueue<node>(Order); } public void addEdge(int from, int to, double v){ edge[cnt] = new Edge(to,head[from],v); head[from] = cnt++; } public double cal(int st,int ed){ dis[st] = 0; pq.add(new node(st,0)); while(!pq.isEmpty()){ node p = pq.poll(); int nown = p.num; if(vis[nown]) continue; vis[nown] = true; for(int i=head[nown]; i!=-1; i=edge[i].next){ Edge e = edge[i]; if(dis[e.to] > dis[nown] + e.v && !vis[e.to]){ dis[e.to] = dis[nown] + e.v; pq.add(new node(e.to,dis[e.to])); } } } return dis[ed]; } }
相关文章推荐
- Java并发框架——AQS阻塞队列管理(二)——自旋锁优化
- 优先队列优化Dijkstra-hdu2066
- UVa 658 It's not a Bug, it's a Feature! 位运算技巧+dijkstra优先队列优化
- hdu 2112 HDU Today 优先队列优化的Dijkstra+map
- C++之路进阶——队列优化dp(玩具装箱)
- 队列的图文解析 和 对应3种语言的实现(C/C++/Java)
- hdu 1874 畅通工程续 (Dijkstra + 优先队列优化)
- hdu1874(dijkstra解法优先队列优化)
- [codevs1021] 玛丽卡 优先队列优化dijkstra
- Dijkstra队列优化矩阵版
- 用优先级队列优化dijkstra
- usaco2.4 comehome 最短路 Floyd, Dijkstra, Dijkstra优先队列优化
- CUGB图论专场:K - The Shortest Path in Nya Graph(dijkstra优先队列优化+线性构图入边)
- pku 1511 Invitation Cards (SPFA 和 dijkstra+优先级队列优化)
- Dijkstra普通算法及优先队列优化
- 【C++心路历程36】单调队列优化多重背包
- AOJ 2249 Road Construction (dijkstra+队列优化)
- 优先队列(C++/Java)
- Dijkstra + 优先队列优化 模板
- poj 1847 最短路 dijkstra模板(vector邻接表+队列优化)