1. 程式人生 > >POJ2502 Subway 最短路

POJ2502 Subway 最短路

### 一、內容 ```cpp 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. ```cpp 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 ```cpp 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 ```cpp 0 0 10000 1000 0 200 5000 200 7000 200 -1 -1 2000 600 5000 600 10000 600 -1 -1 ``` #### Sample Output ```cpp 21 1 ``` ### 二、思路 * 點編號為0,終點編號為201(因為最多200個站臺)。從起點到所有站臺,所有站臺到終點都建立一條步行的邊。兩兩站臺之間也要建立一條步行的邊。 * 然後就是一條航線中的相鄰站臺建立一條40km/h的花費的邊。 * 題目給出的座標算出來的距離單位是m, 給出的速度是km/h。 * 答案輸出應該四捨五入。 有點坑。 `int(d + 0.5)` ### 三、程式碼 ```cpp #include #include #include #include #include using namespace std; const int N = 205, M = 5e4 + 5; struct E { int v, next; double w; } e[M]; struct Stop { int x, y; } stop[N]; int id = 1, x, y, lx, ly, sx, sy, ex, ey, len = 1, h[N];//id代表點的標號 bool vis[N]; //起點是0 終點的編號是201 因為最多有200個站點 double d[N]; double getD(int x1, int y1, int x2, int y2, int t) { double x = x1 - x2; double y = y1 - y2; return sqrt(x*x + y*y) / t / 1000 * 60; } void add(int u, int v, double w) { e[len].v = v; e[len].w = w; e[len].next = h[u]; h[u] = len++; } //若a > b 返回true a <= b 返回false bool cmp(double a, double b) { if (a - b > 1e-4) return true; return false; } void spfa() { for (int i = 1; i < id; i++) d[i] = 1e18; d[0] = 0; d[201] = 1e18; queue q; q.push(0); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int j = h[u]; j; j = e[j].next) { int v = e[j].v; double w = d[u] + e[j].w; if (cmp(d[v], w)) { d[v] = w; if (!vis[v]) q.push(v), vis[v] = true; } } } } int main() { scanf("%d%d%d%d", &sx, &sy, &ex, &ey); while (~scanf("%d%d", &stop[id].x, &stop[id].y)) { x = stop[id].x, y = stop[id].y; add(0, id, getD(x, y, sx, sy, 10)); add(id, 201, getD(x, y, ex, ey, 10)); ++id; while (scanf("%d%d", &stop[id].x, &stop[id].y), stop[id].x != -1) { x = stop[id].x, y = stop[id].y; lx = stop[id - 1].x, ly = stop[id - 1].y; //從站點步行到終點 和起點 add(0, id, getD(x, y, sx, sy, 10)); add(id, 201, getD(x, y, ex, ey, 10)); //建立與前一個站點的邊 add(id, id - 1, getD(x, y, lx, ly, 40)); add(id - 1, id, getD(x, y, lx, ly, 40)); ++id; //-1的時候id不變 } } //每個站臺之間可以步行 總共的標號是【1, id-1】 for (int i = 1; i < id; i++) { for (int j = i + 1; j < id; j++) { x = stop[i].x, y = stop[i].y; lx = stop[j].x, ly = stop[j].y; add(i, j, getD(x, y, lx, ly, 10)); add(j, i, getD(x, y, lx, ly, 10)); } } spfa(); printf("%d", int(d[201] + 0.5));//四捨五入 return 0