Dijkstra+计算几何 POJ 2502 Subway
题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间
分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态.
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std; const int N = 2e2 + 5;
const int E = N * N;
const double INF = 1e30;
struct Point {
double x, y;
}p[N];
bool vis[N];
double d[N];
double w[N][N];
double sx, sy, ex, ey;
int tot; void Dijkstra(int s) {
memset (vis, false, sizeof (vis));
for (int i=1; i<=tot; ++i) {
d[i] = INF;
}
d[s] = 0;
for (int i=1; i<=tot; ++i) {
double mn = INF; int x = -1;
for (int j=1; j<=tot; ++j) {
if (!vis[j] && mn > d[j]) mn = d[x=j];
}
if (x == -1) break;
vis[x] = true;
for (int j=1; j<=tot; ++j) {
if (!vis[j] && d[j] > d[x] + w[x][j]) {
d[j] = d[x] + w[x][j];
}
}
}
} double squ(double x) {
return x * x;
} double get_cost(Point p1, Point p2, int v) {
double dis = sqrt (squ (p1.x - p2.x) + squ (p1.y - p2.y)) / 1000;
return dis / v;
} int main(void) {
while (scanf ("%lf%lf%lf%lf", &sx, &sy, &ex, &ey) == 4) {
for (int i=1; i<N; ++i) {
for (int j=1; j<N; ++j) {
w[i][j] = INF;
}
}
tot = 1;
double x, y, l = tot + 1;
p[tot].x = sx; p[tot].y = sy;
while (scanf ("%lf%lf", &x, &y) == 2) {
if (x == -1 && y == -1) {
for (int i=l; i<tot; ++i) {
double tim = get_cost (p[i], p[i+1], 40);
if (w[i][i+1] > tim) {
w[i][i+1] = w[i+1][i] = tim;
}
}
l = tot + 1;
continue;
}
p[++tot].x = x; p[tot].y = y;
}
p[++tot].x = ex; p[tot].y = ey;
for (int i=1; i<tot; ++i) {
for (int j=i+1; j<=tot; ++j) {
double tim = get_cost (p[i], p[j], 10);
if (w[i][j] > tim) {
w[i][j] = w[j][i] = tim;
}
}
}
Dijkstra (1);
printf ("%.0f\n", d[tot] * 60);
} return 0;
}
Dijkstra+计算几何 POJ 2502 Subway的更多相关文章
- 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 ...
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- POJ 2502 Subway (Dijkstra 最短+建设规划)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6689 Accepted: 2176 Descriptio ...
- (简单) POJ 2502 Subway,Dijkstra。
Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...
- poj 2502 Subway【Dijkstra】
<题目链接> 题目大意: 某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度 ...
- POJ 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- POJ 2502 Subway
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 1602 Descriptio ...
- POJ 2502 Subway (最短路)
Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...
- POJ 2502 Subway ( 最短路 && 最短路建图 )
题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...
随机推荐
- asp.net mvc在Model中控制日期格式
这是默认的日期格式如下图:
- 在CMD窗口中使用javac和java命令进行编译和执行带有包名的具有继承关系的类
一.背景 最近在使用记事本编写带有包名并且有继承关系的java代码并运行时发现出现了很多错误,经过努力一一被解决,今天我们来看一下会遇见哪些问题,并给出解决办法. 二.测试过程 1.父类代码 pack ...
- 利用CocoaPods,在项目中导入AFNetworking类库
场景1:利用CocoaPods,在项目中导入AFNetworking类库 AFNetworking类库在GitHub地址是:https://github.com/AFNetworking/AFNetw ...
- Java实现带括号优先级的计算器
这个计算器不仅能够进行四则运算,还支持添加括号进行优先级计算,例如下面算式: 10+(2*16-20/5)+7*2=52 Java源代码: import java.awt.BorderLayout; ...
- AIX性能监控
http://www.ibm.com/developerworks/cn/aix/library/au-aix7memoryoptimize2/ http://www.aixchina.net/Art ...
- 解决mysql无法插入中文数据及插入后显示乱码的问题
(1)废话不多说就是使用mysql数据库的时候无法输入中文,可以输入中文后显示的又是乱码!! (2开始解决问题: 第一步:找到安装mysql的目录找到 my.ini 文件: 第二步:使用记事本打开my ...
- Oracle buffer cache
Buffer Cache buffer cache 结构图 HASH链 ORACLE使用HASH算法,把buffer cache中每个buffer的buffer header串联起来,组成多条hash ...
- .NET yield
.Net Yield 其实比较简单,手动yield,一学就会. public static class GalaxyClass { public static void ShowGalaxies() ...
- Getting Started with Blocks
本文来源为:developer.apple.com,仅仅是博主练习排版所用. Getting Started with Blocks The following sections help you t ...
- [Java] 使用Java Visual VM寻找PermGen Space的解决办法
在Eclipse使用tomcat运行3个项目时,老是报这个错误,以下为错误详情: 2014-5-28 13:47:41 org.apache.catalina.core.StandardWrapper ...