problem Description
Hangzhou has n scenic spots, there are some two-way connection between 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 k>2, that is, except 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.
Input
- + ).
Output
For each test instance, the minimum value of the output is spent if such a route can be found. If not found, output "It's impossible. " ".
Sample Input
3 3 1 2 1 2 3 1 1 3 1 3 3 1 2 1 1 2 3 2 3 1
Sample Output
3 It ' s impossible.
This problem test instructions very simple, is to seek the smallest ring. If you use the simplest shortest-circuiting solution, the complexity is high. What to do? Solve with floyed, enumerate each point k, and connect it to the two points i,j (I,j are <k), make the I,j,k form the ring. ANS=MIN{ANS,DIS[I][J]+G[K][I]+G[K][J]}, according to floyed principle, if i,j<k dis[i][j] is independent of K. So we have to calculate the maximum value, and do the update. In addition, if the input format gives the form of edge right, must pay attention to the problem of heavy edge, this is very pit Dad!!!
1 #pragmaComment (linker, "/stack:1024000000,1024000000")2#include <iostream>3#include <cstdio>4#include <cstring>5#include <cmath>6#include <math.h>7#include <algorithm>8#include <queue>9#include <Set>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - intdirx[]={0,0,-1,1}; - intdiry[]={-1,1,0,0}; - #definePI ACOs (-1.0) + #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long A #defineEPS 1e-10 at #defineMOD 1000000007 - #defineN 106 - #defineINF 1<<26 - intn,m; - intMp[n][n]; - intDis[n][n]; in intAns_mincost; - voidFlyod () { to for(intk=1; k<=n;k++){ + for(intI=1; i<k;i++){ - for(intj=i+1; j<k;j++){ theAns_mincost=min (ans_mincost,mp[i][k]+mp[k][j]+dis[i][j]); * } $ }Panax Notoginseng for(intI=1; i<=n;i++){ - for(intj=1; j<=n;j++){ theDis[i][j]=min (dis[i][j],dis[i][k]+dis[k][j]); + } A } the } + } - $ intMain () $ { - while(SCANF ("%d%d", &n,&m) = =2){ - the for(intI=1; i<=n;i++){ - for(intj=1; j<=n;j++){Wuyi if(I==J) mp[i][j]=dis[i][j]=0; the Elsemp[i][j]=dis[i][j]=inf; - } Wu } - About for(intI=0; i<m;i++){ $ inta,b,d; -scanf"%d%d%d",&a,&b,&d); - if(d<Mp[a][b]) -mp[a][b]=mp[b][a]=dis[a][b]=dis[b][a]=D; A } + theans_mincost=inf; - $ Flyod (); the the if(ans_mincost!=inf) theprintf"%d\n", ans_mincost); the Else -printf"It ' s impossible.\n"); in the } the return 0; About}
View Code
HDU 1599 Find the Mincost route (Flyod to find the smallest ring)