dij部分还是跟模板差不多的 但是这题的难点是处理输入 或者说理解题意

事实上每个点之间都是可以走的......WA了好几发就因为没意识到同一条路线上的各个站点之间居然也可以走得比车子快....

PS: POJ的C++编译器居然没法自动识别sqrt函数用哪个=.=

#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define INF 1e30
using namespace std; typedef pair<double, int> pdi;
struct cmp{
bool operator () (const pdi a, const pdi b){
return a.first > b.first;
}
}; struct Node{
int x, y;
}node[];
//int pre[210];
double val[][], dis[]; double DISTANCE(int a, int b){
return sqrt((double)((node[a].x - node[b].x) * (node[a].x - node[b].x) +
(node[a].y - node[b].y) * (node[a].y - node[b].y)));
} void dij(int s, int n)
{
//memset(pre, -1 ,sizeof pre);
for(int i = ; i <= n; i++){
dis[i] = INF;
}
priority_queue<pdi, vector<pdi>, cmp> q;
q.push(make_pair(, ));
dis[] = ;
while(!q.empty()){
pdi u = q.top();
q.pop();
if(u.first > dis[u.second]) continue;
for(int i = ; i <= n; i++){
if(i != u.second && dis[i] > dis[u.second] + val[u.second][i]){
dis[i] = dis[u.second] + val[u.second][i];
//pre[i] = u.second;
q.push(make_pair(dis[i], i));
}
}
}
}
int main()
{
int size = ;
for(int i = ; i < ; i++){
for(int j = ; j < ; j++){
val[i][j] = INF;
}
}
scanf("%d%d%d%d", &node[].x, &node[].y, &node[].x, &node[].y);
val[][] = val[][] = DISTANCE(, ) / * ;
while(~scanf("%d%d", &node[size].x, &node[size].y)){
int a, b;
size++;
while(scanf("%d%d", &a, &b), ~a || ~b){
node[size].x = a;
node[size].y = b;
val[size-][size] = val[size][size-]
= DISTANCE(size, size-) / * ;
size++;
}
}
for(int i = ; i < size; i++){
for(int j = ; j < size; j++){
val[i][j] = min(val[i][j], DISTANCE(i, j) / * );
}
}
dij(, size - );
printf("%.0lf\n", dis[]);
/*for(int i = 1; i < size; i++){
printf("x%d = %-8d y%d = %-8d\n", i, node[i].x, i, node[i].y);
}
for(int i = 1; i < size; i++){
for(int j = 1; j < size; j++){
printf("[%d][%d] = %-3.0lf", i, j, val[i][j]);
}
putchar('\n');
}
for(int i = 2; ~i; i = pre[i]){
printf("pre = %d\n", i);
}*/
return ;
}

kuangbin_ShortPath L (POJ 2502)的更多相关文章

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

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

  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. L - Subway - POJ 2502

    题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到 ...

  4. POJ 2502 最短路

    http://poj.org/problem?id=2502 同一条地铁线上的站点相邻点间按照v2建边,然后所有点之间按照v1更新建边,所有的边都是双向边,both directions. 然后直接跑 ...

  5. Subway POJ 2502

    题目链接: http://poj.org/problem?id=2502 题目大意: 你刚从一个安静的小镇搬到一个吵闹的大城市,所以你不能再骑自行车去上学了,只能乘坐地铁或者步行去上学.因为你不想迟到 ...

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

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

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

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

  8. Dijkstra+计算几何 POJ 2502 Subway

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

  9. kuangbin_ShortPath J (POJ 1511)

    其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...

随机推荐

  1. arm裸机驱动错误总结

    错误001:

  2. [Windows驱动]流媒体驱动开发

    从Windows98开始,Windows流媒体驱动遵循Windows Driver Model(WDM)模型并使用Kernel Streaming(KS)组件.Kernel Streaming(KS) ...

  3. public protected default private

    简单来说,如果让一个变量或者方法,只想让自己类中的访问,那么就将它们设置成private 如果你想让一个变量或者方法,本包中的类可以访问,而且子类也可访问,但是包外的缺不想让他访问.就设置成prote ...

  4. Linux下GCC的使用

    1简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objective ...

  5. hdu1116 欧拉回路

    //Accepted 248 KB 125 ms //欧拉回路 //以26个字母为定点,一个单词为从首字母到末尾字母的一条边 //下面就是有向图判断欧拉回路 //连通+节点入度和==出度和 或者 存在 ...

  6. 算法导论 第六章 思考题 6-3 d叉堆

    d叉堆的实现相对于二叉堆变化不大,首先看它如何用数组表示. 考虑一个索引从1开始的数组,一个结点i最多可以有d个子结点,编号从id - (d - 2) 到 id + 1. 从而可以知道一个结点i的父结 ...

  7. webservice basics

    =====================Quote starts================== JAX-WS (JavaTM API for XML-Based Web Services)规范 ...

  8. 【LEETCODE OJ】Clone Graph

    Problem link: http://oj.leetcode.com/problems/clone-graph/ This problem is very similar to "Cop ...

  9. 程序自启动及后台持续运行的研究(voip和GPS方式)

    1,voip方式    1)首先要修改应用的plist配置,添加下面的设置:        Application does not run in background: NO        Requ ...

  10. C#: .net序列化及反序列化 [XmlElement(“节点名称”)]

    .net序列化及反序列化 序列化是指一个对象的实例可以被保存,保存成一个二进制串,当然,一旦被保存成二进制串,那么也可以保存成文本串了.比如,一个计数器,数值为2,我们可以用字符串“2”表示.如果有个 ...