POJ 1734:Sightseeing trip
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的更多相关文章
- 【POJ 1734】 Sightseeing Trip
[题目链接] 点击打开链接 [算法] floyd求最小环 输出路径的方法如下,对于i到j的最短路,我们记pre[i][j]表示j的上一步 在进行松弛操作的时候更新pre即可 [代码] #include ...
- POJ 3301:Texas Trip(计算几何+三分)
http://poj.org/problem?id=3301 题意:在二维平面上有n个点,每个点有一个坐标,问需要的正方形最小面积是多少可以覆盖所有的点. 思路:从第二个样例可以看出,将正方形旋转45 ...
- POJ 3621:Sightseeing Cows(最优比率环)
http://poj.org/problem?id=3621 题意:有n个点m条有向边,每个点有一个点权val[i],边有边权w(i, j).找一个环使得Σ(val) / Σ(w)最大,并输出. 思路 ...
- poj 1734 Sightseeing trip判断最短长度的环
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5590 Accepted: 2151 ...
- Sightseeing trip POJ - 1734 -Floyd 最小环
POJ - 1734 思路 : Floyd 实质 dp ,优化掉了第三维. dp [ i ] [ j ] [ k ] 指的是前k个点优化后 i -> j 的最短路. 所以我们就可以 ...
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd
题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...
- 【poj1734】Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8520 Accepted: 3200 ...
- Ural 1004 Sightseeing Trip
Sightseeing Trip Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
随机推荐
- python自动华 (十一)
Python自动化 [第十一篇]:Python进阶-RabbitMQ队列/Memcached/Redis 本节内容: RabbitMQ队列 Memcached Redis 1. RabbitMQ ...
- Shiro (包含权限满足其中一个就通过的用法)
方法/步骤 1 web.xml添加配置 <!-- shiro过滤器 --> <filter> <filter-name>shiroFilter</filter ...
- 016_编写脚本快速克隆 KVM 虚拟机
#!/bin/bash#本脚本针对 RHEL7.2 或 Centos7.2#本脚本需要提前准备一个 qcow2 格式的虚拟机模板,名称为/var/lib/libvirt/images/.rh7_tem ...
- seq2seq聊天模型(二)——Scheduled Sampling
使用典型seq2seq模型,得到的结果欠佳,怎么解决 结果欠佳原因在这里 在训练阶段的decoder,是将目标样本["吃","兰州","拉面" ...
- P2663 越越的组队
原题链接 https://www.luogu.org/problem/P2663 很容易看出来是个背包问题嘛: 体积是总分的一半,求最高分,每个同学选或不选,是个 01背包问题. 自信地交上去之后发 ...
- TCP_IP Sockets编程C语言实现第2版 源码下载
原书网址:http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/textcode.html 源码下载: 链接:https://pan.baidu. ...
- servlet 中session的使用方法(创建,使用)
创建: protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, ...
- Python语法 - 生成器
生成器基本概念 1 生成器不会把结果保存在一个系列中,而是保存生成器的状态,在每次进行迭代时返回一个值,直到遇到StopIteration异常结束 2 生成器表达式能做的事情列表解析基本都能处理,只不 ...
- 【原创】FltSendMessage蓝屏分析
INVALID_PROCESS_DETACH_ATTEMPT (6)Arguments:Arg1: 00000000Arg2: 00000000Arg3: 00000000Arg4: 00000000 ...
- fiddler 基本知识(一)
工作原理: fiddler,原理是在本机开启了一个http代理服务器,默认监听127.0.0.1:8888. 打开fiddler之后,会帮我们设置ie代理,虽然只设置了ie,但整个计算机都通过整个代理 ...