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 数组加起来大 ...
随机推荐
- python3使用requests发闪存
闪存ing.cnblogs.com是博客园类似推特.饭否的服务, 我写了以下程序可以完成发闪存的操作,目的是顺便练习使用requests库. requests是一个python 轻量的http客户端库 ...
- hdoj-2031
#include "stdio.h"#include "stdlib.h"int main(){ char a[]={'0','1','2','3','4',' ...
- 深入学习:Windows下Git入门教程(上)
一,安装Git: 1.1Linux上安装命令: sudo apt-get install git 1.2在Windows上安装Git: 使用Windows版的msysgit,官方下载地址:http:/ ...
- BZOJ 1657 奶牛的歌声
单调栈. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- HDOJ-三部曲一(搜索、数学)-1002-Children of the Candy Corn
Children of the Candy Corn Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Jav ...
- Charlie's Change_完全背包&&路径记录
Description Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he ofte ...
- jQuery ajax传多个参数
ajax可以传送一个或多个参数到后台php中 <script> $(function(){ $("#sub_btn").click(function(){ var em ...
- 第一个Sprint冲刺第六天
讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:解决编写代码的问题 讨论地点:宿舍 进展:已开始对代码的编写
- timeZoneGetter
function timeZoneGetter(date) { // getTimezoneOffset 返回格林威治时间和本地时间之间的时差,以分钟为单位 var zone = -1 * date. ...
- Spark延长SparkContext初始化时间
有些应用中可能希望先在driver上运行一段java单机程序,然后再初始化SparkContext用集群模式操作java程序返回值.从而避免过早建立SparkContext对象分配集群资源,使资源长时 ...