POJ2502 Subway 最短路

来源:互联网 发布:通过手机网络定位 编辑:程序博客网 时间:2024/05/16 07:39

Subway
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 7482 Accepted: 2433

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 10000 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1

Sample Output

21

告诉一些地铁线路,从起点到终点,中途可以步行,可以坐地铁,找一条最短的路

主要是把图建立好,然后直接dijkstra或者floyd,因为速度不同,所以转化成求最短的时间


#include <iostream>#include <stdio.h>#include <string>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define INF 1<<30double mp[500][500];int subway[500];int n;struct node{    double x,y;}f[500];double dis(node a,node b){    return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );}int check(node a){    int i;    for(i=1;i<n;i++)    {        if(a.x==f[i].x && a.y==f[i].y)        break;    }    if(i==n) f[n++]=a;        return i;}void floyd(){    for(int k=1;k<n;k++)    {        for(int i=1;i<n;i++)        {            for(int j=1;j<n;j++)            {                mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);            }        }    }}int main(){    node a;    n=1;    for(int i=0;i<=333;i++)    {        for(int j=0;j<=333;j++)        {            if(i==j) mp[i][j]=0;            else mp[i][j]=INF;        }    }    scanf("%lf%lf%lf%lf",&f[n].x,&f[n].y,&f[n+1].x,&f[n+1].y);    int cnt;    n+=2;    while(~scanf("%lf%lf",&a.x,&a.y))    {        cnt=0;        subway[cnt++]=check(a);        while(scanf("%lf%lf",&a.x,&a.y) && (a.x!=-1 && a.y!=-1) )        {            subway[cnt++]=check(a);        }        for(int i=1;i<cnt;i++)        {            mp[subway[i]][subway[i-1]]=mp[subway[i-1]][subway[i]]=dis( f[subway[i]],f[subway[i-1]] )*3.0/2000.0;        }    }    for(int i=1;i<n;i++)    {        for(int j=1;j<i;j++)        {            mp[i][j]=mp[j][i]=min( mp[i][j], dis(f[i],f[j])*3.0/500.0 );        }    }    floyd();    printf("%d\n",(int)(mp[1][2]+0.5));    return 0;}








0 0