Sightseeing trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9078   Accepted: 3380   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条边,求出图中的最小环(无向图,有重边)。
解题思路:
int maxn=105;
int a[maxn][maxn],f[maxn][maxn];
a:邻接矩阵,存图
利用floyd算法;
f:记录任意两点间的最短距离,初值为a.
f(k)[i][j]表示从顶点i到顶点j,中间顶点序号不大于k的最短路径长度。
f(k)[i][j]=min(f(k-1)[i][j],f(k-1)[i][k]+f(k-1)[k][j])   
 
则最小环可以表示为a[i][k]+a[k][j]+f(k-1)[i][j]
即表示从顶点i到顶点j,中间顶点序号不大于k-1的最短路径长度+i到k的边长+k到j的边长。(这样保证构成环,而没有重边)
#include<iostream>
#include<cstring>
using namespace std;
int n,m,ans=0x3f3f3f3f,s,t,temk=0x3f3f3f3f,cnt;
const int maxn=;
int a[maxn][maxn],d[maxn][maxn],f[maxn][maxn],path[maxn];
void dfs(int i,int j){
if(f[i][j]==){path[++cnt]=j;return;}
dfs(f[i][j],j);
}
void floy(){
memset(path,,sizeof(path));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
d[i][j]=a[i][j];
for(int k=;k<=n;k++){
for(int i=;i<k;i++)
for(int j=i+;j<k;j++)
if((long long)a[i][k]+a[k][j]+d[i][j]<ans){//注意数据类型,3个连加,容易超Int
ans=a[i][k]+a[k][j]+d[i][j];
s=i;t=j;
temk=k;
cnt=;
path[++cnt]=s;
dfs(s,t);//记录从s到t的中间节点,包含t,不含s.
path[++cnt]=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];
f[i][j]=k;
}
}
return ;
}
int main(){
memset(a,0x3f,sizeof(a));
memset(f,,sizeof(f));
cin>>n>>m;
for(int i=;i<=n;i++) a[i][i]=;
for(int i=;i<=m;i++){
int x,y,w;
cin>>x>>y>>w;
if(w<a[x][y]){
a[x][y]=a[y][x]=w;
}
}
floy();
if(temk==0x3f3f3f3f)cout<<"No solution."<<endl;
else {for(int i=;i<=cnt;i++) cout<<path[i]<<' ';cout<<endl;}
return ;
}
 
 
 
 
 

poj1734的更多相关文章

  1. 「POJ1734」Sightseeing trip

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

  2. POJ1734 - Sightseeing trip

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

  3. POJ1734/Floyd求最小环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6647   Accepted: 2538 ...

  4. 【POJ1734】Sightseeing Trip 无向图最小环

    题目大意:给定一个 N 个顶点的无向图,边有边权,如果存在,求出该无向图的最小环,即:边权和最小的环,并输出路径. 题解:由于无向图,且节点数较少,考虑 Floyd 算法,在最外层刚开始遍历到第 K ...

  5. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  6. POJ1734无向图求最小环

    题目:http://poj.org/problem?id=1734 方法有点像floyd.若与k直接相连的 i 和 j 在不经过k的情况下已经连通,则有环. 注意区分直接连接和间接连接. * 路径记录 ...

  7. 【poj1734】Sightseeing trip

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

  8. 算法复习——floyd求最小环(poj1734)

    题目: 题目描述 N 个景区,任意两个景区之间有一条或多条双向的路来连接,现在 Mr.Zeng 想找一条旅游路线,这个路线从A点出发并且最后回到 A 点,假设经过的路线为 V1,V2,....VK,V ...

  9. 最小环 hdu1599 poj1734

    最小环用floyd改编. hdu1599特殊一些.要求至少有三个不同的点,并且除了起点与终点重合外,中间不能有环.有点很奇怪,最大值不能为0x3f3f3f3f. poj1374就没那么讲究. //hd ...

  10. Poj1734题解

    题目大意 求一个无向图的最小环 题解 假设是有向图的话.仅仅须要令f[i][i]=+∞,再floyd就可以: 对无向图.应该在floyd算法循环至k的一開始进行例如以下操作: 枚举i和j,假设点i存在 ...

随机推荐

  1. 如何用HAProxy+Nginx实现负载均衡

    一.什么是HAProxy HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点, ...

  2. linux——运维基础,与常用命令

    1 运维概述 1 什么是运维 服务器的运行维护 2 名词 IDC(互联网数据中心) 3 监控软件 zabbix(用的最多), nagios, cactti 4 常用的linux操作系统 1 CentO ...

  3. springboot系列(一) Spring Boot浅谈(转载)

    首先申明一下本文是转载自https://blog.csdn.net/fly_zhyu/article/details/76407830: 1. Spring Boot是什么,解决哪些问题 1) Spr ...

  4. Djang drf:APIView源码分析

    Django REST framework 简介   在序列化与反序列化时,虽然操作的数据不尽相同,但是执行的过程却是相似的,也就是说这部分代码是可以复用简化编写的.        开发REST AP ...

  5. idou老师教你学Istio 28:istio-proxy check 的缓存

    功能概述 istio-proxy主要的功能是连接istio的控制面组件和envoy之间的交互,其中check的功能是将envoy收集的attributes信息上报给mixer,在istio中有几十种a ...

  6. 华中校赛 14th

    https://www.nowcoder.com/acm/contest/106#question A 分类讨论 #include<bits/stdc++.h> using namespa ...

  7. 使用raw input 代替全局键盘钩子

    //关于raw input 请查看msdn https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536%28v=vs.85%29 ...

  8. 内存泄露检测之mtrace

    ————————————————版权声明:本文为CSDN博主「知耻而后勇的蜗牛」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog ...

  9. linux下解决安装jdk后‘环境变量’不生效的问题

    1.是否需要配置环境变量,主要看java -version 显示的版本是否为你期望的版本: (1)不需要配置环境变量的情况 使用java -version查看,版本显示正好是你刚刚安装的版本,这一般为 ...

  10. Pollard-Rho 总结

    将一个大数\(N\)分解质因子. 试除法,暴力枚举\(1-\sqrt{N}\)的数.时间复杂度:\(O(\sqrt{N})\). 通常,这个复杂度够了,但有时,\(N\leq10^{18}\). 这就 ...