码迷,mamicode.com
首页 > 其他好文 > 详细

Sightseeing trip

时间:2018-03-01 13:17:43      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:describe   rate   clu   直接   accept   col   which   inpu   end   

Sightseeing trip
Time Limit: 1000MS Memory Limit: 65536K
Special Judge
Description

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 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 be 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 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

The first line of input 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).
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 crossing points on the shortest sightseeing route in the order how to pass them (i.e. the numbers x_1 to x_k 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

5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
Sample Output

1 3 5 2
Source

CEOI 1999

 题意:给你一张无向图,求图中的最小环,输出路径。

题解:

floyed求最小环

详见代码。

 

技术分享图片
 1 #include<cstdio>    
 2 #include<cstring>    
 3 #define find_min(a,b) a<b?a:b    
 4     
 5 const int N=109;    
 6 const int INF=0x7ffffff;    

 7 int f[N][N],dis[N][N],pre[N][N],path[N],n,m,num;  
 8     
 9 int main()    
10 {    
11         scanf("%d%d",&n,&m);
12       
13         for(int i=1;i<=n;i++)   
14             for(int j=1;j<=n;j++) 
15                 f[i][j]=dis[i][j]=INF,pre[i][j]=i;  
16                 
17         while(m--)
18         {    
19             int a,b,c;
20             scanf("%d%d%d",&a,&b,&c);    
21             f[a][b]=f[b][a]=dis[a][b]=dis[b][a]=find_min(f[a][b],c);    
22         }    
23     
24         int min=INF;    
25         for(int k=1;k<=n;k++)//最短路径外一点将最短路首尾链接,那么就得到一个最小环,枚举中间点  
26         {  
27             for(int i=1;i<k;i++)   
28                 for(int j=i+1;j<k;j++)
29                 {    
30                     //求最小环不能用两点间最短路松弛,因为(i,k)之间的最短路,(k,j)之间的最短路可能有重合的部分    
31                     //所以f[][]其实是不更新的,这里和单纯的floyd最短路不一样    
32                     //dis[i][j]保存的是 i 到 j 的最短路权值和,(i->j)    
33                     int tmp=dis[i][j]+f[i][k]+f[k][j];//这里k分别和i还有 j在f[][]中直接相连    
34                     if(tmp<min)
35                     {    
36                         min=tmp;num=0;    
37                         int p=j;    
38                         while(p!=i)//回溯  
39                         {  
40                             path[num++]=p;    
41                             p=pre[i][p];//pre[i][j]表示i->j最短路径上j前面的一个点    
42                         }    
43                         path[num++]=i; path[num++]=k;    
44                     }    
45                 }  
46             
47             for(int i=1;i<=n;i++)   
48                 for(int j=1;j<=n;j++)   
49                     if(dis[i][j]>dis[i][k]+dis[k][j])
50                     {    
51                         dis[i][j]=dis[i][k]+dis[k][j];//dis[][]保存两点间最短距离    
52                         pre[i][j]=pre[k][j];    
53                     }    
54                 
55         }    
56         if(min==INF)puts("No solution.");    
57         else
58         {    
59             printf("%d",path[0]);    
60             for(int i=1;i<num;i++)    
61                 printf(" %d",path[i]);    
62             puts("");    
63         }  
64     return 0;    
65 }    
View Code

 

Sightseeing trip

标签:describe   rate   clu   直接   accept   col   which   inpu   end   

原文地址:https://www.cnblogs.com/adelalove/p/8486682.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!