POJ 2502 Subway(迪杰斯特拉)

Subway
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

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

题目链接: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
",int(0.5+6.0*dist[1]) );
	return 0;
}