博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1734 Sightseeing trip【最小环】
阅读量:4991 次
发布时间:2019-06-12

本文共 4454 字,大约阅读时间需要 14 分钟。

Sightseeing trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions:8588   Accepted:3224   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 71 4 11 3 3003 1 101 2 162 3 1002 5 155 3 20

Sample Output

1 3 5 2

Source

 

题意:

有n个点,m条带权无向边。希望找到一条路径最短的环,环上至少包含三个点,输出长度和路径。

思路:

第一次写这种最小环问题。其实本质就是要枚举节点,再枚举路径上和他直接相邻的两个节点。

在floyd算法的步骤里,外层循环k刚开始时,$d[i,j]$保存的是从$i$经过$1~k-1$中的某个节点到$j$的最短路长度。

那么$d[i,j] + g[j,k] + g[k,i]$就是一个从$i$经过$j$直接到$k$再直接回到$i$的一个环的路径,这个的最小值就是他对应的路径的最小环。

枚举每一个$k$,找到上面式子的最小值,就是整个图的最小环。

输出路径的话其实就是相当于一个dfs

注意点:

有重边,所以存的是边的最小值。

$d[i,j] + g[j,k] + g[k,i]$可能会在运算时超出int,因为可能有的两两节点之间没有边存在也就是inf

 

虐狗宝典阅读笔记:

对于有向图的最小环问题,可枚举起点$s = 1~n$,执行堆优化的Dijkstra算法求解单源最短路经。$s$一定是第一个被从堆中取出的节点,我们扫描$s$的所有出边,当扩展、更新完成后,令$d[s] = inf$,然后继续求解。当$s$第二次被从堆中取出时,$d[s]就是经过点$s$的最小环长度。

1 #include
2 //#include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 using namespace std;13 typedef long long LL;14 #define N 10001015 #define pi 3.141592653516 #define inf 0x3f3f3f3f17 18 int n, m;19 const int maxn = 105;20 int g[maxn][maxn], d[maxn][maxn], pos[maxn][maxn], ans;21 vector
path;22 23 void get_path(int i, int j)24 {25 if(pos[i][j] == 0)return;26 get_path(i, pos[i][j]);27 path.push_back(pos[i][j]);28 get_path(pos[i][j], j);29 }30 31 void floyd()32 {33 ans = inf;34 memcpy(d, g, sizeof(g));35 for(int k = 1; k <= n; k++){36 for(int i = 1; i < k; i++){37 for(int j = i + 1; j < k; j++){38 if(ans > (long long)d[i][j] + g[j][k] + g[k][i]){39 ans = d[i][j] + g[j][k] + g[k][i];40 path.clear();41 path.push_back(i);42 get_path(i, j);43 path.push_back(j);44 path.push_back(k);45 }46 }47 }48 for(int i = 1; i <= n; i++){49 for(int j = 1; j <= n; j++){50 if(d[i][j] > d[i][k] + d[k][j]){51 d[i][j] = d[i][k] + d[k][j];52 pos[i][j] = k;53 }54 }55 }56 }57 }58 59 int main()60 {61 while(scanf("%d%d", &n, &m) != EOF){62 memset(g, 0x3f, sizeof(g));63 memset(pos, 0, sizeof(pos));64 for(int i = 1; i <= n; i++){65 g[i][i] = 0;66 }67 for(int i = 0; i < m; i++){68 int u, v, w;69 scanf("%d%d%d", &u, &v, &w);70 g[u][v] = g[v][u] = min(g[u][v], w);71 }72 73 floyd();74 if(ans == inf){75 printf("No solution.\n");76 }77 else{78 for(int i = 0; i < path.size(); i++){79 printf("%d", path[i]);80 if(i == path.size() - 1){81 printf("\n");82 }83 else{84 printf(" ");85 }86 }87 }88 }89 return 0;90 }

 

转载于:https://www.cnblogs.com/wyboooo/p/9972306.html

你可能感兴趣的文章
01-html介绍和head标签
查看>>
Python之Linux下的 virtualenv
查看>>
ASP.NET Web开发框架之三 报表开发
查看>>
大家好
查看>>
PHP文件上传类
查看>>
Python基础 --- 使用 dict 和 set
查看>>
仿迅雷播放器教程 -- 基于VLC的MFC播放器 (6)
查看>>
Python之数据结构基础
查看>>
WPF:如何高速更新Model中的属性
查看>>
hdu 1010(DFS) 骨头的诱惑
查看>>
(转)Android SDK Manager国内无法更新的解决方案
查看>>
SQL语句修改表
查看>>
ubutnu 挂载磁盘
查看>>
continue 和 break的实例
查看>>
Java学习笔记()ArrayList
查看>>
redis缓存清除
查看>>
django Highcharts制作图表--显示CPU使用率
查看>>
文本处理 tr ,col,join,paste
查看>>
oracle权限
查看>>
java方法的虚分派和方法表
查看>>