Sightseeing trip
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6647   Accepted: 2538   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

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=105;
const int INF=1e6;//改成1e9就不对了
int dis[MAXN][MAXN],mp[MAXN][MAXN];
int R[MAXN][MAXN],path[MAXN];
int N,M,len;
int u,v,w;
int cnt=0;
void Init()
{
for(int i=0;i<=N;i++)
for(int j=0;j<=N;j++)
dis[i][j]=INF,R[i][j]=0;
cnt=0;
}
void Path(int s,int t)
{
if(R[s][t])
{
Path(s,R[s][t]);
Path(R[s][t],t);
}
else
path[++cnt]=t;
}
void Floyd()
{
len=INF;
for(int k=1;k<=N;k++)
{
    //判断负环
for(int i=1;i<k;i++)
for(int j=i+1;j<k;j++)
{
if(len>dis[i][j]+mp[i][k]+mp[k][j])
{
len=dis[i][j]+mp[i][k]+mp[k][j];
cnt=0;
path[++cnt]=i;
Path(i,j);
path[++cnt]=k; }
}
     //求最短路
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
if(dis[i][j]>dis[i][k]+dis[k][j])
{
dis[i][j]=dis[i][k]+dis[k][j];
R[i][j]=k;
}
}
}
}
int main ()
{
while(~scanf("%d%d",&N,&M))
{
Init();
for(int i=1;i<=M;i++)
{
scanf("%d%d%d",&u,&v,&w);
if(dis[u][v]>w)//记录重边
{
dis[u][v]=w;
dis[v][u]=w;
}
}
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
mp[i][j]=dis[i][j];//mp判断环的时候要用到
Floyd();
if(len==INF)
printf("No solution.\n");
else
{
for(int i=1;i<=cnt;i++)
{
if(i!=cnt)
printf("%d ",path[i]);
else
printf("%d\n",path[i]);
}
}
}
return 0;
}

  

POJ1734/Floyd求最小环的更多相关文章

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

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

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

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

  3. 2017"百度之星"程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  4. Floyd求最小环!(转载,非原创) 附加习题(原创。)HDU-1599

    //Floyd 的 改进写法可以解决最小环问题,时间复杂度依然是 O(n^3),储存结构也是邻接矩阵 int mincircle = infinity; Dist = Graph; ;k<nVe ...

  5. 2018.09.15 hdu1599find the mincost route(floyd求最小环)

    传送门 floyd求最小环的板子题目. 就是枚举两个相邻的点求最小环就行了. 代码: #include<bits/stdc++.h> #define inf 0x3f3f3f3f3f3f ...

  6. 【BZOJ 1027】 (凸包+floyd求最小环)

    [题意] 某公司加工一种由铁.铝.锡组成的合金.他们的工作很简单.首先进口一些铁铝锡合金原材料,不同种类的原材料中铁铝锡的比重不同.然后,将每种原材料取出一定量,经过融解.混合,得到新的合金.新的合金 ...

  7. floyd求最小环 模板

    http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...

  8. CF 1206D - Shortest Cycle Floyd求最小环

    Shortest Cycle 题意 有n(n <= 100000)个数字,两个数字间取&运算结果大于0的话连一条边.问图中的最小环. 思路 可以发现当非0数的个数很大,比如大于200时, ...

  9. 弗洛伊德Floyd求最小环

    模板: #include<bits/stdc++.h> using namespace std; ; const int INF = 0xffffff0; ]; void Solve(in ...

随机推荐

  1. 老oj1965:polygon半平面交

    题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965 polygon半平面交 Time Limit:1000MS ...

  2. Webpack入门——使用Webpack打包Angular项目的一个例子

    2016.1.22,对大多数人来说,这是一个非常平常的日子,但这却是我决定在博客园写博客的日子.虽然注册博客园的博客已有4年8个月,却一直没有动手写过一篇博客,原因是觉得自己水平不行,写不出好东西,所 ...

  3. SQLServer乱码问题的分析及解决方法(中文字符被存入数据库后,显示为乱码)

    注:本文为个人转存,原文地址:http://blog.csdn.net/qiuyu8888/article/details/8021410 问题:SQL版在使用过程中有时会出现乱码,我的症状是中文字符 ...

  4. SSL读书笔记

    摘要: 第一次写博客,为读书笔记,参考书目如下: <HTTP权威指南> <图解HTTP> <大型分布式网站架构设计与实践> 作者:陈康贤 一. HTTP+SSL=H ...

  5. NOIP2012-普及组复赛-第二题-寻宝

    题目描述 Description 传说很遥远的藏宝楼顶层藏着诱人的宝藏.小明历尽千辛万苦终于找到传说中的这个藏宝楼,藏宝楼的门口竖着一个木板,上面写有几个大字:寻宝说明书.说明书的内容如下:藏宝楼共有 ...

  6. web上传大文件的配置

    1.项目本身的webconfig  在<system.web>字段下 <httpRuntime targetFramework="4.5" requestLeng ...

  7. DryIoc mvc 项目集成

    据IOC性能测试排名,DryIoc是目前运行性能最好的依赖注入组件. 详情参考:https://bitbucket.org/dadhi/dryioc/ 下面一步一步搭建我们的DryIoc 的mvc项目 ...

  8. c#设计模式-单例模式(面试题)

    c#设计模式-单例模式 单例模式三种写法: 第一种最简单,但没有考虑线程安全,在多线程时可能会出问题, public class Singleton { private static Singleto ...

  9. ACE_Event_Handler:事件响应入口

    1:ACE_Event_Handler类 头文件“Event_Handler.h” 在ACE Reactor框架中,ACE_Event_Handler是所有事件处理器的基类.ACE_Event_Han ...

  10. xp添加右键"打开文件所在位置"

    以下代码保存为czmb.vbs文件并放在C:\windwos目录下: Set OS = GetObject("winmgmts:\\.\root\cimv2")Set CF = O ...