Find the Mincost route
Time limit:1000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3425 Accepted Submission (s): 1397
Problem description Hangzhou has n scenic spots, there are some two-way connection between the scenic spots, now 8600 want to find a tourist route, this route from a point and finally back to a point, assuming the route is V1,v2,.... VK,V1, then must meet the k>2, that is, apart from the starting point to go through at least 2 other different scenic spots, and can not be repeated through the same scenic area. Now 8600 needs you to help him find a route like this, and the less it costs the better.
The first line of input is 2 integers n and m (n <=, M <= 1000), representing the number of scenic spots and the number of roads.
In the next M-line, each row consists of 3 integers a,b,c. Represents a path between A and B and costs C (c <= 100).
Output for each test instance, the minimum value is spent if such a route can be found. If it is not found, output "It's impossible."
Sample INPUT3 31 2 12 3 11 3 13 31 2 11 2 32 3 1
Sample output3it ' s impossible.
Author8600: Wrong to helpless ... In fact, this problem let the ball at least through the shortest ring of three cities, with the idea is to use a map to record the initial edge weights, and then use DIS record the shortest road, and then Anser=min (Anser,dis[i][j]+map[j][k]+map[k][i]); The minimum ring weight of three cities is guaranteed, and the dis "i" [j] represents the shortest circuit code from I to J City:
1#include <stdio.h>2#include <string.h>3 #defineMIN (x, y) (x<y?x:y)4 Const intinf=0x3f3f3f;5 Const intmaxn= the;6 intMap[maxn][maxn],dis[maxn][maxn],anser;7 intn,m;8 voidInitial () {9 for(intI=1; i<= -; i++)Ten for(intj=1; j<= -; j + +) One if(I-J) map[i][j]=INF; A Elsemap[i][j]=0; - } - voidFloyd () { the for(intI=1; i<=n;i++) - for(intj=1; j<=n;j++) -dis[i][j]=Map[i][j]; -Anser=INF; + for(intk=1; k<=n;k++){ - for(intI=1; i<=n;i++) + for(intj=1; j<=n;j++) A if(i!=j&&i!=k&&j!=k) atAnser=min (anser,dis[i][j]+map[j][k]+map[k][i]); - /*if (Anser>dis[i][j]+map[j][k]+map[k][i]) { - printf ("dis[%d][%d]=%d\n", I,j,dis[i][j]); - printf ("map[%d][%d]=%d\n", J,k,map[j][k]); - printf ("map[%d][%d]=%d\n", K,i,map[k][i]); - }*/ in for(intI=1; i<=n;i++) - for(intj=1; j<=n;j++) toDis[i][j]=min (Dis[i][j],dis[i][k]+dis[k][j]);//Make maple wrong half a day .... + } - if(Anser==inf) puts ("It ' s impossible."); the Elseprintf"%d\n", Anser); * } $ intMain () {Panax Notoginseng inta,b,c; - while(~SCANF ("%d%d",&n,&M)) { the initial (); + while(m--){ Ascanf"%d%d%d",&a,&b,&c); the if(C<map[a][b]) map[a][b]=map[b][a]=C; + } - Floyd (); $ } $ return 0; -}
Find the Mincost route (min ring, shortest way, Floyd)