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. 接口强制删除namespace 为Terminating的方法

    kubectl get ns qa01 -o json > qa01.json kubectl proxy --port=8081 curl -k -H "Content-Type: ...

  2. zabbix通过钉钉报警

    1.创建报警脚本  vim /usr/local/share/zabbix/alertscripts/dingalert.py #!/usr/bin/env python import json im ...

  3. com.atomikos.datasource.ResourceException: XA resource 'masterDB': resume for XID异常

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/u014172271/article/det ...

  4. python以下划线开头的变量和函数的作用

    在python中,我们经常能看到很多变量名以_下划线开头,而且下划线的数量还不一样,那么这些变量的作用到底是什么? 变量名分类: # 以数字.字母开头: 正常的公有变量名a = 1def aa(): ...

  5. 欧几里得?x

    可以去看dalao博客 orz 1.欧几里得算法 带余除法定理:a,b∈Z,其中b>0,存在唯一q及r,使a=bq+r,其中0<=r<b; 辗转相除法(欧几里得算法)依据:(a,b) ...

  6. Machine Schedule为什么UVA过了POJ过不了

    UVA1194 POJ1325 POJ要多判一个非零!!! #include<cstdio> #include<vector> #include<cstring> ...

  7. luogu2331

    P2331 [SCOI2005]最大子矩阵 题目描述 这里有一个n*m的矩阵,请你选出其中k个子矩阵,使得这个k个子矩阵分值之和最大.注意:选出的k个子矩阵不能相互重叠. 输入格式 第一行为n,m,k ...

  8. 理解了这些异常现象才敢说真正懂了TCP协议

    很多人总觉得学习TCP/IP协议没什么用,觉得日常编程开发只需要知道socket接口怎么用就可以了.如果大家定位过线上问题就会知道,实际上并非如此.如果应用在局域网内,且设备一切正常的情况下可能确实如 ...

  9. Linux设备驱动 之 中断处理程序

    注册中断处理程序 中断处理程序是管理硬件驱动程序的组成部分:如果设备使用中断,那么相应的驱动程序就注册一个中断处理程序: 驱动程序通过request_irq()函数注册,并且激活给定的中断线,以处理中 ...

  10. 写给新手看的 MyBatis 入门

    目录 MyBatis 使用前的准备 什么是 MyBatis 使用Maven 进行 MyBatis 开发环境搭建 MyBatis 入门 项目整体结构一览 MyBatis 的简单生命周期 1.获取 Sql ...