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. hdu_5790_Prefix(trie+主席树)

    题目链接:hdu_5790_Prefix 题意: 给你n个字符串,字符串总长度不超过10W,然后给你一个区间,问你这个区间的字符串不相同的前缀有多少个. 题解: 由于z与上一个答案有关,所以强制在线, ...

  2. hdu_4417_Super Mario(主席树)

    题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他 ...

  3. .Net Core 第三方工具包整理

    本地日志[NLog.Extensions.Logging]:https://github.com/NLog/NLog.Extensions.Logging

  4. vultr机房vps价格20%优惠,赶紧来抢!

    vps服务商vultr全线优惠促销!价格降幅是20%,强烈推荐站长们留意. vps猫腻很多,我写过大量文章介绍vps的优点和几大厂商情况.友情提醒,千万不要采购国内所谓vps云主机,你的数据很不安全哟 ...

  5. 基于Flash ActionScript 实现RTMP发布与播放媒本流

    1  为什么要采用Flash ActionScript实现RTMP协议发布或播放媒体流,播放媒体流,协议可控,比如对流媒体数加密,混音等. 2 核心思路使用Flash Socket建立TCP二进制传输 ...

  6. 获取Camera 支持视频的尺寸

    <uses-permission android:name="android.permission.CAMERA" > </uses-permission> ...

  7. Hibernate中自带ID的generator的含义

    increment:代理主键,适合于所有数据库,由hibernate维护主键自增,和底层数据库无关,但是不适合于2个或以上hibernate进程. identity:代理主键,适合于Mysql或ms ...

  8. JMeter基础

    转载自虫师-http://www.cnblogs.com/fnng/archive/2012/12/21/2828440.html JMeter 介绍: 一个非常优秀的开源的性能测试工具. 优点:你用 ...

  9. Flask -- 内容管理系统

    例子: # content_manager.py # 把TOPIC存在一个字典里,key为关键字,value为二维数组# TOPIC_DICT['Django'][0]为Title,TOPIC_DIC ...

  10. 照着例子学习protobuf-python

    以下是照着python操作protobuf进行的protobuf-python的学习笔记: 首先是protobuf的下载与安装: 1 由于google被墙,所以去github上面搜索了一下protob ...