热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

poj1734(floyd求最小环)

SightseeingtripTimeLimit:1000MSMemoryLimit:65536KTotalSubmissions:7483Accepted:2827Special

Sightseeing trip

















Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7483 Accepted: 2827 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 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

Source

CEOI 1999

 

此题为求最小环问题

 

考虑的floyd中 ,d[i][j]表示求经过不超过k-1的最短路

所以,最小环可由min{d[i][j]+a[k][i]+a[j][k]}求的;

如图

技术分享图片


#include
#include

#include

#include

using namespace std;
const int maxn=300+10;
int ans=0x3f3f3f3f;
int a[maxn][maxn],d[maxn][maxn],pos[maxn][maxn];
vector
<int>path;
void getpath(int x,int y){
if(pos[x][y]==0) return ;
getpath(x,pos[x][y]);
path.push_back(pos[x][y]);
getpath(pos[x][y],y);
}
int main(){
int n,m;
scanf(
"%d%d",&n,&m);
int u,v,w;
memset(d,
0x3f3f3f3f,sizeof(d));
memset(a,
0x3f3f3f3f,sizeof(a));
while(m--){
scanf(
"%d%d%d",&u,&v,&w);
a[u][v]
=a[v][u]=d[u][v]=d[v][u]=min(a[u][v],w);
}
for (int k=1;k<=n;k++){
for (int i=1;i){
for (int j=i+1;j){
if((long long)d[i][j]+a[k][i]+a[j][k]<ans){
ans
=d[i][j]+a[k][i]+a[j][k];
path.clear();
path.push_back(i);
getpath(i,j);
path.push_back(j);
path.push_back(k);
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
if(d[i][j]>d[i][k]+d[k][j]){
d[i][j]
=d[i][k]+d[k][j];
pos[i][j]
=k;
}
}
}
}
if(ans==0x3f3f3f3f) printf("No solution.\n");
else {
for (int i=0;i){
printf("%d ",path[i]);
}
printf(
"\n");
}
return 0;
}

 


推荐阅读
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 本文详细介绍了Linux中进程控制块PCBtask_struct结构体的结构和作用,包括进程状态、进程号、待处理信号、进程地址空间、调度标志、锁深度、基本时间片、调度策略以及内存管理信息等方面的内容。阅读本文可以更加深入地了解Linux进程管理的原理和机制。 ... [详细]
  • 后台获取视图对应的字符串
    1.帮助类后台获取视图对应的字符串publicclassViewHelper{将View输出为字符串(注:不会执行对应的ac ... [详细]
  • 《数据结构》学习笔记3——串匹配算法性能评估
    本文主要讨论串匹配算法的性能评估,包括模式匹配、字符种类数量、算法复杂度等内容。通过借助C++中的头文件和库,可以实现对串的匹配操作。其中蛮力算法的复杂度为O(m*n),通过随机取出长度为m的子串作为模式P,在文本T中进行匹配,统计平均复杂度。对于成功和失败的匹配分别进行测试,分析其平均复杂度。详情请参考相关学习资源。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 在project.properties添加#Projecttarget.targetandroid-19android.library.reference.1..Sliding ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
author-avatar
手机用户2602901471
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有