Ural 1004 Sightseeing Trip

Source: Internet
Author: User

Sightseeing triptime limit:2000msmemory limit:16384kbthis problem would be judged onUral. Original id:1004
64-bit integer IO format: %lld Java class name: (any) 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 NCrossing points numbered from 1 to Nand MTwo-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 y1,..., YK, k> 2. The road Yi(1≤ Ik−1) connects crossing points XIand x I+1, the Road YKConnects crossing 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 on the sightseeing route, i.e. L ( y1) + L ( y2) + ... + L ( YK) Where L ( Yi) is the length of the road Yi(1≤ Ik). Your program have to find such a sightseeing-route, the length of which is minimal, or to specify that it's not possible, Because there is no sightseeing route in the town.InputInput contains a series of tests. The first line of each test contains, positive integers:the number of crossing points N≤100 and the number of roads M≤10000. Each of the next MLines 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). Input is ended with a "−1" line.OutputEach line of output is an answer. 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 what to pass them (i.e. the numbers x1 to XKFrom our definition 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 204 31 2 101 3 201 4 30-1
Sample Output
1 3 5 2No solution.
SourceCentral European Olympiad in Informatics 1999 problem solving: Floyd seeking minimum ring
1#include <bits/stdc++.h>2 using namespacestd;3 Const intINF =0x3f3f3f3f;4 Const intMAXN = the;5 intN,M,D[MAXN][MAXN],W[MAXN][MAXN],FA[MAXN][MAXN];6vector<int>cycle;7 intFloyd () {8     intMincycle =INF;9      for(intK =1; K <= N; ++k) {Ten          for(inti =1; I < K; ++i) One              for(intj = i +1; J < K && W[i][k] < INF; ++j) { A                 intTMP = D[i][j] + w[i][k] +W[k][j]; -                 if(TMP <mincycle) { -Mincycle =tmp; the cycle.clear (); -                     intp =J; -                      while(P! =i) { - Cycle.push_back (p); +p =Fa[i][p]; -                     } + Cycle.push_back (i); A Cycle.push_back (k); at                 } -             } -          for(inti =1; I <= N; ++i) -              for(intj =1; J <= N && d[i][k] < INF; ++j) { -                 intTMP = D[i][k] +D[k][j]; -                 if(TMP <D[i][j]) { inD[I][J] =tmp; -FA[I][J] =Fa[k][j]; to                 } +             } -     } the     returnmincycle; * } $ intMain () {Panax Notoginseng     intU,v,ww; -      while(~SCANF ("%d",&N)) { the         if(n = =-1)return 0; +scanf"%d",&m); A          for(inti =0; i < MAXN; ++i) the              for(intj =0; J < Maxn; ++j) { +D[I][J] = w[i][j] =INF; -FA[I][J] =i; $             } $          while(m--) { -scanf"%d%d%d",&u,&v,&ww); -WW =min (ww,w[u][v]); theW[U][V] = W[v][u] = D[u][v] = D[v][u] =ww; -         }Wuyi         if(Floyd () = = INF) puts ("No solution."); the         Else { -printf"%d", cycle[0]); Wu              for(inti =1; I < cycle.size (); ++i) -printf"%d", Cycle[i]); AboutPutchar ('\ n'); $         } -     } -     return 0; -}
View Code

Ural 1004 Sightseeing Trip

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.