poj2502
来源:互联网 发布:如何做好企业网络推广 编辑:程序博客网 时间:2024/05/18 00:41
Subway
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3689 Accepted: 1178
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
Source
Waterloo local 2001.09.22
#include<iostream>#include<cmath>#include<cstdio>using namespace std;const int MaxN=205;const int INF=9999999;int n;double map[MaxN][MaxN],d[MaxN];typedef struct{int x,y;}Stop;Stop st[MaxN];double dist(Stop a,Stop b)//返回两个的距离{ return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));}void dijkstra(){ int i,k=0,min; bool vis[MaxN]={0}; vis[0]=true; for(i=0;i<n;i++) d[i]=map[0][i]; while(k!=1) //找到终点的最短就可以不进行了,1是学校 { min=INF;for(i=0;i<n;i++)if(!vis[i] && d[i]<min)min = d[k=i];vis[k]=true;for(i=0;i<n;i++)if(!vis[i] && d[i]>d[k]+map[k][i])d[i]=d[k]+map[k][i]; }}int main(){int x,y,i;bool firstop=true; scanf("%d%d%d%d",&st[0].x,&st[0].y,&st[1].x,&st[1].y);//取家为0下标,学校为1下标 map[0][1]=map[1][0]=dist(st[0],st[1])/500*3;n=2; //前面有家和学校了while(scanf("%d%d",&x,&y)!=EOF){if(x==-1&&y==-1) {firstop=true; continue;}st[n].x=x;st[n].y=y; for(i=0;i<n;i++)//新输入的站能过步行到之前的站的距离map[n][i]=map[i][n]=dist(st[i],st[n])/500*3; if(!firstop)//不是第一站,则可能过地铁到前一个站 map[n][n-1]=map[n-1][n]=dist(st[n],st[n-1])/2000*3;else //是第一站,更新firstopfirstop=false;n++;}dijkstra();int re=int(d[1]+0.5);cout<<re<<endl;return 0;}
- poj2502
- poj2502
- poj2502
- poj2502 - Subway
- poj2502 SubWay
- Poj2502 Subway
- poj2502 Subway
- POJ2502 subway
- poj2502 最短路
- 最短路dijkstra--poj2502
- poj2502 Subway最短路
- POJ2502 Subway flody
- POJ2502——Subway
- POJ2502 Subway(Floyd)
- poj2502(SPFA+建图)
- POJ2502 Subway 最短路
- poj2502 subway dijkstra
- POJ2502 subway(spfa)
- windows-XP下不能查看局域网共享文件
- 编程珠玑 - 算法优化 - 过滤敏感词 - 第一步:快速实现
- 编程珠玑 - 算法优化 - 过滤敏感词 - 第二步:效率问题
- android source 分析
- 编程珠玑 - 算法优化 - 过滤敏感词 - 第三步:树形结构
- poj2502
- Selenium XPATH使用
- Bellman-Ford
- Android布局学习笔记
- 【Java练习2——进制转换】POJ 1220
- 有关Android线程的学习
- Gtkmm 主窗口与其对话框之间的切换
- vimrc_example.vim
- ftp修改主目录