We can enumerate the edges, and use DFS to search for the answer. To do this, we need to mark the directions of the edges. But this method saves us the trouble of reperesenting the graph with vertices.
Solution 2 : Modified shortest path algorithm
Step 1 : Graph Representation
Let the endpoints be the vertices of the graph, and the distance between two endpoints of an edge be the length of the edge. Then connect the vertices that are at the same location. Now, we have a graph reprensented with an adjacency matrix.
Step 2: Find the minimum length cycle
Dijkstra : We enumerate edges. For each edge, we delete it from the graph, and use Dijkstra algorithm to find the distance between its two endpoints, then add it with the length of this edge. The sum is the miminum length of cycles including this edge. So, the minimum of the sums is the answer.
Modified Floyd :
Define g[i][j] as the initial distance of i and j, and dist[i][j] as the minimal distance between i and j.
Notice that, at the start of the kth time of the outermost loop in Floyd algorithm, the value of dist[i][j] is the minimum of distance between vertice i and vertice j via vertices whose number is less than k.
Then we consider the cycles including vertex k, and other vertices in it have vertex numbers less than k, i.e. 1,2,…,k-1. Let us enumerate the two vertices adjacent to vertex k in the cycle, and mark them as i and j, then the length of the cycle is g[k][i]+dist[i][j]+g[j][k].
Obviously, the positive minimal value of g[k][i]+dist[i][j]+g[j][k] (i!=j, i<k, j<k) is the answer.