1004. Sightseeing Trip
Time limit: 2.0 second Memory limit: 64 MB
There is a travel agency in Adelton town on Zanzibar island. It has decided to offer its clients, besides many other attractions, sightseeing the town. To earn as much as possible from this attraction, the agency has accepted a shrewd decision: it is necessary to find the shortest 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 are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be 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 xi and xi+1, the road yk connects crossing points xk and x1. All the numbers x1, …, xk should 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 has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible, because there is no sightseeing route in the town.
Input
Input contains a series of tests. The first line of each test contains two positive integers: the number of crossing points N ≤ 100 and the number of roads M ≤ 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 the road (a positive integer less than 500). Input is ended with a “−1” line.
Output
Each 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 crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x1 to xk from our definition of a sightseeing route), separated by single spaces. If there are multiple sightseeing routes of the minimal length, you can output any one of them.
Sample
input | output |
---|---|
5 7 1 4 1 1 3 300 3 1 10 1 2 16 2 3 100 2 5 15 5 3 20 4 3 1 2 10 1 3 20 1 4 30 -1 |
1 3 5 2 No solution. |
Problem Source: Central European Olympiad in Informatics 1999
***************************************************************************************
floyd算法求最小环
***************************************************************************************
1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<cstdio> 5 #include<cmath> 6 #include<algorithm> 7 #include<stack> 8 using namespace std; 9 const int N=105; 10 const int maxn=0xfffffff; 11 int m[N][N],dis[N][N],g[N][N]; 12 int c,n,ms,i,j,k; 13 int path[N]; 14 int s,t,w; 15 void dfs(int x,int y)//找路径 16 { 17 if(m[x][y]==-1)return; 18 dfs(x,m[x][y]); 19 path[++c]=m[x][y]; 20 dfs(m[x][y],y); 21 } 22 int main() 23 { 24 while(1) 25 { 26 cin>>n; 27 if(n==-1)break; 28 cin>>ms; 29 for(i=0;i<=n;i++) 30 for(j=0;j<=n;j++) 31 g[i][j]=maxn; 32 memset(m,-1,sizeof(m)); 33 for(i=1;i<=ms;i++) 34 { 35 cin>>s>>t>>w; 36 if(g[s][t]>w) 37 g[s][t]=g[t][s]=w;//求最小边权 38 } 39 memcpy(dis,g,sizeof(g)); 40 int min1=maxn; 41 for(i=1;i<=n;i++) 42 { 43 for(j=1;j<i;j++) 44 for(k=j+1;k<i;k++) 45 { 46 if(min1>dis[j][k]+g[j][i]+g[i][k])//求最小环 47 { 48 min1=dis[j][k]+g[j][i]+g[i][k]; 49 path[1]=i; 50 path[c=2]=j; 51 dfs(j,k); 52 path[++c]=k; 53 } 54 } 55 for(j=1;j<=n;j++)//求单边的最短距离 56 for(k=1;k<=n;k++) 57 if(dis[j][k]>dis[j][i]+dis[i][k]) 58 { 59 dis[j][k]=dis[k][j]=dis[j][i]+dis[i][k]; 60 m[j][k]=m[k][j]=i; 61 } 62 } 63 if(min1==maxn) 64 printf("No solution. "); 65 else 66 { 67 for(i=1;i<c;i++) 68 printf("%d ",path[i]); 69 printf("%d ",path[c]); 70 } 71 } 72 return 0; 73 }