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≤
I≤
k−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≤
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, 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