題目鏈接:http://poj.org/problem?id=2502
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 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1
Sample Output
21
已知家和學校的坐標,並給出若干條地鐵線,以及步行速度和地鐵速度,求從家到學校最少需要多少時間
題目的難點在於輸入和建圖,由於輸入的點最多200個,我們可以直接讀入點的坐標,生成一個鄰接矩陣,而同一地鐵線上的各個站點,就可以在輸入是直接存入鄰接矩陣了,為了便於松弛,鄰接矩陣中我們存入路徑的用時,注意速度的單位是km/h,而路程的單位是m
建好圖之后,就是一個簡單的最短路問題了
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<map> 6 #include<cstdio> 7 #include<queue> 8 #include<stack> 9 10 using namespace std; 11 12 const int INF = 0x3f3f3f3f; 13 const int MAXN = 205; 14 const double wsp = 10 * 1000 / 60; 15 const double ssp = 40 * 1000 / 60; 16 17 struct Node{ 18 double x, y; 19 }node[MAXN]; 20 21 struct ff{ 22 int x, d; 23 ff(){} 24 ff( int a, double b ){ x = a; d = b; } 25 bool operator <( const ff & a )const{ 26 return d > a.d; 27 } 28 }; 29 30 int cnt; 31 double cost[MAXN][MAXN]; 32 double dis[MAXN]; 33 34 double gdis( int pre, int pos ){ 35 double dx = node[pre].x - node[pos].x; 36 double dy = node[pre].y - node[pos].y; 37 return sqrt( dx * dx + dy * dy ); 38 } 39 40 void dij(){ 41 for( int i = 1; i < MAXN; i++ ) 42 dis[i] = INF; 43 dis[1] = 0; 44 45 priority_queue<ff> Q; 46 Q.push( ff( 1, dis[1]) ); 47 48 while( !Q.empty() ){ 49 ff temp = Q.top(); Q.pop(); 50 int x = temp.x; 51 if( temp.d > dis[x] ) continue; 52 for( int i = 1; i < cnt; i++ ){ 53 if( dis[i] > dis[x] + cost[x][i] ){ 54 dis[i] = dis[x] + cost[x][i]; 55 Q.push( ff( i, dis[i] ) ); 56 } 57 } 58 } 59 } 60 61 int main(){ 62 ios::sync_with_stdio( false ); 63 64 for( int i = 0; i < MAXN; i++ ) 65 for( int j = 0; j < MAXN; j++ ) 66 cost[i][j] = INF; 67 68 cin >> node[1].x >> node[1].y >> node[2].x >> node[2].y; 69 cnt = 3; 70 71 while( cin >> node[cnt].x >> node[cnt].y ){ 72 cnt++; 73 while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == -1 && node[cnt].y == -1 ) ){ 74 cost[cnt][cnt - 1] = cost[cnt - 1][cnt] = gdis( cnt - 1, cnt ) / ssp; 75 cnt++; 76 } 77 } 78 79 for( int i = 1; i < cnt - 1; i++ ){ 80 cost[i][i] = 0; 81 for( int j = i + 1; j < cnt; j++ ){ 82 cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp ); 83 } 84 } 85 86 dij(); 87 88 cout << int( dis[2] + 0.5 ); 89 }