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. 3.Java集合-HashSet实现原理及源码分析

    一.HashSet概述: HashSet实现Set接口,由哈希表(实际上是一个HashMap实例)支持,它不保证set的迭代顺序很久不变.此类允许使用null元素 二.HashSet的实现: 对于Ha ...

  2. Django hrf:权限、频率控制

    一.权限 二.频率控制 一.权限 1.权限介绍 只有超级用户才能访问指定的数据,所以就要用权限组件进行设置 2.局部使用 # 单独抽出写一个视图类 from rest_framework.permis ...

  3. PAT Advanced 1071 Speech Patterns (25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  4. java中的io流总结(一)

    知识点:基于抽象基类字节流(InputStream和OutputStream).字符流(Reader和Writer)的特性,处理纯文本文件,优先考虑使用字符流BufferedReader/Buffer ...

  5. 一个基于TCP/IP的小项目,实现广播消息的功能。(超详细版)

    1.结合现状 功能分析 该功能基于上个项目的改进,主要是通过对服务器端代码的修改,以及对客户端作少许修改,实现开启多客户端时,一个客户端发送消息,达到对所有客户端广播的效果.可参考网吧里的点歌系统,比 ...

  6. Why Go? – Key advantages you may have overlooked

    Why Go? – Key advantages you may have overlooked yourbasic.org/golang Go makes it easier (than Java ...

  7. python学习之文件读写,序列化(json,pickle,shelve)

    python基础 文件读写 凡是读写文件,所有格式类型都是字符串形式传输 只读模式(默认) r  f=open('a.txt','r')#文件不存在会报错 print(f.read())#获取到文件所 ...

  8. 如何解决web大流量,高并发问题

    对于当今大流量的网站,每天几千万甚至上亿的流量,是如何解决访问量问题的呢? 以下是一些总结的方法:  第一,确认服务器硬件是否足够支持当前的流量.  普通的P4服务器一般最多能支持每天10万独立IP, ...

  9. tomcat——Server.xml

    本机tomcat位置:D:\tomcat7\apache-tomcat-7.0.61 server.xml 位置:D:\tomcat7\apache-tomcat-7.0.61\conf 注意:ser ...

  10. SIGAI机器学习第十四集 支持向量机1

    讲授线性分类器,分类间隔,线性可分的支持向量机原问题与对偶问题,线性不可分的支持向量机原问题与对偶问题,核映射与核函数,多分类问题,libsvm的使用,实际应用 大纲: 支持向量机简介线性分类器分类间 ...