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

题意:给出你一个无向图,要你求一个最小环,最小环的最少有3个节点,如果有环则顺序输出节点上的点,如果没有环,输出NO solution.
分析:其实用dfs就可以AC了,但是我发现其他dalao都用floyd做,学习了一下。
     经过分析可以得到,包含i和j的最小环其实就是i到j的最短路和i到j的次短路组成的,至于为什么,很好想,因为组成了一个环,那么从i到j就有两条不同的路可以走,而要求最小环,所以就是i到j的最短路和i到j的次短路组成的。
     其实我们就是要求一点不在i到j的最短路上的k,那么最小环=min{d[i][j] + a[i][k] + a[k][j]},根据floyd算法,我们先枚举k,然后枚举i和j,不超过k即可(因为k不在i到j的最短路上),然后记录一下路径即可.路径记录可以递归,也可以就用数组记录前驱。
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm> #define inf 0x7ffffff using namespace std; int n, m,a[][],d[][],head[][],res,tot,ans[]; int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
{
a[i][j] = d[i][j] = inf;
head[i][j] = i;
}
for (int i = ; i <= m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[x][y] = a[y][x] = d[x][y] = d[y][x] = min(z, a[x][y]);
}
res = inf;
for (int k = ; k <= n; k++)
{
for (int i = ; i < k; i++)
{
for (int j = i + ; j < k; j++)
{
int temp = d[i][j] + a[i][k] + a[k][j];
if (temp < res)
{
res = temp;
tot = ;
int p = j;
while (p != i)
{
ans[++tot] = p;
p = head[i][p];
}
ans[++tot] = i;
ans[++tot] = 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];
head[i][j] = head[k][j];
}
}
if (res == inf)
puts("No solution.\n");
else
{
printf("%d", ans[]);
for (int i = ; i <= tot; i++)
printf(" %d", ans[i]);
printf("\n");
}
} return ;
}

poj1734Sightseeing trip的更多相关文章

  1. 2018.09.15 poj1734Sightseeing trip(floyd求最小环)

    跟hdu1599差不多.. 只是需要输出方案. 这个可以递归求解. 代码: #include<iostream> #include<cstdio> #include<cs ...

  2. poj1734Sightseeing trip——无向图求最小环

    题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...

  3. Lesson 4 An existing trip

    Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...

  4. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  5. 【poj1041】 John's trip

    http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...

  6. 1301. The Trip

    A number of students are members of a club that travels annually to exotic locations. Their destinat ...

  7. 三分 --- POJ 3301 Texas Trip

    Texas Trip Problem's Link:   http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...

  8. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  9. hdu 3018 Ant Trip 欧拉回路+并查集

    Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem ...

随机推荐

  1. Robot Framework(一)入门

    1.1简介 Robot Framework是一个基于Python的,可扩展的关键字驱动的测试自动化框架,用于端到端验收测试和验收测试驱动开发(ATDD).它可用于测试分布式异构应用程序,其中验证需要涉 ...

  2. 2018.4.15 Mac系统下如何使用StartUml画好需求分析的类图 (同样适用于windows)

    Mac如何使用StartUml (同样适用于windows) 左侧边栏的英文含义及其用法 关联(Association) [关联关系]:是一种拥有的关系,它使一个类知道另一个类的属性和方法:如:老师与 ...

  3. Bootstrap历练实例:激活导航状态

    激活导航状态 您可以在激活状态的胶囊式导航和列表导航中放置徽章.通过使用 <span class="badge"> 来激活链接,如下面的实例所示: <!DOCTY ...

  4. UI Testing in Xcode 7

    参考文章: UI Testing in Xcode - WWDC 2015https://developer.apple.com/videos/play/wwdc2015-406/ Document ...

  5. React组件自适应窗口宽高

    很多时候我们需要组件能够根据窗口变化改变宽高,有时候可以使用css,有时候需要随数据调整则使用js计算. 比如说,当我们在页面中放置一个iframe时,我们希望它的宽高随着其父元素or窗口的变化而变化 ...

  6. 【转】浅谈对主成分分析(PCA)算法的理解

    以前对PCA算法有过一段时间的研究,但没整理成文章,最近项目又打算用到PCA算法,故趁热打铁整理下PCA算法的知识.本文观点旨在抛砖引玉,不是权威,更不能尽信,只是本人的一点体会. 主成分分析(PCA ...

  7. NOIP模拟赛 双色球

    [题目描述] 机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233 “来来来,学弟,我考你道水题检验一下你的水平……” 一个栈内初始有n个红色和蓝色的 ...

  8. Service Mesh是什么技术

    https://blog.csdn.net/weixin_38044696/article/details/80257488 Service Mesh是什么技术 2018年05月09日 22:07:4 ...

  9. 多线程辅助类之CyclicBarrier(四)

    CyclicBarrier是一个线程辅助类,和<多线程辅助类之CountDownLatch(三)>功能类似,都可以实现一组线程的相互等待.要说不通点,那就是CyclicBarrier在释放 ...

  10. graph-basic

    打算使用STL中的vector,通过邻接链表的方式存储图.这里贴基本定义,以及depth-first-search和breadth-first-search的实现代码. 其他图的算法实现,就贴在各自的 ...