1060: [Vijos 1046] sightseeing tour
Time Limit: 1 Sec memory limit: 128 MB
Description
After being a hundred-year-old school, the affiliated High School of Hunan Normal University receives a large number of visitors each year. The school believes that vigorously developing tourism can bring about a considerable amount of income.
There are N scenic spots in the school. Two scenic spots may be directly connected by road, and Dist [I, J] indicates its length; otherwise, there is no direct link between them. There is no direction for the road mentioned here. That is to say, if there is a direct road from I to J, then there is also a road from J to I, and the length is equal.
School Rules: each tourist's travel line can only be a loop (haadao rules ). That is to say, a visitor can set foot on any scenic spot and go through several scenic spots in sequence to return to the starting point. One day, Xiaomengxian decided to travel to the affiliated High School of Hunan Normal University. As he was very tired, he decided to take as few steps as possible. So he wants to ask you-a good programmer-to help him find the optimal route. How is it? (From depressing cashier)
Input
The input contains multiple groups of data. Use SeekEof to determine whether the end Of the file has been reached.
For each group of data:
The first line has two positive integers, N and M, indicating the number of scenic spots in the school and the number of pairs of scenic spots that are directly connected.
(N <= 100, M <= 10000)
In the following M rows, each row has three positive integers, representing the numbers at both ends of a road and the length of the road.
Output
For each group of data, output a line:
If the loop exists, a positive integer is output, indicating the total length of the loop. Otherwise, No solution. (No quotation marks are output)
Sample Input
5 6
1 4 1
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
4 3
1 2 10
1 3 20
1 4 30
Sample output
61
No solution.
Analysis:
Classic minimal ring problem.
Method for Finding the smallest ring: floyd.
The core code of floyd has two cyclic operations.
One is to update the smallest ring,
One is the shortest path of the update.
program vijos_1046; var i,ans,j,n,m,k,x,y,z,l:longint; a,d:array[1..100,1..100]of longint; function min(x,y:longint):longint; begin if x<y then exit(x); exit(y); end; procedure doing; var i,j:longint; begin readln(n,m); for i:=1 to n do for j:=1 to n do a[i,j]:=10000000; for i:=1 to m do begin readln(x,y,z); a[x,y]:=z; a[y,x]:=z; end; ans:=10000000; d:=a; for k:=1 to n do begin for i:=1 to k-1 do for j:=i+1 to k-1 do ans:=min(ans,d[i,j]+a[i,k]+a[k,j]); for i:=1 to n do for j:=1 to n do d[i,j]:=min(d[i,j],d[i,k]+d[k,j]); end; if ans=10000000 then writeln('No solution.') else writeln(ans); end; begin assign(input,'vijos.in'); reset(input); assign(output,'vijos.out'); rewrite(output); while not eof do doing; close(input); close(output); end.