kuangbin_ShortPath L (POJ 2502)
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)的更多相关文章
- POJ 2502 - Subway Dijkstra堆优化试水
做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...
- 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 ...
- L - Subway - POJ 2502
题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到 ...
- POJ 2502 最短路
http://poj.org/problem?id=2502 同一条地铁线上的站点相邻点间按照v2建边,然后所有点之间按照v1更新建边,所有的边都是双向边,both directions. 然后直接跑 ...
- Subway POJ 2502
题目链接: http://poj.org/problem?id=2502 题目大意: 你刚从一个安静的小镇搬到一个吵闹的大城市,所以你不能再骑自行车去上学了,只能乘坐地铁或者步行去上学.因为你不想迟到 ...
- POJ 2502 Subway(迪杰斯特拉)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6692 Accepted: 2177 Descriptio ...
- POJ 2502 Subway (Dijkstra 最短+建设规划)
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6689 Accepted: 2176 Descriptio ...
- Dijkstra+计算几何 POJ 2502 Subway
题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...
- kuangbin_ShortPath J (POJ 1511)
其实虽然一开始有被这个题的8000MS 和 256MB限制又被吓到 但是严格来说跟之前的POJ 3268是一样的做法只是数据大了点 但是问题就出在数据大了点上 其实严格来说也不大 1e6 数组加起来大 ...
随机推荐
- JS页面打印,预览,设置,分页
一)在HTML页中加载打印对象 <object id="WebBrowser" width="0" height="0" classi ...
- Android-LogCat日志工具(二)
既然是Java语言,那么对于很多人来说,用System.out.println() 方法来打印日志是最熟悉.最简单不过了.不过在真正的项目开发中,是极度不建议使用 System.out.println ...
- Struts2 的ModelDriven理解
以UserAction为例,当UserAction实现了ModelDriven接口之后,与该接口相关的默认配置的拦截器会在拦截请求之后判断该请求是将要被UserAction处理而且UserAction ...
- 2013年国庆节前51Aspx源码发布详情
Sky软件公司网站修正版源码 2013-9-30 [VS2010]源码描述:针对Sky软件公司网站源码进行修正.修改ckeditor引用问题,发布样式错误问题.vs2010直接编译.发布成功. 网站 ...
- C++数据结构之List--线性实现
List(表)类似于队列,不同于队列的是,list可以随机读取/修改/插入某一position,通过position这一位置信息就可以直接修改相应位置的元素.实现方式和队列的类似,多了个positio ...
- javaweb-dbutils
package cn.itcast.demo; import java.io.File;import java.io.FileNotFoundException;import java.io.File ...
- 解决:Ubuntu12.04下使用ping命令返回ping:icmp open socket: Operation not permitted的解决
ping命令在运行中采用了ICMP协议,需要发送ICMP报文.但是只有root用户才能建立ICMP报文.而正常情况下,ping命令的权限应为-rwsr-xr-x,即带有suid的文件,一旦该权限被修改 ...
- 渐变背景 css3渐变效果及代码
渐变背景及代码 http://uigradients.com/#Behongo
- 30道四则运算<2>单元测试
该测试未实现除法 该测试中间多了/)两个符号,而且没有等号和回车. 该测试也没有符合除法要求 该测试也没有满足除法要求 该测试满足要求. 总结:程序中涉及到有除法的输出都有问题,多次改正未果:其他条件 ...
- HDU 3335
http://acm.hdu.edu.cn/showproblem.php?pid=3335 题意:在给出的n个数中找出一个集合,使得其中的数互不整除,求该集合最大的元素数量 首先要对输入的数去重,输 ...