题目链接: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

已知家和学校的坐标,并给出若干条地铁线,以及步行速度和地铁速度,求从家到学校最少需要多少时间
题目的难点在于输入和建图,由于输入的点最多200个,我们可以直接读入点的坐标,生成一个邻接矩阵,而同一地铁线上的各个站点,就可以在输入是直接存入邻接矩阵了,为了便于松弛,邻接矩阵中我们存入路径的用时,注意速度的单位是km/h,而路程的单位是m
建好图之后,就是一个简单的最短路问题了
 #include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<cstdio>
#include<queue>
#include<stack> using namespace std; const int INF = 0x3f3f3f3f;
const int MAXN = ;
const double wsp = * / ;
const double ssp = * / ; struct Node{
double x, y;
}node[MAXN]; struct ff{
int x, d;
ff(){}
ff( int a, double b ){ x = a; d = b; }
bool operator <( const ff & a )const{
return d > a.d;
}
}; int cnt;
double cost[MAXN][MAXN];
double dis[MAXN]; double gdis( int pre, int pos ){
double dx = node[pre].x - node[pos].x;
double dy = node[pre].y - node[pos].y;
return sqrt( dx * dx + dy * dy );
} void dij(){
for( int i = ; i < MAXN; i++ )
dis[i] = INF;
dis[] = ; priority_queue<ff> Q;
Q.push( ff( , dis[]) ); while( !Q.empty() ){
ff temp = Q.top(); Q.pop();
int x = temp.x;
if( temp.d > dis[x] ) continue;
for( int i = ; i < cnt; i++ ){
if( dis[i] > dis[x] + cost[x][i] ){
dis[i] = dis[x] + cost[x][i];
Q.push( ff( i, dis[i] ) );
}
}
}
} int main(){
ios::sync_with_stdio( false ); for( int i = ; i < MAXN; i++ )
for( int j = ; j < MAXN; j++ )
cost[i][j] = INF; cin >> node[].x >> node[].y >> node[].x >> node[].y;
cnt = ; while( cin >> node[cnt].x >> node[cnt].y ){
cnt++;
while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == - && node[cnt].y == - ) ){
cost[cnt][cnt - ] = cost[cnt - ][cnt] = gdis( cnt - , cnt ) / ssp;
cnt++;
}
} for( int i = ; i < cnt - ; i++ ){
cost[i][i] = ;
for( int j = i + ; j < cnt; j++ ){
cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp );
}
} dij(); cout << int( dis[] + 0.5 );
}

POJ-2502 Subway( 最短路 )的更多相关文章

  1. POJ 2502 Subway ( 最短路 && 最短路建图 )

    题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...

  2. POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)

    POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离) Description You have just moved from a ...

  3. POJ 2502 - Subway Dijkstra堆优化试水

    做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...

  4. POJ 2502 Subway (最短路)

    Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...

  5. POJ 2502 Subway(迪杰斯特拉)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 2177 Descriptio ...

  6. POJ 2502 Subway (Dijkstra 最短+建设规划)

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 Descriptio ...

  7. POJ 2502 Subway

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4928   Accepted: 1602 Descriptio ...

  8. (简单) POJ 2502 Subway,Dijkstra。

    Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...

  9. Dijkstra+计算几何 POJ 2502 Subway

    题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...

  10. POJ 2502 Subway dij

    这个题的输入输出注意一下就好 #include<cstdio> #include<cstring> #include<queue> #include<cstd ...

随机推荐

  1. LFS8.3BOOT引导疑点解决

    LFS系统 的BOOT引导 在LFS书中写到的BOOT引导,时直接将宿主机的BOOT分区挂载当LFS的BOOT分区中,虽然这样也是可以实现BOOT引导的,但是我并不想这样做,所以BOOT引导就变得有些 ...

  2. wpf界面按钮自动点击

    Button Button = new Button();Button.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));//在按钮生成时便会自动触 ...

  3. Hyper-V虚拟机上安装Ubuntu16.04/Ubuntu18.04.2LTS,搭建GitLab

    我的电脑系统是win10,内存8g如下 一开始是装的Ubuntu18.04.2LTS, gitlab-ce_12.1.3-ce.0_amd64.deb,每次能够安装成功,但是修改完ip后,运行gitl ...

  4. jdk安装及环境配置

    1.下载对应的安装包(我们公司用的是jdk 1.8) 2.选择对应版本,点击安装,在选择安装位置的时候,选择自己对应存放的位置,其他都点击下一步就行了,先安装jdk,后安装jre 3.环境变量,选择 ...

  5. Oracle创建设置查询权限用户

    用户创建的可以参考博客: https://blog.csdn.net/u014427391/article/details/84889023 Oracle授权表权限给用户: 语法:grant [权限名 ...

  6. UnityShader之积雪效果

    积雪效果是比较简单的,只需要计算顶点法线方向和世界向上方向之间的点乘,将得到的值与预设的阀值比较,小于阀值为0,用这个值进行插值就OK了 代码: Shader "MyShader/SnowS ...

  7. Java实现ZooKeeper的zNode监控

    上一篇文章已经完成了ZooKeeper的基本搭建和使用的介绍,现在开始用代码说话.参考 https://zookeeper.apache.org/doc/current/javaExample.htm ...

  8. Visual Studio 中两个窗体(WinForm)之间相互传值的方法

    编写WinowsForm应用程序时,实现两个窗体之间相互传递值的方法其实很简单.以下用一个例子说明:在名为FormMain主窗体运行过程中利用名为FormInfo窗体,获取用户输入信息,并将这些信息返 ...

  9. keras 学习-线性回归

    园子里头看到了一些最基础的 keras 入门指导, 用一层网络,可以训练一个简单的线性回归模型. 自己学习了一下,按照教程走下来,结果不尽如人意,下面是具体的过程. 第一步: 生成随机数据,绘出散点图 ...

  10. 小白学Python(1)——安装与调试,“你好,世界”,“hello,world”

    之前从没接触过编程之类的东西,在网上下载个自己需要的软件真实比较麻烦,找了半天总是没有合适的,好不容易找到了,不过那家公司已经倒闭了,软件不更新也运行不了了,于是乎,求人不如求己,自己没事编程吧. 在 ...