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. 6、组件注册-@Lazy-bean懒加载

    6.组件注册-@Lazy-bean懒加载 懒加载:单实例bean,默认是在容器启动的时候创建对象:懒加载就是启动的是不创建,在第一次使用的时候再创建对象. @Lazy // 单实例下懒加载bean

  2. python自动华 (七)

    Python自动化 [第七篇]:Python基础-面向对象高级语法.异常处理.Scoket开发基础 本节内容: 1.     面向对象高级语法部分 1.1   静态方法.类方法.属性方法 1.2   ...

  3. xunit输出

    //输出,只能注入 public class MyUnitTest { private IServiceCollection service; private readonly ITestOutput ...

  4. npm源管理

    1. 安装淘宝镜像 为了提高npm的安装速度,可以使用淘宝镜像. 使用淘宝镜像的方法有两种: 1. npm install -g cnpm --registry=https://registry.np ...

  5. java+批量下载文件到指定文件夹

    需求 导出文件后存留在了服务器中,需要提供下载按钮,点击后可下载到本地:(因为涉及多个文件,下载前先将文件进行压缩,提供下载压缩文件) 效果预览 代码 主要方法 /**     * 下载生成的所有在线 ...

  6. learning express step(十一)

    learning express.Router() code: const express = require('express'); const app = express(); var route ...

  7. [ZJOI2009]假期的宿舍 二分图匹配匈牙利

    [ZJOI2009]假期的宿舍 二分图匹配匈牙利 一个人对应一张床,每个人对床可能不止一种选择,可以猜出是二分图匹配. 床只能由本校的学生提供,而需要床的有住校并且本校和外校两种人.最后统计二分图匹配 ...

  8. CentOS6.8安装Docker

    在CentOS6.8上安装Docker 1.Docker使用EPEL发布,RHEL系的OS首先要确保已经持有EPEL仓库,否则先检查OS的版本,然后安装相应的EOEL包:如下命令: yum insta ...

  9. js 移除数组中的内容

    使用方法:arr.splice(arr.indexOf(ele),length):表示先获取这个数组中这个元素的下标,然后从这个下标开始计算,删除长度为length的元素 这种删除方式适用于任何js数 ...

  10. 重读APUE(7)-link/unlink与mkdir/rmdir

    link–用于创建一个现有文件的链接:实际上是新建一个目录项,指向当前文件的i节点: unlink–用于删除一个现有文件的连接:实际上是对引用i节点的目录项进行删除,并且对链接计数-1:系统会检查文件 ...