POJ1734 - Sightseeing trip
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 1 to N and M two-way roads numbered from 1 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>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, 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 (1<=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<=100 and the number of roads M<=10000. Each of the next M lines describes one road. It contains 3 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 500).
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
5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
Sample Output
1 3 5 2
题目大意:给你一个无向图,要你求最小环,并输出路径
用floyd求最短路时顺便求最小环
floyd主程序
for k:= to n do
for i:= to n do
for j:= to n do
f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);
然后我们可以在里面加一点东西
for k:= to n do
begin
for i:= to k- do
for j:= to i- do
minc:=min(minc,f[i,j]+g[i,k]+g[k,j]);
for i:= to n do
for j:= to n do
f[i,j]:=min(f[i,j],f[i,k]+f[k,j]);
end;
g存的是原图信息
因为当k枚举到a时,最短路除了两端点外,都只能经过编号小于a的点
在最小环中,一定有一个编号最大的点,而且只有一个(废话......)
设这个点编号为b,当k枚举到b时,i,j枚举到b在环上相邻的两点时,f[i,j]存的是i,j之间不通过大于b的点的最短路,这当然就是最小环了
const
maxn=;
var
f,g,p:array[..maxn,..maxn]of longint;
path:array[..maxn]of longint;
ans,tot,n,m:longint; procedure init;
var
i,x,y,z:longint;
begin
read(n,m);
fillchar(g,sizeof(g),);
for i:= to m do
begin
read(x,y,z);
if g[x,y]>z then
begin
g[x,y]:=z;
g[y,x]:=z;
end;
end;
f:=g;
end; procedure get(i,j:longint);
begin
if p[i,j]<> then
begin
get(i,p[i,j]);
get(p[i,j],j);
exit;
end;
inc(tot);
path[tot]:=j;
end; procedure work;
var
i,j,k:longint;
begin
ans:=g[,];
for k:= to n do
begin
for i:= to k- do
for j:= to i- do
if ans>f[i,j]+g[i,k]+g[k,j] then
begin
ans:=f[i,j]+g[i,k]+g[k,j];
tot:=;
inc(tot);
path[tot]:=i;
get(i,j);
inc(tot);
path[tot]:=k;
end;
for i:= to n do
for j:= to n do
if f[i,j]>f[i,k]+f[k,j] then
begin
p[i,j]:=k;
f[i,j]:=f[i,k]+f[k,j];
end;
end;
if ans=g[,] then write('No solution.')
else
for i:= to tot do
write(path[i],' ');
end; begin
init;
work;
end.
POJ1734 - Sightseeing trip的更多相关文章
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- poj1734 Sightseeing trip(Floyd求无向图最小环)
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> ...
- poj1734 Sightseeing trip[最小环]
一个最小环裸题.最小环的两种求法dijkstra和Floyd直接参见这里我就是从这里学的,不想写了. 注意这里最重要的一个点是利用了Floyd的dp过程中路径上点不超过$k$这一性质,来枚举环上最大编 ...
- POJ1734 Sightseeing trip (Floyd求最小环)
学习了一下用Floyd求最小环,思路还是比较清晰的. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring ...
- 「POJ1734」Sightseeing trip
「POJ1734」Sightseeing trip 传送门 这题就是要我们求一个最小环并且按顺序输出一组解. 考虑 \(O(n^3)\) 地用 \(\text{Floyd}\) 求最小环: 考虑 \( ...
- 【poj1734】Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8520 Accepted: 3200 ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- 「LOJ#10072」「一本通 3.2 例 1」Sightseeing Trip(无向图最小环问题)(Floyd
题目描述 原题来自:CEOI 1999 给定一张无向图,求图中一个至少包含 333 个点的环,环上的节点不重复,并且环上的边的长度之和最小.该问题称为无向图的最小环问题.在本题中,你需要输出最小环的方 ...
- poj 1734 Sightseeing trip判断最短长度的环
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5590 Accepted: 2151 ...
随机推荐
- BZOJ1975 [Sdoi2010]魔法猪学院
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Linux命令(4):cat命令
1.作用:连接并显示指定的一个和多个文件的有关信息: 2.格式:cat [选项] 文件1 文件2 ... 其中文件1.文件2为要显示的多个文件: 3.常见参数: 4.使用实例: [youname@ww ...
- Cocos2d-x开发中Ref内存管理
Ref类是Cocos2d-x根类,Cocos2d-x中的很多类都派生自它,例如,我们熟悉的节点类Node也派生自Ref.我们介绍Ref内存管理.内存引用计数Ref类设计来源于Cocos2d-iphon ...
- (转)MSSQL 各个发行版本版本号以及Compact 版本号
终于开始写博客了. 不要笑啊. 下面是MSSQL 的发行版本以及版本号.自己整理的. http://support.microsoft.com/kb/321185/zh-cn SQL Server 2 ...
- css Hack 以及css的一些兼容问题小结
坚持每天做总结.今天下班还算早.写个跟css兼容有关的知识点.便于后期查看与学习.一.先说说各种主流浏览器的内核 浏览器最重要或者说核心的部分是“Rendering Engine”,可大概译为“渲染引 ...
- javascript 基础API
Math.random() 取值范围[0,1) 大于等于0小于1,包括0,不包括1 Math.floor() 向下取整 Math.ceil() 向上取整 第一题:一组数的规则如下:1.1.2.3. ...
- [CSS]三角形
CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent:
- memcached 使用积累
1.memcahed在windows上的安装 . 下载memcache的windows稳定版,解压放某个盘下面,比如在c:\memcached . 在终端(也即cmd命令界面)下输入 ‘c:\memc ...
- js-shortid:优雅简洁地实现短ID
短ID在实际运用中很广泛, 其中比较典型的运用就是短地址. 市面上肯定有不少开源的生成短ID库, 基于node.js的估计也不少. 鉴于本人已然是node.js的脑残粉(本职java开发), 很多业余 ...
- 布局—column(属性)
column-width:||用来定义多列中每列的宽度,用像素表示column-count:||用来定义多列中的列数,用数字来表示1-10columns: 200px 2||列宽和列数column-g ...