poj 2502 Subway (最短路 Dijksta)

Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8323   Accepted: 2690

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

Waterloo local 2001.09.22

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

已知人走路的速度是10km/h,地铁的速度是40km/h, 题目首先给出一个起点,一个终点,
以及几条地铁线路运行的站点,以-1,-1结束;注意 题目给的点的做坐标单位是m,我们要 把速度统一为m/min
 
答案输出从起点到终点的时间,到最近的分钟数。
 
10km/h= 10000/60 m/min
40km/h= 40000/60 m/min
 
所有的点直接以步行的速度建边。 地铁线路两站相邻的以地铁速度建边

一道比较 变态的 最短路吧~  输入时需要注意;


#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N=330;
const double INF=1e30;

struct node
{
    double x,y;
}p[N];

int vis[N];
double dist[N],map[N][N]; ///全部设成double以免出错

double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));   ///求两站点之间的距离;
}

void Dijkstra(int n)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        dist[i]=INF;
        vis[i]=0;
    }
    dist[1]=0;
    for(i=1;i<=n;i++)
    {
        int index=-1;
        double Min=INF;
        for(j=1;j<=n;j++)
        {
           if(!vis[j] && dist[j]<Min)
           {
               Min=dist[j];
               index=j;
           }
        }
        if(index==-1) break;
        vis[index]=1;
        for(j=1;j<=n;j++)
        {
            if(!vis[j] && dist[j]>dist[index]+map[index][j])
                dist[j]=dist[index]+map[index][j];
        }
    }
    printf("%.0f\n",dist[2]);   ///起点到终点就是 dist[2],1到2的最短路;
}

int main()
{
    double v1=10000.0/60;  先把人和地铁的速度转化一下
    double v2=40000.0/60;
    while(scanf("%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y)==4)
    {
        int n=2;
        int cot=3;  ///初始化cot=3..... 
        int x,y,i,j;
        for(i=1;i<N;i++)
            for(j=1;j<N;j++)
            {
               if(i==j) map[i][j]=0;
               else map[i][j]=INF;  ///初始化~
            }
        while(scanf("%d%d",&x,&y)==2)  ///结束时记得 按ctrl+Z;
        {
            if(x==-1 && y==-1)
            {
                cot=n+1;  ///控制 相邻的两个站台才可以 坐车
                continue;
            }
            n++;
            p[n].x=x;
            p[n].y=y;
            if(n!=cot)
                map[n][n-1]=map[n-1][n]=min(map[n][n-1],dis(p[n],p[n-1])/v2);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
                map[i][j]=min(map[i][j],dis(p[i],p[j])/v1);
        }
        Dijkstra(n);
    }
    return 0;
}

你可能感兴趣的:(#,最短路,ACM+算法)