[kuangbin]专题四 最短路练习 Subway POJ - 2502【dijkstra】

【题目描述】
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.
你刚刚从一个安静的滑铁卢社区搬到了一个喧闹的大城市。 你现在可以步行乘坐地铁,而不是每天骑自行车上学。 因为你不想上课迟到,你想知道你上学需要多长时间。
你以10公里/小时的速度行走。 地铁以40公里/小时的速度行驶。 假设你很幸运,每当你到达地铁站时,都可以立刻上车。 您可以多次上下地铁,如果您愿意,可以在不同的地铁线路之间切换。 所有地铁线都朝两个方向行驶。

【输入】
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.
输入包括您家和学校的x,y坐标,其次是几条地铁线的规格。每条地铁线路按顺序由线路上每个站点的非负整数x,y坐标组成。您可以假设地铁在相邻站点之间以直线运行,坐标表示整数米。每条线至少有两个停靠点。每条地铁线的末端后跟虚拟坐标对-1,-1。该市最多有200个地铁站。

【输出】
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
输出是您上学所需的分钟数,四舍五入到最近的分钟,采用最快的路线。

【样例输入】
0 0 10000 1000
0 200 5000 200 7000 200 -1 -1
2000 600 5000 600 10000 600 -1 -1

【样例输出】
21

题目链接:https://cn.vjudge.net/problem/POJ-2502

建图有一点小小的恶心,剩下的只有模板了

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
#define INF 1e9
static const int MAXN=410;
struct Node{
    int v;
    double c;
    Node(int _v=0,double _c=0):v(_v),c(_c){};
    bool operator < (const Node &r) const {
        return c>r.c;
    }
};
struct Edge{
    int v;
    double cost;
    Edge(int _v=0,double _cost=0):v(_v),cost(_cost){}
};
vector<Edge> E[MAXN];
bool vis[MAXN];
double dist[MAXN];
void dijkstra(int n,int start)
{
    for(int i=1;i<=n;i++)
    {
        vis[i]=false;
        dist[i]=INF;
    }
    priority_queue<Node> Q;
    dist[start]=0;
    Q.push(Node(start,0));
    while(!Q.empty())
    {
        Node tmp=Q.top();
        Q.pop();
        int u=tmp.v;
        if(vis[u])
            continue;
        vis[u]=true;
        for(int i=0;i<E[u].size();i++)
        {
            int v=E[u][i].v;
            double cost=E[u][i].cost;
            if(!vis[v] && dist[v]>dist[u]+cost)
            {
                dist[v]=dist[u]+cost;
                Q.push(Node(v,dist[v]));
            }
        }
    }
}
void addedge(int u,int v,double w)
{
    E[u].push_back(Edge(v,w));
}
int x[MAXN],y[MAXN];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0),cout.tie(0);
    cin>>x[1]>>y[1]>>x[2]>>y[2];
    int cnt=3,start=3;
    while(cin>>x[cnt]>>y[cnt])
    {
        if(x[cnt]==-1 && y[cnt]==-1)
        {
            for(int i=start;i<cnt-1;i++)
            {
                double dis=sqrt((x[i]-x[i+1])*(x[i]-x[i+1])+(y[i]-y[i+1])*(y[i]-y[i+1]));
                addedge(i,i+1,dis/40000.0);
                addedge(i+1,i,dis/40000.0);
            }
            start=cnt;
        }
        else
            cnt++;
    }
    for(int i=1;i<cnt;i++)
        for(int j=1;j<cnt;j++)
        {
            double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
            addedge(i,j,dis/10000.0);
            addedge(j,i,dis/10000.0);
        }
    dijkstra(cnt-1,1);
    cout<<(int)(dist[2]*60+0.5)<<endl;
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值