POJ 1734 Sightseeing Trip judging the shortest length of the ring

Source: Internet
Author: User

Sightseeing Trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5590 Accepted: 2151 Special Judge

Description

There is a travel agency in Adelton Town on Zanzibar Island. It has decided-clients, besides many other attractions and sightseeing the town. To earn as much as possible from this attraction, the agency have accepted a shrewd decision:it is necessary to find the S Hortest route which begins and ends at the same place. Your task is to write a program which finds such a route.

In the town there is N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. The crossing points can is connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, ..., Y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points X_i and X_{i+1}, the road Y_k connects crossing points X_k and X_1 . All the numbers x_1,..., X_k should is different. The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L (y_1) +l (y_2) +. . +l (Y_k) where L (y_i) is the length of the road Y_i (1<=i<=k). Your program have to find such a sightseeing-route, the length of which is minimal, or to specify that it's not possible,b Ecause there is no sightseeing route in the town.

Input

The first line of input contains, positive integers:the number of crossing points n<=100 and the number of roads m& lt;=10000. Each of the next M lines describes one road. It contains 3 positive integers:the number of its first crossing point, the number of the second one, and the length of T He road (a positive integer less than 500).

Output

There is only one line in output. It contains either a string ' No solution. ' In case there isn ' t any sightseeing route, or it contains the numbers of all CR Ossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k from our Defini tion of a sightseeing route), separated by single spaces. If There is multiple sightseeing routes of the minimal length, you can output any one of the them.

Sample Input

5 71 4 11 3 3003 1 101 2 162 3 1002 5 155 3 20

Sample Output

1 3 5 2

Source

CEOI 1999

[Submit]   [Go back]   [Status] [Discuss]

The solution on the internet is done with Floyd, using the Floyd shortest-circuit algorithm to determine if there is a ring, but I feel that DFS can solve the problem

Below is my code and the puzzle, I will also post on the internet Daniel with Floyd write algorithm

DFS: Take the edge as the core, the two points on the side as the starting point, one for the end point, start the DFS search. Each time you start from the starting point, if you find the end point and the distance is smaller, record the path, and then output the shortest path.

If the value of ANS is the same as the starting value, it is equivalent to no ring, then the output of the string can be

#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;Const intinf=9999999;intn,m;intmap[ the][ the];inthead[ the];inttemp;BOOLvis[ the];inta[ the];intstart, end;intans;BOOLjudge;intT;intCNT;intAns_num;voidDfsintUintNodeintdis) {     if(u==end) {              if(dis<ans) {ans=dis;  for(intI=0; i<node;i++) A[i]=Head[i]; A[node]=u; Ans_num=node; }              return ; } Head[node]=u;  for(intI=1; i<=n;i++){           if(! map[u][i]| |Vis[i])Continue; Vis[i]=true; if(dis+map[u][i]<ans) dfs (I,node+1, dis+Map[u][i]); Vis[i]=false; }      return;}intMain () { while(SCANF ("%d", &n)! =EOF) {        if(n==-1)             Break; memset (Map,0,sizeof(MAP)); scanf ("%d",&m); intu,v,w;  for(intI=0; i<m;i++) {scanf ("%d%d%d",&u,&v,&W); if(!Map[u][v]) {Map[u][v]=W; Map[v][u]=W; }           Else{              if(w<Map[u][v]) {Map[u][v]=W; Map[v][u]=W; }}} ans=inf;  for(intI=1; i<=n;i++)             for(intj=i+1; j<=n;j++){                if(map[i][j]==0)                Continue; memset (Vis,false,sizeof(VIS)); T=Map[i][j]; MAP[I][J]=0; Map[j][i]=0; Start=i; End=J; Vis[i]=true; DFS (Start,0, T); MAP[I][J]=T; Map[j][i]=T; }        if(ans!=inf) {                         for(intI=0; i<=ans_num;i++) printf ("%d%c", A[i],i==ans_num?'\ n':' '); }          Elseprintf ("No solution.\n"); }    return 0;}

Floyd algorithm Code

A graph is given to find a minimum loop and the output path.

Tell me about my feelings:

Including the minimum ring of point I and Point J, we can be seen as the combination of the shortest and minor short-circuit between I and J, can be obtained by Floyd any two points between the minimum distance, then we just find a shortest path outside the shortest way to ensure that I and J can reach. At the same time as the Floyd cycle, we take the minimum value of the ring weight (shortest path weight + Short circuit weight = minimum ring weight) as the standard, always update the precursor of each point, that is, record I to J shortest path, and can relax I and J of the point K (k is not the shortest route I to J) the least expensive (i.e., a short circuit between I and J) and then output in the natural order of the ring.

The code is also commented in detail:

#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 ("No solution."); Else{printf ("%d", path[0]);  for(i=1; i<num;i++) printf ("%d", Path[i]); Puts (""); }    }    return 0;}

POJ 1734 Sightseeing Trip judging the shortest length 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.