Ural 1004 Sightseeing Trip

Sightseeing Trip

Time Limit: 2000ms
Memory Limit: 16384KB
This problem will be judged on Ural. 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 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, …, ykk > 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 nextM 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

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

Sample Output

1 3 5 2
No solution.

Source

 
解题:Floyd 求最小环
 
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int INF = 0x3f3f3f3f;
 4 const int maxn = 110;
 5 int n,m,d[maxn][maxn],w[maxn][maxn],fa[maxn][maxn];
 6 vector<int>cycle;
 7 int Floyd() {
 8     int minCycle = INF;
 9     for(int k = 1; k <= n; ++k) {
10         for(int i = 1; i < k; ++i)
11             for(int j = i + 1; j < k && w[i][k] < INF; ++j) {
12                 int tmp = d[i][j] + w[i][k] + w[k][j];
13                 if(tmp < minCycle) {
14                     minCycle = tmp;
15                     cycle.clear();
16                     int p = j;
17                     while(p != i) {
18                         cycle.push_back(p);
19                         p = fa[i][p];
20                     }
21                     cycle.push_back(i);
22                     cycle.push_back(k);
23                 }
24             }
25         for(int i = 1; i <= n; ++i)
26             for(int j = 1; j <= n && d[i][k] < INF; ++j) {
27                 int tmp = d[i][k] + d[k][j];
28                 if(tmp < d[i][j]) {
29                     d[i][j] = tmp;
30                     fa[i][j] = fa[k][j];
31                 }
32             }
33     }
34     return minCycle;
35 }
36 int main() {
37     int u,v,ww;
38     while(~scanf("%d",&n)) {
39         if(n == -1) return 0;
40         scanf("%d",&m);
41         for(int i = 0; i < maxn; ++i)
42             for(int j = 0; j < maxn; ++j) {
43                 d[i][j] = w[i][j] = INF;
44                 fa[i][j] = i;
45             }
46         while(m--) {
47             scanf("%d%d%d",&u,&v,&ww);
48             ww = min(ww,w[u][v]);
49             w[u][v] = w[v][u] = d[u][v] = d[v][u] = ww;
50         }
51         if(Floyd() == INF) puts("No solution.");
52         else {
53             printf("%d",cycle[0]);
54             for(int i = 1; i < cycle.size(); ++i)
55                 printf(" %d",cycle[i]);
56             putchar('\n');
57         }
58     }
59     return 0;
60 }
View Code

 

posted @ 2015-08-09 10:58  狂徒归来  阅读(325)  评论(0编辑  收藏  举报