Poj 2502 Subway
Subway
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 4511 | Accepted: 1473 |
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
Source
思路:本题的题意是,给出你家的地点位置(用坐标<X,Y>表示),然后给出你的学校的位置。然后给出一系列地铁站的坐标,一行上的站都是一条线路上的。然后相邻的地铁站之间的距离是相邻的两点之间的直线距离。步行的速度是10KM/H,地铁的速度是40KM/H,问从家到学校的最短时间。
首先可以求出每两个点之间的通过步行或地铁的最短时间。 然后就是最短路径了。
注意:这个题目需要注意地铁站之间的距离,使用地铁速度时,只有相邻的地铁站之间距离才是两点之间的直线距离。其他间接点之间的时间,如果要使用地铁的时间都要间接的通过其他的点。否则,使用直接距离就只能使用步行速度了。然后就是存储这些地点信息,地点只有200+2个,但是如果这些点可能会出现在多条地铁线上。所以要不能够将每一条地铁线上的站点都存一次,因为这样很可能会导致许多站点重复存储,甚至会使存储空间超出预期的空间。通过上述方法把距离换算成时间后,然后就用最短路径的解法就可以求出解了。
1 #include <cstdlib> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <string> 8 9 10 #define MAXD 1.0e20 11 12 13 using namespace std; 14 15 16 17 18 struct CityStops{ 19 int x,y; 20 }cityStops[200+5]; 21 22 23 24 25 26 double d[200+5][200+5]; 27 int vis[200+5]; 28 29 double lowcost[200+5]; 30 31 32 33 34 35 int pos=0; 36 37 int q[205]; 38 int Len=0; 39 40 41 int search(int x,int y,int n) 42 { 43 int i; 44 for(i=0;i<n;i++) 45 { 46 if((cityStops[i].x==x)&&(cityStops[i].y==y)) 47 return i; 48 } 49 return -1; 50 } 51 52 53 54 55 56 57 int main(int argc, char *argv[]) 58 { 59 int i,j,k; 60 int n; 61 pos=0; 62 63 //int beginflag,endflag; 64 65 66 67 68 69 for(i=0;i<=204;i++) 70 for(j=0;j<=204;j++) 71 {d[i][j]=MAXD;} 72 73 74 75 Len=0; 76 77 scanf("%d%d",&cityStops[pos].x,&cityStops[pos].y); 78 pos++; 79 scanf("%d%d",&cityStops[pos].x,&cityStops[pos].y); 80 if(search(cityStops[pos].x,cityStops[pos].y,pos)==-1) 81 pos++; 82 83 84 85 while(scanf("%d%d",&cityStops[pos].x,&cityStops[pos].y)!=EOF) 86 { 87 if((cityStops[pos].x==-1)&&(cityStops[pos].y==-1)) 88 {break;} 89 Len=0; 90 91 92 //beginflag=endflag=pos; 93 int tmp=search(cityStops[pos].x,cityStops[pos].y,pos); 94 if(tmp==-1) 95 {q[Len++]=pos;pos++;} 96 else 97 {q[Len++]=tmp;} 98 99 100 101 while(scanf("%d%d",&cityStops[pos].x,&cityStops[pos].y)!=EOF) 102 { 103 if((cityStops[pos].x==-1)&&(cityStops[pos].y==-1)) 104 {break;} 105 106 107 108 109 tmp=search(cityStops[pos].x,cityStops[pos].y,pos); 110 if(tmp==-1) 111 {q[Len++]=pos;pos++;} 112 else 113 {q[Len++]=tmp;} 114 115 116 117 118 119 120 121 122 123 } 124 125 int ii,jj; 126 for(ii=0;ii<Len-1;ii++) 127 //for(jj=ii+1;jj<Len;jj++) 128 {i=q[ii]; 129 j=i+1; 130 d[i][j]=d[j][i]=sqrt(1.0*(cityStops[i].x-cityStops[j].x)*(cityStops[i].x-cityStops[j].x)+(cityStops[i].y-cityStops[j].y)*(cityStops[i].y-cityStops[j].y)); 131 132 d[i][j]=d[i][j]*60/40000; 133 d[j][i]=d[i][j]; 134 135 } 136 137 138 139 140 141 142 } 143 144 145 146 147 n=pos; 148 149 150 151 for(i=0;i<n;i++) 152 for(j=0;j<n;j++) 153 { 154 if(i==j) 155 continue; 156 157 if(d[i][j]<MAXD) 158 continue; 159 160 d[i][j]=sqrt(1.0*(cityStops[i].x-cityStops[j].x)*(cityStops[i].x-cityStops[j].x)+(cityStops[i].y-cityStops[j].y)*(cityStops[i].y-cityStops[j].y)); 161 d[i][j]=d[i][j]*60/10000; 162 } 163 164 165 166 167 168 169 170 171 double mincost=MAXD; 172 173 for(i=0;i<n;i++) 174 { 175 vis[i]=0; 176 lowcost[i]=d[0][i]; 177 } 178 179 180 181 vis[0]=1; 182 183 184 185 for(i=1;i<n;i++) 186 { 187 k=-1; 188 mincost=MAXD; 189 190 for(j=0;j<n;j++) 191 { 192 if((vis[j]==0)&&(mincost>lowcost[j])&&(lowcost[j]<MAXD)) 193 {k=j;mincost=lowcost[j];} 194 } 195 196 vis[k]=1; 197 198 for(j=0;j<n;j++) 199 { 200 if((vis[j]==0)&&(lowcost[j]>lowcost[k]+d[k][j])) 201 {lowcost[j]=lowcost[k]+d[k][j];} 202 } 203 204 } 205 206 //for(i=0;i<n;i++) 207 208 printf("%.0f\n",lowcost[1]); 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 //system("PAUSE"); 227 return EXIT_SUCCESS; 228 }