题目传送门

题意:列车上行驶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的更多相关文章

  1. 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 ...

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

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

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

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

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

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

  5. poj 2502 Subway【Dijkstra】

    <题目链接> 题目大意: 某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度 ...

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

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

  7. POJ 2502 Subway

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

  8. POJ 2502 Subway (最短路)

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

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

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

随机推荐

  1. [Android Pro] adb 进入 recovery, adb 进入 bootloader

    reference to : http://blog.csdn.net/mldxs/article/details/18699965 重启到Recovery界面 adb reboot recovery ...

  2. MyISAM表的维护和恢复

    本节将讨论如何使用myisamchk检查和修复MyISAM表. 同时,你可以用myisamchk来检查,修复和优化数据库表.下面将讲述如何执行这些操作并建立维护计划. 虽然使用myisamchk很安全 ...

  3. c语言中的浮点数

    一.浮点数常量(小数) 0.11L, 0.0f ,0.0,1.88,2.5f ,0.188E1 E3表示103        比如 1.88E 3=1.88*1000=1880.0f E-3表示10- ...

  4. "稀奇古怪的"delete this

    myClass::foo(){     delete this; } .. void func(){     myClass *a = new myClass();     a->foo(); ...

  5. LeetCode : 223. Rectangle Area

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABRQAAAQ0CAYAAAAPPZBqAAAMFGlDQ1BJQ0MgUHJvZmlsZQAASImVlw

  6. 谈谈我的编程之路---WAMP(四)

    WAMP的一些配置与使用心得(windows)W-windows,说到windows,其实大家都最熟悉不过了,GUI(graphic user interface)图形界面就没有什么可说的了,基本会用 ...

  7. js DOM对象

    查找元素 根据id找 document.getElementById("b"): 根据class找 document.getElementsByClassName("aa ...

  8. 微信token验证失败的解决方法

    一.问题由来 在使用URL和Token启用微信公众平台开发模式消息接口的时候,我们会碰到下面三种情况 1. token校验失败 这样回头检查一下各项配置是否正确.如果确定配置没有问题,请按下面的方法检 ...

  9. 【JAVA集合框架之Map】

    一.概述.1.Map是一种接口,在JAVA集合框架中是以一种非常重要的集合.2.Map一次添加一对元素,所以又称为“双列集合”(Collection一次添加一个元素,所以又称为“单列集合”)3.Map ...

  10. <转>Oracle SQL性能优化

    原文链接:http://www.cnblogs.com/rootq/archive/2008/11/17/1334727.html (1)      选择最有效率的表名顺序(只在基于规则的优化器中有效 ...