最短路径题集2
来源:互联网 发布:赢时胜软件笔试题 编辑:程序博客网 时间:2024/05/18 22:45
POJ 2502
原题链接: http://poj.org/problem?id=2502
大意:一个同学从家到学校,有几条地铁,给出家和学校的位置坐标和每条地铁线上的车站坐标,同学步行速度和地铁运行速度也给出。求同学从家到学校的最短时间。
原题中只有200个车站,所以直接用矩阵存储就可以。然后重点是如何建立图结构。车站与车站之间,必须是在一条地铁线上才想通。每有一个地铁站,就要计算家和学校步行到地铁站的时间,也作为一个距离。建图之后就是Dijkstra算法了。
我原来想存储边的信息,但是这样在本题中过于麻烦。(下面写了部分代码,还没写Dijkstra,)
#include <iostream>#include <algorithm>#include <cstring>#include "stdio.h"#include <math.h>using namespace std;struct edge{ int x1, y1; int x2, y2; float t;}a[40000];int main(){ int startx, starty, endx, endy; scanf("%d%d%d%d", &startx, &starty, &endx, &endy); int num = 0; int x1 = -1, y1 = -1, x2 = -1, y2 = -1; while ((scanf("%d%d", &x2, &y2)) != EOF){//这个题,建边是个大问题 if (x1 == -1 && y1 == -1) { x1 = x2; y1 = y2; a[num].x1 = startx; a[num].y1 = starty; a[num].x2 = x2; a[num].y2 = y2; a[num++].t = (float)(sqrt((startx - x2)*(startx - x2) + (starty - y2)*(starty - y2))) * 60 / 1000 / 10; a[num].x1 = x2; a[num].y1 = y2; a[num].x2 = endx; a[num].y2 = endy; a[num++].t = (float)(sqrt((endx - x2)*(endx - x2) + (endy - y2)*(endy - y2))) * 60 / 1000 / 10; continue; } a[num].x1 = x1; a[num].y1 = y1; a[num].x2 = x2; a[num].y2 = y2; a[num++].t = (float)(sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2)))*60/1000/40; a[num].x1 = startx; a[num].y1 = starty; a[num].x2 = x2; a[num].y2 = y2; a[num++].t = (float)(sqrt((startx - x2)*(startx - x2) + (starty - y2)*(starty - y2))) * 60 / 1000 / 10; a[num].x1 = x2; a[num].y1 = y2; a[num].x2 = endx; a[num].y2 = endy; a[num++].t = (float)(sqrt((endx - x2)*(endx - x2) + (endy - y2)*(endy - y2))) * 60 / 1000 / 10; x1 = x2; y1 = y2; }//处理完输入 return 0;}
阅读全文
0 0
- 最短路径题集2
- 最短路径题集1
- 最短路径(7)--poj1502(最短路径模板题)
- 最短路径问题2
- 最短路径--算法2
- 最短路径(2)--poj1797(Dijkstra变形题)
- 最短路径算法合集
- 街区最短路径问题 规律题
- 动物园查找最短路径题
- 【题解】最短路径四题
- 【训练题】最短路径树 SPFA
- poj 3268一道最短路径题
- Dijkstra最短路径模板2
- 数据结构之 最短路径2
- 单源点最短路径--2
- 图(最短路径)2
- 最短路径问题(2)
- 蓝桥-最短路径2-Dijstra
- 选择排序算法
- [转] 卷积
- 目标检测
- quartz学习
- Java中某个类的equals方法模板
- 最短路径题集2
- redis 源码简介he
- 一周目-6.26~6.30 结果
- php实现链式队列
- Java中return的用法
- python数据导入与转换
- 理解AtomicBoolean
- 几何形体对象--Geometry
- 《数据结构学习与实验指导》4-3:朋友圈