POJ 2502 Subway

来源:互联网 发布:影视制作软件app 编辑:程序博客网 时间:2024/06/05 16:53

Subway
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 9437 Accepted: 3059

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.

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


觉得这个题的难点在输入上,还有用double是,交c++输出可以用%lf  如果是g++,那么只能是

%f,我这里错了三遍才知道咋回事

ac代码

#include <stdio.h>#include <math.h>#include <iostream>#include <algorithm>using namespace std;struct node{double x,y;}p[305];double map[305][305];double dist(node a,node b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int main(){int i,j,k,n;double sv,pv;for(i=0;i<305;i++){for(j=0;j<305;j++){map[i][j]=-1;}}scanf("%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y);n=2;sv=40000*1.0/60;pv=10000*1.0/60;node rec,next;next.x=-1;next.y=-1;while(scanf("%lf%lf",&rec.x,&rec.y)!=EOF){if(rec.x!=-1 || rec.y!=-1){p[n]=rec;if(next.x!=-1 || next.y!=-1){map[n][n-1]=map[n-1][n]=dist(p[n],p[n-1])/sv;}n++;}next=rec;}for(i=0;i<n;i++){for(j=0;j<n;j++){if(map[i][j]==-1)map[i][j]=dist(p[i],p[j])/pv;}}for(k=0;k<n;k++){for(i=0;i<n;i++){for(j=0;j<n;j++){if(map[i][j]>(map[i][k]+map[k][j]))map[i][j]=map[i][k]+map[k][j];}}}printf("%.0lf\n",map[0][1]);return 0;}


1 0