Different types of questions are found in the review documents. I want to start with a simple question first, and the result will show that something is wrong. It's not easy.
We have encountered such a problem before, because we need to calculate the distance between two points. I thought about whether to calculate all the data first and put it into the array for separate calls.
But then I thought of it again. This time complexity is higher, and N * (n + 1)/2 is time complexity. It's a bit messy.
By referring to other answers on the Internet, we found that they also calculated and searched. This is the advantage of short answer.
The requirement of the question is to find the minimum time spent, and the Dijkstra algorithm is just being reviewed over the past few days. You can use it.
Here is a summary:
Algorithm general route: uses the starting point as the set flag of the shortest path, constantly searches for the shortest path in the remaining point, and adds it to the shortest path set flag one by one, the minimum distance from the starting point to each vertex is updated.
1. initialize the smallest distance array DIS from the starting point to each vertex. It is calculated based on the map of each node.
2. A total of N nodes are added to the smallest node in the current State in order for N large loops.
2.1 search for the shortest distance from the starting point using the greedy algorithm n times;
2.2 mark the shortest path and add the shortest path set flag;
2.3. The shortest path set flag is updated using the found node as the intermediary. That is, we can compare the shortest of sta --> J & sta --> pos --> J.
Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 891
/* Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 8911, input the input nodes respectively, and classify them according to the line mark; 2. Use Dijkstra to find the least cost path. It is true that the distance between two points must be calculated separately. Dijkstra. Calculate the shortest distance from the starting node to other nodes cyclically. */# Include <stdio. h> # include <math. h> # define M 202 # define INF 0x3f3f3ftypedef struct {Double X, Y;} node; node point [m]; int flag [m]; int Cate [m]; // double dis [m] on the same line; // FL = 1, walk; 2, subwaydouble calc (node A, Node B, int FL) // SQRT (A * A + B * B) {double tmp1 = (. x-b.x) * (. x-b.x); double tmp2 = (. y-b.y) * (. y-b.y); If (FL = 1) return SQRT (tmp1 + tmp2)/10000*60.0; return SQRT (tmp1 + tmp2)/40000*60.0;} // should be noted more Point, used to add the shortest path in the point loop, the role of I is to count, // do not need it in the loop to determine void Dijkstra (int sta, int N) {int I, j; for (I = 0; I <n; I ++) {flag [I] = 0;} for (I = 1; I <n; I ++) {dis [I] = calc (point [sta], point [I], 1);} flag [sta] = 1; for (I = 1; I <N; I ++) // Add each point {double min = inf; int Pos; For (j = 1; j <n; j ++) in a loop) // n-1 greedy {If (flag [J] = 1) continue; If (DIS [J] <min) {min = dis [J]; pos = J ;}} flag [POS] = 1; DIS [POS] = min; For (j = 1; j <n; j ++) {If (flag [J] = 1) continue; double T1 = dis [POS]; // pos -- J,'s distanceif (Cate [POS] = Cate [J] & (Pos = J-1 | j = pos-1) // The point of the intermediary comparison is POs, not I. Here, pay attention to T1 + = calc (point [POS], point [J], 2 ); // use the subway Method on an adjacent subway. Else t1 + = calc (point [POS], point [J], 1); If (t1 <dis [J]) dis [J] = T1 ;}}int main () {int I, j; Double X, Y; while (scanf ("% lf", & X, & Y )! = EOF) {If (x =-1 & Y =-1) break; point [0]. X = x; point [0]. y = y; scanf ("% lf", & point [1]. x, & point [1]. y); Cate [0] = 0; // at this time, assume that the start point and end point are not with the platform of any subway line. Cate [1] = 1; I = 2; j = 2; while (scanf ("% lf", & X, & Y) & (X! =-1) & (y! =-1) {Cate [I] = J; point [I]. X = x; point [I ++]. y = y; while (scanf ("% lf", & X, & Y) & (X! =-1) & Y! =-1) {Cate [I] = J; point [I]. X = x; point [I ++]. y = y;} J ++;}/* For (j = 0; j <I; j ++) printf ("[% d, % d],", point [J]. x, point [J]. y); */Dijkstra (0, I); printf ("%. 0lf \ n ", DIS [1]);} return 0 ;}
I was very excited to say that once I got the AC.