Sightseeing trip
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: Accepted: 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 to N and M two-way roads numbered from 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>. The road y_i (<=i<=k-) connects crossing points x_i and x_{i+}, 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 (<=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<= and the number of roads M<=. Each of the next M lines describes one road. It contains 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 ).
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 Sample Output

题目

  (PS:其实就是求图上最小环啦)

  芒果君:本来以为自己最短路学的可以来着,结果知道最小环用floyd而不用tarjan时我的内心是崩溃的,然后也打不出来。这道题的巧妙之处在于,求环的过程和floyd一块做而在其之前,使得不会在结果中出现重复节点。最短路无非是加了一句记录中转点;求环的话每次都要重做,首先要清楚它不只是一条最短路,还有一个不在路上的点k将其首尾相连,先记录i,再进行递归找到最短路上更新的所有点,这段代码需要仔细理解。

  感觉这道题不太好想呢?

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define inf 1<<29
using namespace std;
int ans,dis[][],road[][],ma[][],path[],n,m,cnt;
int read()
{
int x=;
char ch=getchar();
while(ch<''||ch>'') ch=getchar();
while(ch>=''&&ch<=''){
x=x*+ch-'';
ch=getchar();
}
return x;
}
void init()
{
ans=inf;
memset(road,,sizeof(road));
memset(ma,,sizeof(ma));
for(int i=;i<=n;++i)
for(int j=;j<=n;++j)
dis[i][j]=inf;
}
void record(int x,int y)
{
if(road[x][y]){
record(x,road[x][y]);
record(road[x][y],y);
}
else path[++cnt]=y;
}
int main(){
int x,y,t,i,j,k;
while((scanf("%d%d",&n,&m))!=EOF){
init();
for(i=;i<=m;++i){
x=read();y=read();t=read();
if(t<dis[x][y]) dis[x][y]=dis[y][x]=t;
}
for(i=;i<=n;++i)
for(j=;j<=n;++j)
ma[i][j]=dis[i][j];
for(k=;k<=n;++k){
for(i=;i<k;++i)
for(j=i+;j<k;++j){
if(ans>dis[i][j]+ma[i][k]+ma[k][j]){
ans=dis[i][j]+ma[i][k]+ma[k][j];
cnt=;
path[++cnt]=i;
record(i,j);
path[++cnt]=k;
}
}
for(i=;i<=n;++i)
for(j=;j<=n;++j)
if(dis[i][j]>dis[i][k]+dis[k][j]){
dis[i][j]=dis[i][k]+dis[k][j];
road[i][j]=k;
}
}
if(ans==inf) printf("No solution.\n");
else{
for(i=;i<=cnt;i++) printf("%d ",path[i]);
printf("\n");
}
}
return ;
}

POJ 1734:Sightseeing trip的更多相关文章

  1. 【POJ 1734】 Sightseeing Trip

    [题目链接] 点击打开链接 [算法] floyd求最小环 输出路径的方法如下,对于i到j的最短路,我们记pre[i][j]表示j的上一步 在进行松弛操作的时候更新pre即可 [代码] #include ...

  2. POJ 3301:Texas Trip(计算几何+三分)

    http://poj.org/problem?id=3301 题意:在二维平面上有n个点,每个点有一个坐标,问需要的正方形最小面积是多少可以覆盖所有的点. 思路:从第二个样例可以看出,将正方形旋转45 ...

  3. POJ 3621:Sightseeing Cows(最优比率环)

    http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...

  4. poj 1734 Sightseeing trip判断最短长度的环

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5590   Accepted: 2151 ...

  5. Sightseeing trip POJ - 1734 -Floyd 最小环

    POJ - 1734 思路 : Floyd 实质 dp ,优化掉了第三维. dp [ i ] [ j ] [ k ] 指的是前k个点优化后    i  ->  j   的最短路. 所以我们就可以 ...

  6. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

  7. 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd

    题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...

  8. 【poj1734】Sightseeing trip

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8520   Accepted: 3200 ...

  9. Ural 1004 Sightseeing Trip

    Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...

随机推荐

  1. 顺序表Vector

    程序中会使用数据结构:例如:顺序表.链表.二叉树: 数据结构在底层中本质上只有两种:数据之间挨着和不挨着:   1.关于Vector

  2. VS2012 Update 2: 0x80040154 corrupt install when starting the debugger

    使用VS2012開發console program ,发现生成32位的exe文件在別的机上不能正确运行,有文章說update1可以解決這個問題,如下 Setup.exe is not a valid ...

  3. BZOJ 4025 二分图 LCT维护最大生成树

    怎么说呢,我也不知道该咋讲,你就手画一下然后 yy 一下就发现这么做是对的. 为什么我明明都想出来了,却还是讲不出来啊~ #include <cstdio> #include <ve ...

  4. 迭代加深 A* IDA* 初探

    并没有有用的东西, 只是用来水的. 今天看搜索,想起来了A*和IDA* 看A*去了.... 啥玩意啊这是,他们代码为啥这么长??.... 看完了,...代码怎么写啊?? .....算了,直接看题吧 找 ...

  5. python基础-垃圾回收机制

    垃圾回收 Python中的垃圾回收是以引用计数为主,分代收集为辅.引用计数的缺陷是循环引用的问题. 引用计数 原理:当一个对象的引用被创建或者复制时,对象的引用计数加1:当一个对象的引用被销毁时,对象 ...

  6. Friends (ZOJ - 3710)

    Problem Alice lives in the country where people like to make friends. The friendship is bidirectiona ...

  7. windows游戏编程地址

    本系列文章由jadeshu编写,转载请注明出处.http://blog.csdn.net/jadeshu/article/details/22309325 作者:jadeshu   邮箱: jades ...

  8. pandas入门之Series

    一.创建Series 参数 - Series (Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组.轴标签统称为索引. - data 参数 - index ...

  9. Apache Web服务器 安装步骤 和遇到的坑

    Apache Web服务器是开发放源码的网页服务器,我们看到的网页都是上传到服务器然后呈现给用户的. 在开发中,在自己的电脑上安装Apache Web服务器,你的电脑也会成为服务器,配置文件,访问你的 ...

  10. 检测ip代理有效性

    转载及总结 转载:https://xw.qq.com/amphtml/20190428A05ZS200 1.telnet 方法 经过测试,会看到存在以下问题: 即使一些代理商能够用telnet测试过关 ...