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. ACM第一天研究懂的AC代码——BFS问题解答——习题zoj2165

    代码参考网址:http://blog.csdn.net/slience_perseverance/article/details/6706354 试题分析: 本题是研究red and black的一个 ...

  2. angularjs表单

    <form ng-app="myApp" ng-controller="validateCtrl" name="myForm" nov ...

  3. 神经网络工具箱nntool的使用方法

    关于如何使用nntool神经网络工具箱进行“数据训练”的方法: 1. 在命令窗口键入nntool命令打开神经网络工具箱: 2. 点击Import按钮两次,分别把输入向量和目标输出加入到对应的窗口([I ...

  4. 1.Perl 多线程:Threads

    详情可查看: perldoc threads 调用线程的方法: $thr = threads->create(FUNCTION, ARGS) #This will create a new th ...

  5. 二十二、oracle pl/sql分类二 函数

    函数用于返回特定的数据,当建立函数时,在函数头部必须包含return子句.而在函数体内必须包含return语句返回的数据.我们可以使用create function来建立函数. 1).接下来通过一个案 ...

  6. FTP、TFTP

      FTP 文件传送协议  (File Transfer Protocol) FTP是因特网上使用得最广泛的文件传送协议. 文件传送协议 FTP (File Transfer Protocol) 是因 ...

  7. A. Night at the Museum Round#376 (Div. 2)

    A. Night at the Museum time limit per test 1 second memory limit per test 256 megabytes input standa ...

  8. InvalidateRect只是增加重绘区域,在下次WM_PAINT的时候才生效

    emWIN里面的无效重绘和windows很类似. WM_InvalidateArea()和WM_InvalidateRect()只重绘指定的区域,其他区域不会重绘,这样避免了闪烁,重绘发生在下次WM_ ...

  9. C# 16位的GUDI

    引用:  http://www.cnblogs.com/lcwzj/archive/2009/04/16/1436992.html 当我们想要获得一个唯一的key的时候,通常会想到GUID.这个key ...

  10. android命令行网络时间同步

    一.简介 Android基于Linux平台的开源手机操作系统. 二.原理 既然是Linux,那就应该支持linux的各种命令行,高度的可配置,但实验发现Android是Google的一个高度阉割版的l ...