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

题意:

有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$的最小环长度。

 #include<iostream>
//#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<climits>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f int n, m;
const int maxn = ;
int g[maxn][maxn], d[maxn][maxn], pos[maxn][maxn], ans;
vector<int>path; void get_path(int i, int j)
{
if(pos[i][j] == )return;
get_path(i, pos[i][j]);
path.push_back(pos[i][j]);
get_path(pos[i][j], j);
} void floyd()
{
ans = inf;
memcpy(d, g, sizeof(g));
for(int k = ; k <= n; k++){
for(int i = ; i < k; i++){
for(int j = i + ; j < k; j++){
if(ans > (long long)d[i][j] + g[j][k] + g[k][i]){
ans = d[i][j] + g[j][k] + g[k][i];
path.clear();
path.push_back(i);
get_path(i, j);
path.push_back(j);
path.push_back(k);
}
}
}
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
if(d[i][j] > d[i][k] + d[k][j]){
d[i][j] = d[i][k] + d[k][j];
pos[i][j] = k;
}
}
}
}
} int main()
{
while(scanf("%d%d", &n, &m) != EOF){
memset(g, 0x3f, sizeof(g));
memset(pos, , sizeof(pos));
for(int i = ; i <= n; i++){
g[i][i] = ;
}
for(int i = ; i < m; i++){
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
g[u][v] = g[v][u] = min(g[u][v], w);
} floyd();
if(ans == inf){
printf("No solution.\n");
}
else{
for(int i = ; i < path.size(); i++){
printf("%d", path[i]);
if(i == path.size() - ){
printf("\n");
}
else{
printf(" ");
}
}
}
}
return ;
}

poj1734 Sightseeing trip【最小环】的更多相关文章

  1. poj1734 Sightseeing trip[最小环]

    一个最小环裸题.最小环的两种求法dijkstra和Floyd直接参见这里我就是从这里学的,不想写了. 注意这里最重要的一个点是利用了Floyd的dp过程中路径上点不超过$k$这一性质,来枚举环上最大编 ...

  2. POJ1734 Sightseeing trip (Floyd求最小环)

    学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...

  3. poj1734 Sightseeing trip(Floyd求无向图最小环)

    #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...

  4. POJ1734 - Sightseeing trip

    DescriptionThere is a travel agency in Adelton town on Zanzibar island. It has decided to offer its ...

  5. 「POJ1734」Sightseeing trip

    「POJ1734」Sightseeing trip 传送门 这题就是要我们求一个最小环并且按顺序输出一组解. 考虑 \(O(n^3)\) 地用 \(\text{Floyd}\) 求最小环: 考虑 \( ...

  6. 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd

    题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...

  7. 【poj1734】Sightseeing trip

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8520   Accepted: 3200 ...

  8. URAL 1004 Sightseeing Trip(最小环)

    Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...

  9. poj 1734 Sightseeing trip判断最短长度的环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151 ...

随机推荐

  1. Android——SQLite/数据库 相关知识总结贴

    android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...

  2. 用 JAAS 和 JSSE 实现 Java 安全性

    JAAS 和 JSSE 概述 JAAS 提供了一种灵活的.说明性的机制,用于对用户进行认证并验证他们访问安全资源的能力.JSSE 定义了通过安全套接字层(SSL)进行安全 Web 通信的一种全 Jav ...

  3. Ubuntu界面重新安装图形界面

    前两天装了ubuntu12升级到16,升级完成后发现界面用的时候怪怪大的,感觉界面有问题:就从新安装了一下,还挺好用,shell脚本如下: #/bin/bash sudo apt-get update ...

  4. 【Visual Studio】VS发布应用未能创建默认证书的问题解决方法

    解决方法:点击你创建的项目 右键> 属性>签名>从存储区选择>选择证书这时候显示无可用证书 ,然后我从文件区选择了一个结果,又出现了第二个问题.提示我“签名时出错: 指定了无效 ...

  5. Kriging插值法

    克里金法是通过一组具有 z 值的分散点生成估计表面的高级地统计过程.与插值工具集中的其他插值方法不同,选择用于生成输出表面的最佳估算方法之前,有效使用克里金法工具涉及 z 值表示的现象的空间行为的交互 ...

  6. 针对个别utf8编码站点在iis7上浏览器编码不能自动识别的解决方法

    个别utf8编码站点在iis7上客户端浏览器编码不能自动识别的编号,输入仍为gbk2312,而不是utf-8,这样导致我们看到的网站为乱码. 要解决这个问题,有两个方法,一为打开网站以后,右键,选择编 ...

  7. Clustered Shading架构实现步骤

    最终决定越过Forward+,一步到位,直接调整至更先进的Clustered架构.步骤如下: 里程碑1:以CPU方式实现Light Culling,旨在理念验证,并与D3D10兼容里程碑2:以GPU ...

  8. jpush在有网的情况下6002

    网络处理问题. https://www.jpush.cn/qa/?qa=2476/%E7%BD%91%E7%BB%9C%E6%AD%A3%E5%B8%B8%E7%9A%84%E6%83%85%E5%8 ...

  9. 9款最好的JavaScript压缩工具

    削减是一个从源代码中删除不必要的字符的技术使它看起来简单而整洁.这种技术也被称为代码压缩和最小化.在这里,我们为你收集了10个最好的JavaScript压缩工具将帮助您删除不必要的空格,换行符,评论, ...

  10. PentesterLab渗透演练平台

    转载自: https://www.blackh4t.org/archives/1143.html http://www.91ri.org/5958.html     1.  什么是WebApp Pen ...