URAL 1004 Sightseeing Trip (min. ring)

Source: Internet
Author: User

Sightseeing Triptime limit:0.5 Second
Memory limit:64 Mbthere is a travel agency in Adelton Town on Zanzibar Island. It has decided tooffer its clients, besides many other attractions, sightseeing the town. Toearn as much as possible from this attraction, the agency have accepted ashrewd decision:it are necessary to find the SHO Rtest Route which begins andends at the same place. Your task is to write a program which finds such a route. In the town there is NCrossing points numbered from 1 to Nand MTwo-wayroads numbered from 1 to M. The crossing points can is connected by Multipleroads and no road connects a crossing point with itself. Each sightseeingroute is a sequence of road numbers y1,..., YK, k> 2. The road Yi(1≤ Ik? 1) connects crossing points XIand x I+1, the Road YKConnectscrossing points XKand x1. All the numbers x1,..., XKshould be different. The length of the sightseeing route is the sum of the lengths of all roads onthe sightseeing route, i.e. L ( y1) + L ( y2) + ... + L ( YK) Where L ( Yi) is thelength of the road Yi(1≤ Ik). Your program have to find such a sightseeingroute, the length of which was minimal, or to specify that it's not possible,be Cause there is no sightseeing route in the town. Inputinput contains TTests (1≤ T≤5). The first line of each test contains, integers:thenumber of crossing points NAnd the number of roads M(3≤ N≤100; 3≤ MN· ( N? 1)). Each of the next MLines describes one road. It contains 3 integers:the numberof It first crossing point aThe number of the second one b, and the length of the road L(1≤ a, bN; ab; 1≤ L≤300). Input is ended with a '? 1 ' line. Outputeach line of output are an answer. It contains either a string "No solution." In case there isn ' t any sightseeing route, or it contains thenumbers of all Cros Sing points on the shortest sightseeing route in the orderhow to pass them (i.e. the numbers x1 to XKFrom our definition of asightseeing route), separated by single spaces. If there is multiplesightseeing routes of the minimal length, you can output any one of the them. Sample
input Output
5 71 4 11 3 3003 1 101 2 162 3 1002 5 155 3 204 31 2 101 3 201 4 30-1
1 3 5 2No solution.
problem Source:Central European Olympiad in Informatics 1999
#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>#include<string>#include<stack>#include<queue>#include<vector>#defineINF 0x3f3f3f3f#defineMet (b) memset (a,b,sizeof a)typedefLong Longll;using namespacestd;Const intN = 1e2+Ten;Const intM =24005;Const intinf=0x7ffffff;intDist[n][n], w[n][n];intPre[n][n];intPath[n];intN, M, num, minc;voidFloyd () {Minc=INF;  for(intk=1; k<=n; k++) {         for(intI=1; i<k; i++)             for(intj=i+1; j<k; J + +) {                intans=dist[i][j]+w[i][k]+W[k][j]; if(Ans<minc) {//ж‰?е€°ж?? Д?? И§?Minc=ans; Num=0; intp=J;  while(p!=i) {//й?? Keyboard? ' Keyboard?? Ж‰?е‰?й?±й?? Keyboard?? By?? И? Keyboard?? Е№?е°?е ...? Е-?е?ёиμ ж??path[num++]=p; P=Pre[i][p]; } path[num++]=i; Path[num++]=K; }            }         for(intI=1; i<=n; i++)             for(intj=1; j<=n; J + +) {                if(dist[i][j]>dist[i][k]+Dist[k][j]) {Dist[i][j]=dist[i][k]+Dist[k][j]; PRE[I][J]=Pre[k][j]; }            }    }}intMain () {intu, V, cost;  while(Cin >>N) {if(n<0) Break; CIN>>m;  for(intI=1; i<=n; i++)             for(intj=1; j<=n; J + +) {Dist[i][j]=w[i][j]=INF; PRE[I][J]=i; }         for(intI=1; i<=m; i++) {scanf ("%d%d%d",&u,&v,&Cost ); if(Dist[u][v]>cost)//е¤?з?? Й?? И?№w[u][v]=w[v][u]=dist[u][v]=dist[v][u]=Cost ;        } Floyd (); if(minc==INF) printf ("No solution.\n"); Else{printf ("%d", path[0]);  for(intI=1; i<num; i++) printf ("%d", Path[i]); Puts (""); }    }    return 0;}

URAL 1004 Sightseeing Trip (min. 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.