POJ 2502 Subway(迪傑斯特拉)
阿新 • • 發佈:2018-12-22
Subway
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 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 is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Time Limit: 1000MS | Memory Limit: 65536K |
Total Submissions: 6692 | Accepted: 2177 |
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
Output
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
題目連結:http://poj.org/problem?id=2502
題目大意:給出家和學校座標表示固定的起點和終點,接下來每一行代表一條地鐵線,輸入該條地鐵線的每個站點的座標,以-1,-1(不是座標)結束一行,保證地鐵線沿直線,且至少有兩站。給出乘地鐵和步行的不同速度,求一個人從家到學校用到的最小時間。
解題思路:構建無向圖,時間為權值,用迪傑斯特拉演算法,求家到學校兩個結點最短路徑問題。同一條地鐵線上兩站間的時間先計算出來,輸入結束,沒有權值任意兩以步行的速度計算時間。注意輸入格式,以EOF結束。
補充:Dijkstra演算法適用於權值都為正的圖結構,dist[ i ]陣列儲存 i 到起點v0 的最短路長度,初始為鄰接矩陣eg[v0][i]值無窮大.遍歷n-1次找出n-1條最短路。s[i ]陣列記錄結點是否已確定最小dist[ i ],初始為0,確定後為1,找到的i值賦為u,以u為起點找下一個距離u最近的節點j,更新條件:dist [ j ] = min(dist [ u ]+eg [ v0 ] [ u ],dist [ j ]),保證每個點距離起點的距離最短。如果要記錄路徑,要用到path陣列,如果通過u找到了j,path [ j ]=u記錄即可。
程式碼如下:
#include <cstdio>
#include <cstring>
#include <cmath>
#define INF 100000000.0;
int const maxn=400;
int s[maxn];
double dist[maxn];
double eg[maxn][maxn];
int num;
struct A
{
double x,y;
}stop[maxn];
double lenth(double x1,double y1,double x2,double y2)
{
double a=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(a);
}
void Dijkstra(int v0)
{
int i,j;
for(i=0;i<num;i++)
{
s[i]=0;
dist[i]=eg[v0][i];
}
s[v0]=1;
for(i=0;i<num-1;i++)
{
double min=INF;
int u;
for(j=0;j<num;j++)
{
if(!s[j] && dist[j]<min)
{
u=j;
min=dist[j];
}
}
s[u]=1;
for(j=0;j<num;j++)
{
if(!s[j] && dist[u]+eg[u][j]<dist[j])
dist[j]=dist[u]+eg[u][j];
}
}
}
int main()
{
int i,j;
double x1,x2,y1,y2;
memset(eg,0,sizeof(eg));
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
stop[0].x=x1;
stop[0].y=y1;
stop[1].x=x2;
stop[1].y=y2;
num=2;
int p=0;
while(scanf("%lf%lf",&stop[num].x,&stop[num].y)!=EOF)
{
if(stop[num].x==-1 && stop[num].y==-1)
{
p=0;
continue;
}
if(!p)
{
p=1;
num++;
continue;
}
eg[num-1][num]=eg[num][num-1]=lenth(stop[num-1].x,stop[num-1].y,stop[num].x,stop[num].y)/4000.0;
num++;
}
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
if(eg[i][j]==0 )
eg[i][j]=eg[j][i]=lenth(stop[i].x,stop[i].y,stop[j].x,stop[j].y)/1000.0;
}
Dijkstra(0);
printf("%d\n",int(0.5+6.0*dist[1]) );
return 0;
}