URAL1004 Sightseeing Trip(Folyd求最小环,打印路径)

2023-10-06 00:01

本文主要是介绍URAL1004 Sightseeing Trip(Folyd求最小环,打印路径),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

1004. Sightseeing Trip

Time limit: 0.5 second
Memory limit: 64 MB
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, …,  ykk > 2. The road  yi  (1 ≤  i ≤  k − 1) connects crossing points xi and  x i+1, the road  yk connects crossing points  xk and  x 1. All the numbers  x 1, …,  xk 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( yk) where L( yi) is the length of the road  yi (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

Input contains  T tests (1 ≤  T ≤ 5). The first line of each test contains two integers: the number of crossing points  N and the number of roads  M (3 ≤  N ≤ 100; 3 ≤  M ≤  N · ( N − 1)). Each of the next  M lines describes one road. It contains 3 integers: the number of its first crossing point  a, the number of the second one  b, and the length of the road  l (1 ≤  ab ≤  Na ≠  b; 1 ≤  l ≤ 300 ). Input is ended with a “−1” line.

Output

Each line of output is an answer. 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  xk 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 output
5 7
1 4 1
1 3 300
3 1 10
1 2 16
2 3 100
2 5 15
5 3 20
4 3
1 2 10
1 3 20
1 4 30
-1
1 3 5 2
No solution.
Problem Source: Central European Olympiad in Informatics 1999


思路:

题意给了一张无向图,求的是这个图的最小环(就是从某个点出发再走回它自己的最短路),并且把路径打印出来。

所以我们求出一个最短路+次短路的和就是所求的路径.我们以环权值最小为标准(最短路权值+次短路权值),记录从i~j的路径以及能松弛i和j点的k中代价最小的那个,一直更新路径,如果遇见更短的路径那么就放弃之前记录的重新记录.floyd是按照结点的顺序更新最短路的,所以我们在更新最短路之前先找到一个连接点k,当前的点k肯定不存在于已存在的最短路dis[i][j]的路径上,因为我们还没用这个k去更新最短路,这样一个环就找到了


map[]来存储原图

dis[]来存储松弛后的图

path[]记录路径

vis[][]标记两个点之间经过哪个点中转过

ans来存储当前最小的环的值,如果遇到更小的,那么就重新记录路径

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define N 110
#define inf 9999999
#define ll long long
using namespace std;
int n,m,ans,cnt;
int map[N][N],dis[N][N];
int vis[N][N],path[N];
void init()
{for(int i=1; i<=n; i++)for(int j=1; j<=n; j++){dis[i][j]=inf;vis[i][j]=0;}
}
void record(int i,int j)//递归记录路径
{if(vis[i][j]){record(i,vis[i][j]);record(vis[i][j],j);}elsepath[cnt++]=j;
}
void floyd()
{ans=inf;for(int k=1; k<=n; k++){//找出最小环并且记录路径for(int i=1; i<k; i++)for(int j=i+1; j<k; j++)if(ans>dis[i][j]+map[i][k]+map[k][j])//如果最短路加上次短路权值可以成环,就记录{ans=dis[i][j]+map[i][k]+map[k][j];cnt=0;path[cnt++]=i;record(i,j);//回溯记录路径path[cnt++]=k;}//正常的folydfor(int i=1; i<=n; i++)for(int j=1; j<=n; j++)if(dis[i][j]>dis[i][k]+dis[k][j]){dis[i][j]=dis[i][k]+dis[k][j];vis[i][j]=k;}}
}
int main()
{int u,v,w;while(~scanf("%d",&n)&&n!=-1){scanf("%d",&m);init();for(int i=1; i<=m; i++){scanf("%d%d%d",&u,&v,&w);if(w<dis[u][v])dis[u][v]=dis[v][u]=w;}for(int i=1; i<=n; i++)for(int j=1; j<=n; j++)map[i][j]=dis[i][j];floyd();if(ans==inf)puts("No solution.");else{printf("%d",path[0]);for(int i=1; i<cnt; i++)printf(" %d",path[i]);puts("");}}return 0;
}



这篇关于URAL1004 Sightseeing Trip(Folyd求最小环,打印路径)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/152342

相关文章

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

Java实现预览与打印功能详解

《Java实现预览与打印功能详解》在Java中,打印功能主要依赖java.awt.print包,该包提供了与打印相关的一些关键类,比如PrinterJob和PageFormat,它们构成... 目录Java 打印系统概述打印预览与设置使用 PageFormat 和 PrinterJob 类设置页面格式与纸张

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

VSCode设置python SDK路径的实现步骤

《VSCode设置pythonSDK路径的实现步骤》本文主要介绍了VSCode设置pythonSDK路径的实现步骤,包括命令面板切换、settings.json配置、环境变量及虚拟环境处理,具有一定... 目录一、通过命令面板快速切换(推荐方法)二、通过 settings.json 配置(项目级/全局)三、

使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)

《使用Python和Matplotlib实现可视化字体轮廓(从路径数据到矢量图形)》字体设计和矢量图形处理是编程中一个有趣且实用的领域,通过Python的matplotlib库,我们可以轻松将字体轮廓... 目录背景知识字体轮廓的表示实现步骤1. 安装依赖库2. 准备数据3. 解析路径指令4. 绘制图形关键

如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)

《如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)》:本文主要介绍如何更改pycharm缓存路径和虚拟内存分页文件位置(c盘爆红)问题,具有很好的参考价值,希望对大家有所帮助,如有... 目录先在你打算存放的地方建四个文件夹更改这四个路径就可以修改默认虚拟内存分页js文件的位置接下来从高级-

一文详解如何查看本地MySQL的安装路径

《一文详解如何查看本地MySQL的安装路径》本地安装MySQL对于初学者或者开发人员来说是一项基础技能,但在安装过程中可能会遇到各种问题,:本文主要介绍如何查看本地MySQL安装路径的相关资料,需... 目录1. 如何查看本地mysql的安装路径1.1. 方法1:通过查询本地服务1.2. 方法2:通过MyS

Python如何调用指定路径的模块

《Python如何调用指定路径的模块》要在Python中调用指定路径的模块,可以使用sys.path.append,importlib.util.spec_from_file_location和exe... 目录一、sys.path.append() 方法1. 方法简介2. 使用示例3. 注意事项二、imp