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

POJ1734 Sightseeing trip

时间:2017-09-27 17:44:38      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:strong   connected   ota   where   idt   esc   char   init   necessary   

Sightseeing trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 7114   Accepted: 2708   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

 
【题解】
Floyd求最小环。
最小环里一定存在一个编号最大的节点。
于是我们只需要在k这层循环开始前,看一下
过点k和小于k的点有没有环即可
看k的出边i,枚举i能到的点j,若j能到k,则
形成最小简单环(因为边权为负,不可能有负环)
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #define min(a, b) ((a) < (b) ? (a) : (b))
 6 #define max(a, b) ((a) > (b) ? (a) : (b))
 7 
 8 inline void read(long long &x)
 9 {
10     x = 0;char ch = getchar(), c = ch;
11     while(ch < 0 || ch > 9)c = ch, ch = getchar();
12     while(ch <= 9 && ch >= 0)x = x * 10 + ch - 0, ch = getchar();
13     if(c == -)x = -x; 
14 } 
15 
16 const long long INF = 0x3f3f3f3f;
17 const long long MAXN = 500 + 10;
18 const long long MAXM = 20000 + 10;
19 
20 long long n,m,g[MAXN][MAXN], w[MAXN][MAXN], ans, zhuan[MAXN][MAXN], l,r,mid, fangan[MAXN], tot;
21 
22 void dfs(long long s, long long t)
23 {
24     if(s == t || !zhuan[s][t])
25         return;
26     dfs(s, zhuan[s][t]);
27     fangan[++ tot] = zhuan[s][t];
28     dfs(zhuan[s][t], t);
29 }
30 
31 int main()
32 {
33     while(scanf("%d %d", &n, &m) != EOF) 
34     { 
35         ans = INF;
36         memset(g, 0x2, sizeof(g));
37         memset(w, 0x2, sizeof(w));
38         memset(zhuan, 0, sizeof(zhuan));
39         for(register long long i = 1;i <= m;++ i)
40         {
41             long long tmp1, tmp2;
42             read(tmp1), read(tmp2);
43             read(g[tmp1][tmp2]);
44             g[tmp2][tmp1] = w[tmp1][tmp2] = w[tmp2][tmp1] = g[tmp1][tmp2];
45         }
46         for(register long long k = 1;k <= n;++ k)
47         {
48             for(register long long i = 1;i < k;++ i)
49                 for(register long long j = 1;j < k;++ j)
50                 {
51                     if(i == j) continue;
52                     if(ans > w[k][i] + g[i][j] + w[j][k])
53                     {
54                         ans = w[k][i] + g[i][j] + w[j][k], l = i, r = j, mid = k;
55                         tot = 0;fangan[++tot] = mid;fangan[++tot] = l;dfs(l, r);fangan[++tot] = r;
56                     }
57                 }
58             for(register long long i = 1;i <= n;++ i)
59                 for(register long long j = 1;j <= n;++ j)
60                     if(g[i][j] > g[i][k] + g[k][j])
61                         g[i][j] =  g[i][k] + g[k][j], zhuan[i][j] = k;
62         }
63         if(ans >= INF)
64             printf("No solution.");
65         else 
66             for(register long long i = 1;i <= tot;++ i)
67                 printf("%lld ", fangan[i]);
68         putchar(\n);
69     } 
70     return 0;
71 }
POJ1734

 

POJ1734 Sightseeing trip

标签:strong   connected   ota   where   idt   esc   char   init   necessary   

原文地址:http://www.cnblogs.com/huibixiaoxing/p/7602666.html

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