POJ 2502 - Subway Dijkstra堆优化试水

时间:2021-07-05 09:05:17

做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法。

Dijkstra算法概述

//从0出发的单源最短路

dis[][] = {INF}
ReadMap(dis);
for i = 0 -> n - 1
d[i] = dis[0][i]
while u = GetNearest(1 .. n - 1, !been[])
been[u] = 1
for_each edge from u
d[edge.v] = min(d[edge.v], d[u] + dis[u][edge.v])

上述算法遍历所有节点,每次 GetNearest() 循环一次,并遍历了所有边,算法复杂度 O(V2+E) = O(V2)

其中 GetNearest() 总取未去过的点中 d[] 最小的点,可以用小根堆维护 d[] 数组优化。

堆优化算法概述

//从0出发的单源最短路

dis[][] = {INF}
ReadMap(dis);
for i = 0 -> n - 1
d[i] = dis[0][i]
heap = BuildHeap(d[], n - 1)
while u = heap.pop()
been[u] = 1
for_each edge from u
if IsBetterDist()
d[edge.v] = d[u] + dis[u][edge.v]
heap.up(heap.IndexOf(edge.v))

建堆 O(VlogV),n次pop() O(VlogV),遍历边并更新堆 O(ElogV),优化算法复杂度 O(2VlogV+ElogV) = O((V+E)logV)

其他心得

* 几何类最短路一定要判重点

//POJ 2502
//Dijkstra 堆优化试水
//飘忽不定的迷离的数据
//AC 2016-10-17 #define HEAP_OPTIMIZE #include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define MAXN (200 + 2) + 10
#define SUB_SPEED (40000.0 / 60.0)
#define WALK_SPEED (10000.0 / 60.0)
#define INF 0x7f7f7f7f
using namespace std; inline double sqr(int x){
return x * x;
} struct point {
int x, y;
point(){}
point(int X, int Y): x(X), y(Y){}
inline double norm(){
return sqrt(sqr(x) + sqr(y));
}
friend point operator - (const point &p1, const point &p2){
return point(p1.x - p2.x, p1.y - p2.y);
}
friend bool operator == (const point &p1, const point &p2){
return (p1.x == p2.x) && (p1.y == p2.y);
}
}pt[MAXN]; int n = 0; int IndexOf(const point &p){
for (int i = 0; i < n; i++){
if (pt[i] == p)
return i;
}
return n;
} double dis[MAXN][MAXN], d[MAXN];
bool been[MAXN]; struct BHeap{
int heap[MAXN], n, index[MAXN];
BHeap(): n(0){}
BHeap(int N): n(0){
for (int i = 1; i <= N; i++)
push(i);
}
void down(int i){
for (int j = 2 * i; j <= n; j <<= 1){
j += j < n && d[heap[j]] > d[heap[j + 1]];
if (d[heap[j]] < d[heap[i]]){
swap(index[heap[i]], index[heap[j]]);
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
void up(int i){
for (int j = i/2; j > 0; j >>= 1){
if (d[heap[j]] > d[heap[i]]){
swap(index[heap[i]], index[heap[j]]);
swap(heap[i], heap[j]);
i = j;
}
else break;
}
}
void push(int a){
heap[++n] = a;
index[a] = n;
up(n);
}
int pop(){
if (!n) return 0;
swap(index[heap[1]], index[heap[n]]);
swap(heap[1], heap[n--]);
down(1);
return heap[n + 1];
}
}heap; int main(){
freopen("fin.c", "r", stdin);
for (int i = 0; i < MAXN; i++)
for(int j = 0; j < MAXN; j++)
if (i == j) dis[i][j]=0;
else dis[i][j] = INF;
scanf("%d%d%d%d", &pt[0].x, &pt[0].y, &(pt[1].x), &pt[1].y);
n = 2;
dis[0][1] = dis[1][0] = (pt[0] - pt[1]).norm() / WALK_SPEED;
point p;
while (~scanf("%d%d", &p.x, &p.y)){
int iter = IndexOf(p), olditer = - 1;
if (iter == n){
pt[n++] = p;
for (int i = 0; i < n; i++){
dis[i][n - 1] = dis[n - 1][i] = (pt[n - 1] - pt[i]).norm() / WALK_SPEED;
}
}
olditer = iter;
while (scanf("%d%d", &p.x, &p.y), ~p.x && ~p.y){
int iter = IndexOf(p);
if (iter == n){
pt[n++] = p;
for (int i = 0; i < n; i++){
if (i != olditer)
dis[i][n - 1] = dis[n - 1][i] = (pt[n - 1] - pt[i]).norm() / WALK_SPEED;
else
dis[i][n - 1] = dis[n - 1][i] = (pt[n - 1] - pt[i]).norm() / SUB_SPEED;
}
}
else dis[olditer][iter] = dis[iter][olditer] = (pt[iter] - pt[olditer]).norm() / SUB_SPEED;
olditer = iter;
}
}
for (int i = 1; i < n; i++)
d[i] = dis[0][i];
#ifdef HEAP_OPTIMIZE
heap = BHeap(n - 1);
#endif
while (
#ifdef HEAP_OPTIMIZE
int v = heap.pop()
#else
1
#endif
){
#ifndef HEAP_OPTIMIZE
double minn = INF;
int v = - 1;
for (int i = 1; i < n; i ++)
if ((!been[i])&&(d[i] < minn)){
minn = d[i];
v = i;
}
if (!~v) break;
#endif
been[v] = 1;
for (int i = 1; i < n; i++){
if (!been[i]){
if (d[v] + dis[v][i] < d[i]){
d[i] = d[v] + dis[v][i];
#ifdef HEAP_OPTIMIZE
heap.up(heap.index[i]);
#endif
}
}
}
}
printf("%.f\n", d[1]);
}