[POJ 2502]Subway[最短路]
来源:互联网 发布:网络电玩城 编辑:程序博客网 时间:2024/05/22 12:50
题目链接:[POJ 2502]Subway[最短路]
题意分析:
你从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[n], &y[n])) { if (x[n] == -1 && y[n] == -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;}
0 0
- POJ 2502 Subway(最短路)
- poj 2502 subway (最短路)
- [POJ 2502]Subway[最短路]
- poj 2502最短路subway
- poj 2502 Subway(dijkstra 最短路)
- poj 2502 Subway (最短路 Dijksta)
- 【POJ 2502】Subway(最短路dij)
- POJ 2502 Subway 最短路 Floyd
- POJ-2502 Subway(最短路 Dijkstra)
- POJ 2502 Subway 单源点最短路模板
- POJ 2502 Subway (Dijkstra 最短路+建图)
- poj2502 Subway最短路
- POJ2502 Subway 最短路
- Subway(最短路)
- Subway(最短路)
- POJ 2502 Subway(Floyd单源最短路)
- POJ 2502 - Subway(单源最短路)
- POJ 2502 Subway ACM解题报告 (dijkstra求最短路)
- form中input标签的各种参数设置
- 使用RecyclerView CardView相关问题 解决The hierarchy of the type is inconsistent问题
- Http的编解码
- 欢迎使用CSDN-markdown编辑器
- org.apache.hadoop.hdfs.server.namenode.SafeModeException: Cannot delete
- [POJ 2502]Subway[最短路]
- PHP中获取当前页面的完整URL
- 43. Multiply Strings
- Andriod下音频的相关操作
- linux硬件级虚拟机系统 电脑安桌游戏多开完全去除vm标识去虚拟化
- lintcode-最长回文子串-200
- php 类型(对象)提示
- java基础-程序流程控制
- Codevs P3287 货车运输