This article goes from here
Minimum Ring: Starting at one point, go back to the beginning as a loop through a simple path. The smallest ring in the graph is the smallest of all rings.
How to find the smallest ring?
1 Traditional Solutions (Dijkstra):
any one of the minimum ring weights, we can be regarded as two with the edge of the junction I, J of the direct distance plus I, J does not include The Edge (edge i->j) Shortest path. find the shortest path we first thought of the Dijkstra algorithm. What Dijkstra is asking for is the shortest distance from a point to all points. The shortest distance of I, J, which is asked by Dijkstra, must be the direct distance of I and J (if I,j is connected), so we need to delete the edge of I and J first (if the i,j is not connected, then do not delete), then use the shortest distance of I and J in Dijkstra novelty map. So each time we select an edge in the picture, we delete it. Then the 2 points corresponding to the deleted edge are Dijkstra, that is, the M-time Dijkstra.
2.floyd Minimum Ring:
Throw away the Dijkstra algorithm, and then we think about using the Floyd algorithm. We know that the Floyd algorithm continuously updates the Matrix dist (k) when it is in progress. Set Dist[k,i,j] indicates that from node I to node J and satisfies all intermediate nodes, they all belong to the right of a shortest path of set {A,?, k}. Where Dist[0,i,j] is the direct distance from the initial state I to J. For a given weighted graph, the weighted value and the smallest one are calculated. We can turn any of the rings into the following form: U->k->v---(x1-> x2->? xm1) U (U and K, K and V are directly connected), where V--(x1-> 2->? m), U is a path from V to u that does not go through K.
in the case of u,k,v determination, in order to minimize the ring weight, the requirement (x1 one >x2->? a >XM)->u path weight is minimized. That is to ask for V to u without the shortest path of k, then the shortest path of the u,k,v ring is: [V to U does not contain the shortest distance of K]+dist[o,u,k]+dist[o,k,v]. We can only find out any 2 points between the Floyd to meet the middle node is the shortest path of the set {,?, k}, but how can we find the shortest distance from V to u that does not contain k?
Now let's add a constraint to K: K is the largest node in the current ring (the maximum point for short). Because K is the maximum point, there is no point ≥k in the current ring, that is, all points are <k. Because V-> (X1->X2->......XM)->u belongs to the current ring, so x1,x2,? , Xm<k, or x1,x2. Xm≤k 11. Thus, the shortest distance from V to u can be expressed as Dist[k 11, u,v]. Dist[k a 1,v,u] represents the right of a shortest path from V to u and satisfies all intermediate nodes that belong to the set {,?, K 11}. Next, we can find the shortest distance from V to u that does not contain K. Here is only the request does not contain k, and the above method is Dist[k a 1,v,u], the path will never contain k+l,k+2,? In case the smallest ring to be asked contains k+1,k+2,? What do we do? Indeed, if the smallest ring contains a node larger than K, the ring found in the current u,k,v is obviously not the smallest ring. However, we know that there must be a maximum point ko in this smallest ring, that is, although the current K does not find the minimum ring we need, when we do the KO from K, all the points on this ring are smaller than the KO. This means that the minimum ring can be found when K=ko. We use an example to illustrate the assumption that the minimum ring is 1-3-4-5-6-2-1. Indeed, when U=l,v=4,k=3, k<6,dist[3,4,1] did not find 4-5-6-2-1 this ring, but when u=4,v=6,k=5 or u=5,v=2,k=6, Dist[k,v,u] This is the shortest path. So we'll just enumerate u after Floyd. V,k three variables to find the smallest ring. The time complexity is O (N3). We can see that the process of Floyd and the last enumeration u,v,k three variables for the minimum loop is u,v,k three variables, so we can merge them. In this way, we look for the smallest ring with the largest point k when the K variable is changed, that is, the Floyd algorithm is carried out.
POJ 1734
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #define_CLR (x, y) memset (x, y, sizeof (x))5 #defineINF 0XFFFFFFF6 #defineN 1107 using namespacestd;8 9 intMat[n][n], dist[n][n];Ten intNext[n][n];//Next[i][j] Represents the first point that I-to-j experiences. One intPath[n]; A intcnt, N; - - voidFloyd () the { - intmins=INF; - for(intk=1; k<=n; k++) - { + for(intI=1; i<k; i++) - for(intj=i+1; j<k; J + +) + { A intTMP = dist[i][j]+mat[i][k]+Mat[k][j]; at if(tmp < mins)//Update the weight of the minimum ring - { -mins =tmp; -Cnt=0; - intp =i; - while(P!=J)//record the path of the smallest ring in { -path[cnt++] =p; top =Next[p][j]; + } -path[cnt++] =J; thepath[cnt++] =K; * } $ }Panax Notoginseng for(intI=1; i<=n; i++) - for(intj=1; j<=n; J + +) the { + if(Dist[i][k]+dist[k][j] <Dist[i][j]) A { theDIST[I][J] = Dist[i][k] +Dist[k][j]; +NEXT[I][J] =Next[i][k]; - } $ } $ } - if(mins==INF) -Puts"No solution."); the Else - {Wuyi for(intI=0; i<cnt; i++) theprintf"%d%s", Path[i], i==cnt-1?"\ n":" "); - } Wu } - About voidInit () $ { - for(intI=1; i<=n; i++) - for(intj=1; j<=n; J + +) - { AMAT[I][J] = dist[i][j] =INF; +NEXT[I][J] =J; the } - } $ intMain () the { the intm, A, B, C; the while(~SCANF ("%d%d", &n, &m)) the { - Init (); in while(m--) the { thescanf"%d%d%d", &a, &b, &c); About if(C <Mat[a][b]) the { theMAT[A][B] = Mat[b][a] =C; theDIST[A][B] = Dist[b][a] =C; + } - } the Floyd ();Bayi } the return 0; the}
View Code
Floyd Minimum Ring