Find the Mincost route
Time limit:1000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3885 Accepted Submission (s): 1559
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
Sourcehdu 2007-spring Programming Contest-warm up (1)
Recommend8600 | We have carefully selected several similar problems for you:1595 1598 1596 1142 1162 The title is the smallest ring for the graph, because the data range is less than 100, so the Floyd is calculated , the method is very ingenious, can be used as a template problem learning. Test instructions: Chinese question, very good understanding. Attached code:
1#include <iostream>2#include <cstdio>3#include <cstring>4 #defineM 10055 #defineMAX 10000006 using namespacestd;7 intMap[m][m],dis[m][m];8 intN;9 Ten voidFloyd () One { A inti,j,k; - intans=MAX; - for(k=1; k<=n; k++) the { - for(i=1; i<k; i++) - for(j=i+1; j<k; J + +) - if(Ans>map[i][k]+map[k][j]+dis[j][i])//pay attention to the wording here. +ans=map[i][k]+map[k][j]+Dis[j][i]; - for(i=1; i<=n; i++) + for(j=1; j<=n; J + +) A if(dis[i][j]>dis[i][k]+Dis[k][j]) atdis[i][j]=dis[i][k]+Dis[k][j]; - } - if(ans<MAX) -printf"%d\n", ans); - Else -printf"It ' s impossible.\n"); in } - to intMain () + { - intm,i,j; the while(~SCANF ("%d%d",&n,&m)) * { $ for(i=1; i<=n; i++)Panax Notoginseng for(j=1; j<=n; J + +) - { the if(I==J) map[i][j]=dis[i][j]=0; + Elsemap[i][j]=dis[i][j]=MAX; A } the inta,b,c; + while(m--) - { $scanf"%d%d%d",&a,&b,&c); $ if(map[a][b]>c) -map[a][b]=map[b][a]=dis[a][b]=dis[b][a]=C; - } the Floyd (); - }Wuyi return 0; the}
HDU 1599 Find the Mincost route (smallest loop of the graph without direction)