POJ 2502 Subway

Subway

Time Limit: 1000ms
Memory Limit: 65536KB
This problem will be judged on  PKU. Original ID: 2502
64-bit integer IO format: %lld      Java class name: Main
 
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 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1

Sample Output

21

Source

Waterloo local 2001.09.22
 
解题:最短路,随便搞啦,反正点不多
 
 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 const int maxn = 500;
 7 struct Point{
 8     int x,y;
 9     Point(int a = 0,int b = 0){
10         x = a;
11         y = b;
12     }
13 };
14 vectorp;
15 const double speed[2] = {10000.0/60.0,40000.0/60.0};
16 double calc(int a,int b,double v){
17     double tmp = (p[a].x - p[b].x)*(p[a].x - p[b].x);
18     tmp += (p[a].y - p[b].y)*(p[a].y - p[b].y);
19     return sqrt(tmp)/v;
20 }
21 double d[maxn][maxn];
22 int main(){
23     int x,y;
24     scanf("%d%d",&x,&y);
25     p.push_back(Point(x,y));
26     scanf("%d%d",&x,&y);
27     p.push_back(Point(x,y));
28     for(int i = 0; i < maxn; ++i)
29         for(int j = 0; j < maxn; ++j)
30             d[i][j] = i == j?0:-1;
31     int pre = -1;
32     while(~scanf("%d%d",&x,&y)){
33         if(x == -1 && y == -1) {pre = -1;continue;}
34         p.push_back(Point(x,y));
35         int sz = p.size();
36         if(pre != -1) d[sz-2][sz-1] = d[sz-1][sz-2] = calc(sz-2,sz-1,speed[1]);
37         pre = x;
38     }
39     int sz = p.size();
40     for(int i = 0; i < sz; ++i)
41         for(int j = 0; j < sz; ++j)
42             if(d[i][j] == -1) d[i][j] = calc(i,j,speed[0]);
43     for(int k = 0; k < sz; ++k)
44         for(int i = 0; i < sz; ++i)
45             for(int j = 0; j < sz; ++j)
46                 if(d[i][k]!=-1&&d[k][j]!=-1)
47                     d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
48     printf("%.0f\n",d[0][1]);
49     return 0;
50 }
View Code

 

转载于:https://www.cnblogs.com/crackpotisback/p/4668486.html

你可能感兴趣的:(POJ 2502 Subway)