D - Subway

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

题意:给出起点和终点,给出每一条地铁线上的地铁站,一条线上的相邻地铁站能坐地铁,其余的只能走路,问从起点到终点的最快时间。

相邻地铁站用地铁站建边,然后再整幅图建走路的边。跑一遍最短路(spfa)。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <map>
#include <iostream>
#include <vector>
#include <cmath>
#include <cstdlib>
const int MAXM=40005;
const int MAXN=205;
using namespace std;
int tol;
bool vis[MAXN];
double dir[MAXM];
struct q
{
    int u,v,w;
}road[MAXM];
struct Edge
{
    int v;
    double w;
};
vector<Edge> edge[MAXM];
double walktime(int x1,int y1,int x2,int y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))/1000.0*6.0;
}
double subtime(int x1,int y1,int x2,int y2)
{
    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))/4000.0*6.0;
}
void add(int u,int v,double w)
{
    /*edge[tol].u=u;
    edge[tol].v=v;
    edge[tol++].w=w;*/
    Edge tmp;
    tmp.v=v;
    tmp.w=w;
    edge[u].push_back(tmp);
}
void spfa(int from)
{
    memset(vis,false,sizeof(vis));
    //memset(dir,0x3f,sizeof(dir));
    for(int i=0;i<MAXM;i++)
    {
        dir[i]=10000000001;
    }
    dir[from]=0;
    vis[from]=true;
    queue<int>que;
    while(!que.empty())
    {
        que.pop();
    }
    que.push(from);
    while(!que.empty())
    {
        int now=que.front();
        que.pop();
        vis[now]=false;
        for(int i=0;i<edge[now].size();i++)
        {
            if(dir[edge[now][i].v]>edge[now][i].w+dir[now])
                {
                    dir[edge[now][i].v]=edge[now][i].w+dir[now];
                    if(!vis[edge[now][i].v])
                    {
                        que.push(edge[now][i].v);
                        vis[edge[now][i].v]=true;
                    }
                }
        }
    }
}
int main (void)
{
    int x1,y1,x2,y2;
    scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
    double le=walktime(x1,y1,x2,y2);
    road[1].u=x1,road[1].v=y1;
    road[2].u=x2,road[2].v=y2;
    add(1,2,le);
    int cnt=3;
    int i=3;
    //int flag=1;
    int x,y;
    while(~scanf("%d %d",&x,&y))
        {
            if(x==-1&&y==-1)
                {
                    for(;i<cnt-1;i++)
                    {
                        le=subtime(road[i].u,road[i].v,
                            road[i+1].u,road[i+1].v);
                        add(i,i+1,le);
                        add(i+1,i,le);
                    }
                    i++;//注意
                    continue;
                }
            else
            {   
            road[cnt].u=x;
            road[cnt].v=y;
            cnt++;
            }
        }
        for(int i=1;i<cnt;i++)
        {
            for(int j=i+1;j<cnt;j++)
            {
                le=walktime(road[i].u,road[i].v,road[j].u,road[j].v);
                add(i,j,le);
                add(j,i,le);
            }
        }
        spfa(1);
        printf("%.0f\n",dir[2]);
        return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值