hdu1599 find the Mincost Route Floyd to find the minimum weight of the ring

Source: Internet
Author: User

Find the Mincost route

Time limit:1000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3456 Accepted Submission (s): 1409


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

Source HDU 2007-spring Programming Contest-warm up (1)

Recommend8600 | We have carefully selected several similar problems for you:1142 1217 1597 1301 1054 The subject of the test instructions when the Chinese, so the translation will not have to say, we look at that will know, the subject to obtain When given a bidirectional diagram of the path, ask if you have the shortest ring, if there is, then output his minimum length, if not on the output IMPOSSIBL topic if you search with DFS will time out, so try not to suppress the use of DFS for searching needs, before in other OJ have seen a lifetimes topic, The problem is also the shortest path of the ring but Xu Yaping output path, the mention I used DFS, but this problem is not enough, so still can only use Floyd do the following code can be used as the last class
#include <cstdio>#include<cstring>#defineFind_min (A, b) a<b?a:bConst intN =101;Const intINF =0x7ffffff;intMat[n][n],dist[n][n],pre[n][n],path[n],n;intMain () {intI,j,k,m,a,b,c; intnum;  while(~SCANF ("%d%d",&n,&m)) {         for(i=1; i<=n;i++){             for(j=1; j<=n;j++) {Mat[i][j]=dist[i][j]=INF; PRE[I][J]=i; }        }         while(m--) {scanf ("%d%d%d",&a,&b,&c); MAT[A][B]=mat[b][a]=dist[a][b]=dist[b][a]=find_min (MAT[A][B],C); }        intmin=INF;  for(k=1; k<=n;k++) {//The shortest path will be shorted to the end of the link, then a minimum loop             for(i=1; i<k;i++){                 for(j=i+1; j<k;j++){                    //The minimum ring can not be used to two points between the shortest slack, because (i,k) between the shortest circuit, (K,J) the shortest possible overlap between the part//so mat[][] actually is not updated, here and the simple Floyd the shortest circuit is not the same//Dist[i][j] Saved is the shortest path weight of I to J and                    intTMP=DIST[I][J]+MAT[I][K]+MAT[K][J];//here K and I and J are directly connected in the mat.                    if(tmp<min) {min=tmp; Num=0; intp=J;  while(p!=i) {//Backtrackingpath[num++]=p; P=PRE[I][P];//Pre[i][j] Represents a point in front of J on the I to J Shortest Path} path[num++]=i; Path[num++]=K; }                }            }             for(i=1; i<=n;i++){                 for(j=1; j<=n;j++){                    if(dist[i][j]>dist[i][k]+Dist[k][j]) {Dist[i][j]=DIST[I][K]+DIST[K][J];//dist[][] Save two points the shortest distance betweenpre[i][j]=Pre[k][j]; }                }            }        }        if(Min==inf) puts ("It ' s impossible."); Else{printf ("%d\n", Min); }    }    return 0;}

hdu1599 find the Mincost Route Floyd to find the minimum weight of the ring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.