POJ 2502 Subway

Subway

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4928 Accepted: 1602

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         人走路的速度是10km/h,地铁的速度是40km/h 题目给出一个起点,一个终点, 以及几条地铁线路运行的站点。   题目给的点的做坐标单位是m 把速度统一为m/min   答案输出从起点到终点的时间,到最近的分钟数。   10km/h= 10000/60 m/min 40km/h= 40000/60 m/min   所有的点直接以步行的速度建边。 地铁线路两站相邻的以地铁速度建边

//============================================================================
// Name        : POJ.cpp
// Author      : 
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int MAXN=300;
struct Node
{
    double  x,y;
}node[MAXN];
double dis(Node a,Node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
const double INF=1e30;
bool vis[MAXN];
double dist[MAXN];
double cost[MAXN][MAXN];
void Dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        dist[i]=INF;
        vis[i]=false;
    }
    dist[start]=0;
    for(int j=0;j<n;j++)
    {
        int k=-1;
        double Min=INF;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&dist[i]<Min)
            {
                Min=dist[i];
                k=i;
            }
        if(k==-1)break;
        vis[k]=true;
        for(int i=1;i<=n;i++)
            if(!vis[i]&&dist[k]+cost[k][i]<dist[i])
                dist[i]=dist[k]+cost[k][i];
    }
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    double v1=10000.0/60;
    double v2=40000.0/60;
    while(scanf("%lf%lf%lf%lf",&node[1].x,&node[1].y,&node[2].x,&node[2].y)==4)
    {
        int n=2;
        int cnt1=3;
        int x,y;
        for(int i=1;i<300;i++)
            for(int j=1;j<300;j++)
            {
                if(i==j)cost[i][j]=0;
                else cost[i][j]=INF;
            }
        while(scanf("%d%d",&x,&y)==2)
        {
            if(x==-1&&y==-1)
            {
                cnt1=n+1;
                continue;
            }
            n++;
            node[n].x=x;
            node[n].y=y;
            if(n!=cnt1)cost[n][n-1]=cost[n-1][n]=min(cost[n][n-1],dis(node[n],node[n-1])/v2);
            //只有相邻的站点能到
        }
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                cost[i][j]=min(cost[i][j],dis(node[i],node[j])/v1);
        Dijkstra(n,1);
//        int ans=(int)round(dist[2]);
//        cout<<ans<<endl;
        printf("%.0lf\n",(dist[2]));
    }
    return 0;
}

 

           

    原文作者:kuangbin
    原文地址: https://www.cnblogs.com/kuangbin/p/3142560.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞