POJ2502 subway

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


题意:给出家和学校的坐标,然后给出地铁站点的坐标,每条地铁用-1,-1结束,人的速度是10km/h,地铁是40km/h,求从家到学校最少需要多少分钟。

(ctrl+z结束输入)

思路:把路程换成对应的时间,就变成了求单源最短路的问题,用dijkstra就好。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=250;
bool vis[maxn];
double cost[maxn][maxn];
double d[maxn];
const double v1=10000.0/60;  //先把单位换成每分钟走多少米
const double v2=40000.0/60;
struct node
{
    double x, y;
}a[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));
}
void dij(int n, int s)
{
    for(int i=1; i<=n; i++)
    {
        d[i]=INF;
        vis[i]=false;
    }
    d[s]=0;
    for(int i=1; i<=n; i++)
    {
        int k=-1;
        double m=INF;
        for(int j=1; j<=n; j++)
        {
            if(d[j]<m&&!vis[j])
                m=d[k=j];
        }
        if(k==-1)
            break;
        vis[k]=true;
        for(int j=1; j<=n; j++)
            if(!vis[j]&&d[j]>d[k]+cost[k][j])
                d[j]=d[k]+cost[k][j];
    }
}
int main()
{
    while(~scanf("%lf%lf%lf%lf", &a[1].x, &a[1].y, &a[2].x, &a[2].y))
    {
         int n=2;
         int cnt=3;
         int x,y;
         for(int i=1; i<maxn; i++)
             for(int j=1; j<maxn; j++)
             {
                 if(i==j)
                    cost[i][j]=0;
                 else
                    cost[i][j]=INF;
             }
        while(~scanf("%d%d", &x, &y))
        {
            if(x==-1&&y==-1)
            {
                cnt=n+1;
                continue;
            }
            n++;
            a[n].x=x;
            a[n].y=y;
            if(n!=cnt)
                cost[n-1][n]=cost[n][n-1]=min(cost[n-1][n], dis(a[n-1], a[n])/v2);   //只有两站之间才能坐地铁
        }
        for(int i=1; i<maxn; i++)
            for(int j=1; j<maxn; j++)
                cost[i][j]=min(cost[i][j], dis(a[i], a[j])/v1);
        dij(n, 1);
        printf("%.0f\n", d[2]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值