POJ2502:Subway(最短路)

本文介绍了一个基于二维平面上的起点和终点坐标,结合多条地铁线路站点信息的路径规划问题。通过模拟建边的方法,计算从起点到终点的最短时间,考虑了地铁速度和步行速度的不同,并详细展示了算法实现过程。

Subway

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14634 Accepted: 4718

题目链接:http://poj.org/problem?id=2502

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

题意:

在二维平面上,给出起点和终点的坐标。另外还有多条地铁线路,给出每条线路有哪些站。

如果坐地铁的速度是40km/h,而走路的速度为10km/h,现在问从起点到终点最少花费多少时间。

 

题解:

直接模拟建边就行了,注意要换算一下单位,因为最后问的是分钟。

有个需要注意的地方就是地铁站之间也可以走路。另外结果四舍五入!!

 

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 805;
int x,y,X,Y,cnt,num,s,t;
map <ll,map<ll,int> > p;
double d[N];
int vis[N],head[N];
struct node{
    int x,y;
};
vector <node> vec[N];
double dis(int X1,int X2,int Y1,int Y2){
    return sqrt((double)(X1-X2)*(X1-X2)+(double)(Y1-Y2)*(Y1-Y2));
}
double T(double d,int op){
    if(op==1) return 6*d/1000.0;
    else return 6*d/4000.0;
}
struct Edge{
    int u,v,next ;
    double w;
}e[N*N<<2];
int tot;
struct Node{
    double d;
    int u;
    bool operator < (const Node &A)const{
        return d>A.d;
    }
};
void adde(int u,int v,double w){
    e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
void Dijkstra(int s){
    priority_queue <Node> q;
    for(int i=0;i<=t;i++) d[i]=INF;
    memset(vis,0,sizeof(vis));d[s]=0;
    Node now;
    now.d=0;now.u=s;
    q.push(now);
    while(!q.empty()){
        Node cur = q.top();q.pop();
        int u=cur.u;
        if(vis[u]) continue ;
        vis[u]=1;
        for(int i=head[u];i!=-1;i=e[i].next){
            int v=e[i].v;
            if(d[v]>d[u]+e[i].w){
                d[v]=d[u]+e[i].w;
                now.d=d[v];now.u=v;
                q.push(now);
            }
        }
    }
}
int main(){
    cin>>x>>y>>X>>Y;
    int xx,yy;
    memset(head,-1,sizeof(head));
    while(scanf("%d",&xx)!=EOF){
        scanf("%d",&yy);
        cnt++;
        node cur;
        cur.x=xx;cur.y=yy;
        vec[cnt].push_back(cur);
        while(1){
            scanf("%d%d",&xx,&yy);
            if(xx==-1 && yy==-1) break ;
            cur.x=xx;cur.y=yy;
            vec[cnt].push_back(cur);
        }
    }
    s=0;t=300;
    for(int i=1;i<=cnt;i++){
        for(int j=0;j<vec[i].size();j++){
            node cur = vec[i][j];
            if(p[cur.x][cur.y]==0) p[cur.x][cur.y]=++num;
            adde(s,p[cur.x][cur.y],T(dis(0,cur.x,0,cur.y),1));
            adde(p[cur.x][cur.y],t,T(dis(cur.x,X,cur.y,Y),1));
        }
    }
    adde(s,t,T(dis(x,X,y,Y),1));
    for(int i1=1;i1<=cnt;i1++){
        for(int j1=0;j1<vec[i1].size();j1++){
            int len1 = vec[i1].size();
            node cur1 = vec[i1][j1];
            if(j1>0){
                node cur2 = vec[i1][j1-1];
                adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),2));
            }
            if(j1<len1-1){
                node cur2 = vec[i1][j1+1];
                adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),2));
            }
            for(int i2=1;i2<=cnt;i2++){
                for(int j2=0;j2<vec[i2].size();j2++){
                    node cur2 = vec[i2][j2];
                    adde(p[cur1.x][cur1.y],p[cur2.x][cur2.y],T(dis(cur1.x,cur2.x,cur1.y,cur2.y),1));
                }
            }
        }
    }
    Dijkstra(s);
    cout<<(int)(d[t]+0.5);
    return 0;
}

 

转载于:https://www.cnblogs.com/heyuhhh/p/10351974.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值