POJ2502 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
1
二、思路
点编号为0,终点编号为201(因为最多200个站台)。从起点到所有站台,所有站台到终点都建立一条步行的边。两两站台之间也要建立一条步行的边。
然后就是一条航线中的相邻站台建立一条40km/h的花费的边。
题目给出的坐标算出来的距离单位是m, 给出的速度是km/h。
答案输出应该四舍五入。 有点坑。
int(d + 0.5)
三、代码
#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 205, M = 5e4 + 5;
struct E {
int v, next;
double w;
} e[M];
struct Stop {
int x, y;
} stop[N];
int id = 1, x, y, lx, ly, sx, sy, ex, ey, len = 1, h[N];//id代表点的标号
bool vis[N]; //起点是0 终点的编号是201 因为最多有200个站点
double d[N];
double getD(int x1, int y1, int x2, int y2, int t) {
double x = x1 - x2;
double y = y1 - y2;
return sqrt(x*x + y*y) / t / 1000 * 60;
}
void add(int u, int v, double w) {
e[len].v = v;
e[len].w = w;
e[len].next = h[u];
h[u] = len++;
}
//若a > b 返回true a <= b 返回false
bool cmp(double a, double b) {
if (a - b > 1e-4) return true;
return false;
}
void spfa() {
for (int i = 1; i < id; i++) d[i] = 1e18;
d[0] = 0; d[201] = 1e18;
queue<int> q;
q.push(0);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = false;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
double w = d[u] + e[j].w;
if (cmp(d[v], w)) {
d[v] = w;
if (!vis[v]) q.push(v), vis[v] = true;
}
}
}
}
int main() {
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
while (~scanf("%d%d", &stop[id].x, &stop[id].y)) {
x = stop[id].x, y = stop[id].y;
add(0, id, getD(x, y, sx, sy, 10));
add(id, 201, getD(x, y, ex, ey, 10));
++id;
while (scanf("%d%d", &stop[id].x, &stop[id].y), stop[id].x != -1) {
x = stop[id].x, y = stop[id].y;
lx = stop[id - 1].x, ly = stop[id - 1].y;
//从站点步行到终点 和起点
add(0, id, getD(x, y, sx, sy, 10));
add(id, 201, getD(x, y, ex, ey, 10));
//建立与前一个站点的边
add(id, id - 1, getD(x, y, lx, ly, 40));
add(id - 1, id, getD(x, y, lx, ly, 40));
++id; //-1的时候id不变
}
}
//每个站台之间可以步行 总共的标号是【1, id-1】
for (int i = 1; i < id; i++) {
for (int j = i + 1; j < id; j++) {
x = stop[i].x, y = stop[i].y;
lx = stop[j].x, ly = stop[j].y;
add(i, j, getD(x, y, lx, ly, 10));
add(j, i, getD(x, y, lx, ly, 10));
}
}
spfa();
printf("%d", int(d[201] + 0.5));//四舍五入
return 0;
}
POJ2502 Subway 最短路的更多相关文章
- POJ-2502 Subway( 最短路 )
题目链接:http://poj.org/problem?id=2502 Description You have just moved from a quiet Waterloo neighbourh ...
- L - Subway(最短路spfa)
L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...
- POJ2502:Subway(最短路)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14634 Accepted: 4718 题目链接:http ...
- (poj 2502) Subway 最短路
题目链接: 题意:在一个城市里有许多地铁,现在你知道每条地铁的起点 终点与停站点的坐标,知道我们的起始坐标与终点坐标,问加上走路最快到达终点的时间是多少? 方法:求出任意两点的车速时间与步行时间,再 ...
- POJ 2502 Subway ( 最短路 && 最短路建图 )
题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...
- poj2502 Subway
思路: 需要注意的地方:一条地铁线路并不一定和样例描述的那样是直的:同一条线路上的两个站点步行可能更快. 实现: #include <iostream> #include <cstd ...
- poj图论解题报告索引
最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman- ...
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- POJ-2502(Dijikstra应用+最短路)
Subway POJ-2502 这里除了直接相连的地铁站,其他图上所有的点都要连线,这里是走路的速度. 记住最后的结果需要四舍五入,否则出错. #include<iostream> #in ...
- poj2502 最短路
//Accepted 504 KB 16 ms //spfa最短路 //把n个地铁站作为n个顶点,边权为从一个站到另一个站的时间 //注意:地铁在相邻的两站之间是直线行驶,但其他的就不是了 #incl ...
随机推荐
- 数据分析师如何做好PPT
作为一名数据分析师,制作出色的PPT是非常重要的技能之一.一个好的PPT可以让你的数据洞察更加生动有趣,并有效传达给观众.以下是一些详细的步骤和技巧,帮助你成为一名擅长制作PPT的数据分析师: 确定目 ...
- Postgres 和 MySQL 应该怎么选?
PostgreSQL和MySQL是两个流行的关系型数据库管理系统(DBMS).它们都具有一些相似的功能,但也有一些区别. 在选择使用哪个DBMS时,需要考虑多个因素,包括性能.可扩展性.安全性.功能丰 ...
- Excel 中使用数据透视图进行数据可视化
使用数据透视表(PivotTable)是在Excel中进行数据可视化的强大工具.下面将提供详细的步骤来使用数据透视表进行数据可视化. **步骤一:准备数据** 首先,确保你有一个包含所需数据的Exce ...
- 数据结构(C语言版 第2版)课后习题答案全集 严蔚敏
有的小伙伴在网上奋力的找 严蔚敏版 第2版 数据结构 的始终无果,那么我这里就整理好,放在同名公众号中了,也可扫文章末尾的二维码直达公众号,回复数据结构的关键词即可拿到.
- 自研、好用的ORM 读写分离功能使用
Fast Framework 作者 Mr-zhong 代码改变世界.... 一.前言 Fast Framework 基于NET6.0 封装的轻量级 ORM 框架 支持多种数据库 SqlServer O ...
- [AGC038E] Gachapon
Problem Statement Snuke found a random number generator. It generates an integer between $0$ and $N- ...
- PyTorch项目源码学习(1)
PyTorch PyTorch是一个开源的深度学习框架,项目地址 https://github.com/pytorch/pytorch 在学术工作中的使用频率较高.其主体由C++开发,PyTorch是 ...
- Unity3D学习笔记5——创建子Mesh
目录 1. 概述 2. 详论 2.1. 实现 2.2. 解析 3. 参考 1. 概述 在文章Unity3D学习笔记4--创建Mesh高级接口通过高级API的方式创建了一个Mesh,里面还提到了一个Su ...
- 大数据实践解析(下):Spark的读写流程分析
导读: 众所周知,在大数据/数据库领域,数据的存储格式直接影响着系统的读写性能.spark是一种基于内存的快速.通用.可扩展的大数据计算引擎,适用于新时代的数据处理场景.在"大数据实践解析( ...
- 理论+示例,详解GaussDB(DWS)资源管理
摘要:合理地管理和分配系统资源,是保证数据库系统稳定高效运行的关键. 本文分享自华为云社区<GaussDB(DWS)资源管理能力介绍与应用示例>,作者: 门前一棵葡萄树 . 一.资源管理能 ...