[POJ 2502]Subway[最短路]
2015-10-23 00:55
495 查看
题目链接:[POJ 2502]Subway[最短路]
题意分析:
你从A到B上学,可以选择走路或者到地铁站搭乘地铁到达,走路速度10km/h,地铁速度40km/h,你可以任意站点下车上车,问:最快要多少分钟到达学校?
解题思路:
建图求最短路即可。
地铁每条线路相邻站点之间建一条双向边,速度40km/h
所有点之间建双向边,速度10km/h
over。
个人感受:
心塞!!!spfa写错了超级粗心的错误,debug有两个多小时。然后debug完发现还是WA了,最终才知道是建图出了问题!!!当初觉得同一条线路肯定能搭地铁最快,没想到还能走路!!!TAT。给个这种样例,红色是地铁,蓝色是走路。如下:
![](https://img-blog.csdn.net/20151023005455178)
具体代码如下:
题意分析:
你从A到B上学,可以选择走路或者到地铁站搭乘地铁到达,走路速度10km/h,地铁速度40km/h,你可以任意站点下车上车,问:最快要多少分钟到达学校?
解题思路:
建图求最短路即可。
地铁每条线路相邻站点之间建一条双向边,速度40km/h
所有点之间建双向边,速度10km/h
over。
个人感受:
心塞!!!spfa写错了超级粗心的错误,debug有两个多小时。然后debug完发现还是WA了,最终才知道是建图出了问题!!!当初觉得同一条线路肯定能搭地铁最快,没想到还能走路!!!TAT。给个这种样例,红色是地铁,蓝色是走路。如下:
具体代码如下:
#include<cmath> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 311; const int INF = 0x7f7f7f7f; struct Edge{ int to, next; double w; }edge[2 * MAXN * MAXN]; int head[MAXN], cnt, sta[MAXN]; double x[MAXN], y[MAXN], dis[MAXN]; bool in[MAXN]; void add_edge(int from, int to, double w) { edge[cnt].to = to; edge[cnt].w = w; edge[cnt].next = head[from]; head[from] = cnt++; } void double_make(int from, int to, double w) { add_edge(from, to, w); add_edge(to, from, w); } double getdis(int a, int b) { double delx = x[a] - x[b], dely = y[a] - y[b]; return sqrt(delx * delx + dely * dely); } void spfa(int s) { dis[s] = 0; int top = 0; sta[top++] = s; in[s] = 1; while (top) { int cur = sta[--top]; in[cur] = 0; // in[cur] 写成了 in[s]。哭瞎。 for (int i = head[cur]; ~i; i = edge[i].next) { Edge &e = edge[i]; if (dis[e.to] > dis[cur] + e.w) { dis[e.to] = dis[cur] + e.w; if (!in[e.to]) { in[e.to] = 1; sta[top++] = e.to; } } } } } int main() { memset(head, -1, sizeof head); for (int i = 0; i < 311; ++i) dis[i] = INF; scanf("%lf %lf %lf %lf", &x[0], &y[0], &x[1], &y[1]); int n = 2, last = 0; while (~scanf("%lf %lf", &x , &y )) { if (x == -1 && y == -1) { last = 0; continue; } if (last) double_make(last, n, getdis(last, n) * 3.0 / 2000.0); last = n++; } for (int i = 0; i < n; ++i) for (int j = i + 1; j < n; ++j) { double_make(i, j, getdis(i, j) * 3.0 / 500.0); // 不管怎么样,在每个站点都有权利选择走到其它站点 } spfa(0); printf("%.0f\n", round(dis[1])); return 0; }
相关文章推荐
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot delete
- 欢迎使用CSDN-markdown编辑器
- NodeJS的代码调试和性能调优
- Xcode磁盘空间大清理
- 使用RecyclerView CardView相关问题 解决The hierarchy of the type is inconsistent问题
- Http的编解码
- form中input标签的各种参数设置
- 解决 多列 布局 左右等高问题
- c语言 -> 条件控制 / if / switch / for
- bzoj1614【Usaco2007 Jan】Telephone Lines 架设电话线
- Git学习 -- 搭建Git服务器
- 访问者模式
- org.apache.hadoop.security.AccessControlException: Permission denied:
- [深入理解Java虚拟机]第六章 字节码指令简介
- python svn
- vijos1909【noip2014】寻找道路
- 机器学习实战朴素贝叶斯的java实现
- arguments对象
- (六)黑马程序员——oc基础之封装
- Android开机自启动应用