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. leetcode-63. Unique Paths II · DP + vector

    题面 A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...

  2. eclipse开发scrapy爬虫工程,附爬虫临门级教程

    写在前面 自学爬虫入门之后感觉应该将自己的学习过程整理一下,也为了留个纪念吧. scrapy环境的配置还请自行百度,其实也不难(仅针对windows系统,centos配置了两天,直到现在都没整明白) ...

  3. Tornado目录

    第一篇:白话tornado源码之一个脚本引发的血案 第二篇:白话tornado源码之待请求阶段 第三篇:白话tornado源码之请求来了 第四篇:白话tornado源码之褪去模板外衣的前戏 第五篇:白 ...

  4. 《浏览器工作原理与实践》<08>调用栈:为什么JavaScript代码会出现栈溢出?

    在上篇文章中,我们讲到了,当一段代码被执行时,JavaScript 引擎先会对其进行编译,并创建执行上下文.但是并没有明确说明到底什么样的代码才算符合规范. 那么接下来我们就来明确下,哪些情况下代码才 ...

  5. C++——数组形参退化为指针

    数组做形参退化为指针 如果数组作为函数参数,则数组形参会退化为指针,以下代码在编译器看来是等价的 ]); ]); void fun3(int a[]); void fun4(int *a); #inc ...

  6. Mongo db 简单介绍及命令笔记

    首先来了解下什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为W ...

  7. aiops常用算法

    1.数据聚合/关联技术 概念聚类算法AOI分类算法K近邻/贝叶斯分类器/logistic回归(LR)/支持向量机(SVM)/随机森林(RF) 2.数据异常点检测技术独立森林算法 3.故障诊断和分析策略 ...

  8. [转]Linux 桌面玩家指南:20. 把 Linux 系统装入 U 盘打包带走

    原文:https://www.cnblogs.com/youxia/p/LinuxDesktop020.html ------------------------------------------- ...

  9. 前端笔记-html

    前端笔记html 前端三大利器,html(本源),css(着装),js(动作) html 学习html就是学习一套规则能够被浏览器识别,在页面中展示,一个页面只能运行一个html 标签 <> ...

  10. mybatis配置和映射文件

    配置: <?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configurationPUBLIC ...