转载请注明出处:http://blog.csdn.net/jiangshibiao/article/details/21377419
【序言】求单源最短路径一直是个很热门的话题。网上,大家也在争相比试dijkstra和SPFA的优越性。遗憾的是,我一直没学过dijkstra的堆优化,于是打算好好学习一下。以下是poj上随便找的一道最短路的题目。
【dijkstra的堆优化】试想,在普通的dij中,每次寻找的当前dis的最小值时,要花费O(N)的效率,这样实在太不佳了。我们基本的思想是构造一个最小堆,每次找最小值的时候直接取出堆顶即可。详见这里。
【STL的基本操作】
1.头文件 #include<queue>
2.定义 priority_queue<x,vector<y>,greater<z> >q; ——–>>>>>x、y、z是类型名,q是堆的变量名。其中vector是容器,greater可以使自己定义的bool类型(如sort的cmp)。当然,greater是系统已经定义好了的。
3.使用 q.push(x) 往堆中压入x这个值
q.size() 查询堆中的元素
q.pop() 弹出堆顶
q.top() 返回堆顶元素的值
【注意】我又新定义了一种点对类型,定义方法:typedef pair<x,y>q;x和y是点对类型中每个元素的类型。这样有什么好处呢?因为dij的堆中我要存两个元素:到此点的最小值和这个点的编号。用点对十分方便。
使用方法:make_pair(x,y)。返回点对的类型。
【题目】
Subway
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5708 | Accepted: 1846 |
Description
You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don’t want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Sample Input
0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
Sample Output
21
Source
【大意】
给出起点、终点的坐标。你要从起点走到终点。步行的速度是10km/h。(当然,你在两个点之间走,肯定是勾股距离最短)。再给出N条地铁线路,每条给出不小于2个的坐标,表示这些坐标构成一条线路。在地铁轨道中,速度是40km/h。我们认为无论何时你到某个站,都恰好有一辆车来。求起点到终点的最小时间。
【代码】
#include<stdio.h>
#include<queue>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=200+5;
double d[maxn],map[maxn][maxn],ans;
int x[maxn],y[maxn],n,i;
bool flag[maxn];
typedef pair<double,int>Pair;
double dis(int x1,int y1,int x2,int y2)
{
return sqrt(double((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)))/1000;
}
double dijkstra(int start,int end)
{
for (int i=1;i<=n;i++) d[i]=2100000000;
memset(flag,0,sizeof(flag));
d[start]=0;
priority_queue< Pair,vector<Pair>,greater<Pair> >q;
q.push(make_pair(d[start],start));
while (!q.empty())
{
Pair top=q.top();
q.pop();
int now=top.second;
if (flag[now]) continue;
flag[now]=true;
for (int j=1;j<=n;j++)
if ((!flag[j])&&(map[now][j]+d[now]<d[j]))
{
d[j]=d[now]+map[now][j];
q.push(make_pair(d[j],j));
}
}
return d[end];
}
void read()
{
for (int i=1;i<maxn;i++)
for (int j=1;j<maxn;j++)
map[i][j]=2100000000;
int start=n+1,xx,yy;
while (scanf("%d%d",&xx,&yy)!=EOF)
{
if (xx==-1&&yy==-1)
{
for (int i=start;i<n;i++)
{
double temp=dis(x[i],y[i],x[i+1],y[i+1])/40;
if (temp<map[i][i+1]) map[i][i+1]=map[i+1][i]=temp;
}
start=n+1;
continue;
}
n++;
x[n]=xx;y[n]=yy;
}
for (int i=1;i<n;i++)
for (int j=i+1;j<=n;j++)
{
double temp=dis(x[i],y[i],x[j],y[j])/10;
if (temp<map[i][j]) map[i][j]=map[j][i]=temp;
}
}
int main()
{
scanf("%d%d%d%d",&x[1],&y[1],&x[2],&y[2]);
n=2;read();
ans=dijkstra(1,2);ans*=60;
printf("%.f",ans);
return 0;
}