poj 2502 Dijkstra
来源:互联网 发布:本地文件同步软件 编辑:程序博客网 时间:2024/06/02 00:37
Subway
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 8667 Accepted: 2801
Description
You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.
Input
Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.
Output
Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.
Sample Input
0 0 10000 10000 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
Sample Output
21
题意:
给出你起点和终点
再给你 若干 条地铁线,每一条地铁线有 若干 站点
求从起点到终点的最短的时间
题解:
首先当然是建图
建图后就简单了,直接用dijkstra就行了
注意:
这里求的是时间(分钟)
题目中给出了不同的速度
每一个站点间可以步行走过去
只有相邻的站点才有路通过去
所有的路径都是双向的
时间=路径/ 速度
这里的速度单独列出来还不会出错,我就是直接写在上面并且还是化简的反而出错了
通过这个题目知道了,自己还是看别人的解题报告看太多了,模板也写不出,都是套用的
这些都是不好的习惯,希望继续加油
#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define INF 0x3f3f3f3fstruct node{ int x,y;}a[300],e,frist,endn;int cnt;double map[300][300];int find_it(int x,int y){ for(int i=1;i<cnt;i++) if(a[i].x==x&&a[i].y==y) return i; return cnt++;}double calc(int i,int j){ double x=a[i].x-a[j].x; double y=a[i].y-a[j].y; return sqrt(x*x+y*y);}double path[300];int flag[300];int find_min(int n){ int posi=-1; int min_weight=INF; for(int i=1;i<=cnt;i++) if(min_weight>path[i]&&flag[i]==0){ posi=i; min_weight=path[i]; } return posi;}void Dijkstra(){ for(int i=1;i<=cnt;i++){ path[i]=INF; flag[i]=0; } path[1]=0; int p=1; while(p!=-1) { flag[p]=1; for(int i=1;i<=cnt;i++) if(path[i]>map[p][i]+path[p]&&flag[i]==0) path[i]=map[p][i]+path[p]; p=find_min(cnt); }}int main(){ double v1=40000.0/60; double v2=10000.0/60; //freopen("in.txt","r",stdin); for(int i=0;i<=250;i++) for(int j=0;j<=250;j++) map[i][j]=INF; scanf("%d%d%d%d",&a[1].x,&a[1].y,&e.x,&e.y); cnt=2; int p1,p2; while(scanf("%d%d",&frist.x,&frist.y)!=EOF) { p1=find_it(frist.x,frist.y); a[p1].x=frist.x; a[p1].y=frist.y; while(scanf("%d%d",&endn.x,&endn.y),endn.x!=-1&&endn.y!=-1) { p2=find_it(endn.x,endn.y); a[p2].x=endn.x; a[p2].y=endn.y; if(p1!=p2){ double temp=calc(p1,p2)/v1; map[p1][p2]=map[p2][p1]=min(map[p1][p2],temp); } p1=p2; } } a[cnt].x=e.x; a[cnt].y=e.y; for(int i=1;i<=cnt;i++) for(int j=1;j<=cnt;j++) if(i!=j) map[i][j]=min(map[i][j],calc(i,j)/v2); Dijkstra(); printf("%.0lf",path[cnt]); return 0;}
0 0
- POJ 2502 Dijkstra
- POJ 2502 Dijkstra
- Dijkstra-POJ-2502-Subway
- poj 2502 Dijkstra
- POJ 2502 Subways(Dijkstra算法)
- POJ 2502 Dijkstra OR spfa
- poj 2502 dijkstra(关键是建图)
- Dijkstra POJ
- poj 2502 Subway dijkstra基础 !!!!入门题
- poj 2502 题解 & dijkstra的堆优化
- poj 2502 Subway(dijkstra 最短路)
- POJ-2502 Subway(最短路 Dijkstra)
- poj 3159 heap+dijkstra
- poj 2253 - Frogger --- dijkstra
- poj 3268 dijkstra
- POJ 1847 Dijkstra应用
- poj 1847(Tram) Dijkstra
- poj 2253 : Frogger (dijkstra)
- 判断是否为二分图
- Pull和SAX解析XML,以解析中国省市列表为例子
- 几何画板画虚线的方法
- javafx8无法读取系统中文字体
- Android 中WebView与JS交互
- poj 2502 Dijkstra
- Android Studio快捷键
- 基于tcl tk 的HyperView二次开发实例【3】
- 工作后,我该如何学习?
- 301跳转.htaccess 详解以及 .htaccess 参数说明
- 大数据应用
- NSIS x64 位系统下安装
- 针对复制东西Eclipse或者MyEclipse中出现cannot paste the clipboard contents into the selected elements报错问题
- struts2表单重复提交问题