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. springboot2.0入门(六)-- ymal语法、数据校验

    一.基本使用 1.ymal语法是一种更符合人类命名习惯的语法: # 1. 一个家庭有爸爸.妈妈.孩子. # 2. 这个家庭有一个名字(family-name)叫做“happy family” # 3. ...

  2. the schema version of 'microsoft.aspnet.mvc' is incompatible with version of nuget

    Nuget versioning issue with package restore http://stackoverflow.com/questions/12035976/nuget-versio ...

  3. .pid文件

    pid文件为进程文件,默认的在每个/var/run/目录下生成,当使用systemctl进行进程启动的时候,在这个目录下就会生成相应的pid文件,今天在进行poc测试的时候,对进程执行了enable操 ...

  4. 利用前端三大件(html+css+js)开发一个简单的“todolist”项目

    一.介绍 todolist,即待办事项.在windows android ios上参考微软家出的那个To-Do应用,大概就是那样的.我这个更简单,功能只有“待办” “已完成”两项,并且是在浏览器打开的 ...

  5. maven+SSM+junit+jetty+log4j2环境配置的最佳实践

    思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合 pom中的依赖跟着思路一批一批的 ...

  6. NSString的导出

    字符串的导出,写到某个文件中去 void stringExport(){ NSString *str=@"123456"; //if file not exist will not ...

  7. awk、grep、sed

    awk.grep.sed是linux操作文本的三大利器,也是必须掌握的linux命令之一.三者的功能都是处理文本,但侧重点各不相同,其中属awk功能最强大,但也最复杂.grep更适合单纯的查找或匹配文 ...

  8. Apache Flink - 配置依赖,连接器,库

    每个Flink程序都依赖于一组Flink库. 1.Flink核心和应用程序依赖项 Flink本身由一组类和运行需要的依赖组成.所有类和依赖的组合形成了Flink运行时的核心,并且当一个Flink程序运 ...

  9. RAFT选举算法-分布式数据库困惑

    在做HIS研发工作的时候一直想完善其数据组件,想做一个分布式的数据库支持系统.但一直以来都不清楚这个选举算法应怎么做,原来有一个叫raft的算法https://www.cnblogs.com/just ...

  10. Redis监控之redis-stat安装与详解

    一.安装环境 安装编译环境.ruby运行环境.git代码 yum install gcc-c++ yum -y install ruby-devel yum install ruby yum inst ...