L - 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

题解:

注意先把路径转化为metres/minutes.
然后套用Dijkstra模板。
路径距离除以速度就是时间。
参考kuangbin大神

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 1010;

struct Node
{
    double x,y;
}node[MAXN];

double dis(Node a,Node b)
{
    return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
}

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()
{
    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]=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);

         printf("%.0lf\n",(dist[2]));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值