Sightseeing trip

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

 

题意:

给定一张无向图,求图中一个至少包含 3 个点的环,环上的节点不重复,并且环上的边的长度之和最小。该问题称为无向图的最小环问题。

在本题中,你需要输出最小环的方案,若最小环不唯一,输出任意一个均可。若无解,输出 No solution.

图的节点数不超过 100。

题解:

找环的一般方法即是直接搜索,但复杂度较高且不稳定,我们需要寻求一种复杂度较优秀的算法。

注意到经常使用的$Floyd$算法是基于动态规划思想,依次经由$1-N$号中转点更新$dis(i,j)$的。

那么在更新$k$中转点之前的$dis(i,j)$即为严格不经过$k$的最短路,在该路径上加上$i\rightarrow k$和$k\rightarrow j$后一定是一个环。

我们对于所有形如上述的$i,j,k$取$min$即能得到该图的最小环。记录路径输出即可。

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define MAXN 105
#define MAXM 500005
#define INF 0x3f3f3f3f
#define ll long long ll mp[MAXN][MAXN],dis[MAXN][MAXN];
ll ans[MAXN],pre[MAXN][MAXN],cnt; inline ll read(){
ll x=,f=;
char c=getchar();
for(;!isdigit(c);c=getchar())
if(c=='-')
f=-;
for(;isdigit(c);c=getchar())
x=x*+c-'';
return x*f;
} inline void add(ll u,ll v){
if(!pre[u][v]) return;
add(u,pre[u][v]);
ans[++cnt]=pre[u][v];
add(pre[u][v],v);
return;
} int main(){
ll N=read(),M=read();
memset(dis,,sizeof(dis));
memset(mp,,sizeof(mp));
for(ll i=;i<=M;i++){
ll u=read(),v=read(),w=read();
mp[u][v]=mp[v][u]=w;
dis[u][v]=dis[v][u]=w;
}
for(ll i=;i<=N;i++)
dis[i][i]=,mp[i][i]=;
ll minans=INF;
for(ll k=;k<=N;k++){
for(ll i=;i<k;i++)
for(ll j=i+;j<k;j++)
if(dis[i][j]+mp[i][k]+mp[k][j]<minans){
//cout<<dis[i][j]<<" "<<mp[i][k]<<" "<<mp[k][j]<<endl;
//cout<<i<<" "<<j<<" "<<k<<endl;
minans=dis[i][j]+mp[i][k]+mp[k][j];
cnt=,ans[++cnt]=i,add(i,j);
ans[++cnt]=j,ans[++cnt]=k;
}
for(ll i=;i<=N;i++)
for(ll j=;j<=N;j++)
if(dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j],pre[i][j]=k;
}
if(minans==INF){
printf("No solution.\n");
return ;
}
printf("%d",ans[]);
for(ll i=;i<=cnt;i++)
printf(" %d",ans[i]);
printf("\n");
return ;
}
//skeleton

【poj1734】Sightseeing trip的更多相关文章

  1. 【POJ1734】Sightseeing Trip 无向图最小环

    题目大意:给定一个 N 个顶点的无向图,边有边权,如果存在,求出该无向图的最小环,即:边权和最小的环,并输出路径. 题解:由于无向图,且节点数较少,考虑 Floyd 算法,在最外层刚开始遍历到第 K ...

  2. 【CEOI1999】Sightseeing trip

    Description https://loj.ac/problem/10072 Solution 现在我连普及组题都不会了?(bushi) 懒得讲了,看这吧.

  3. 【POJ3621】Sightseeing Cows 分数规划

    [POJ3621]Sightseeing Cows 题意:在给定的一个图上寻找一个环路,使得总欢乐值(经过的点权值之和)/ 总时间(经过的边权值之和)最大. 题解:显然是分数规划,二分答案ans,将每 ...

  4. 【题解】POJ1934 Trip (DP+记录方案)

    [题解]POJ1934 Trip (DP+记录方案) 题意: 传送门 刚开始我是这么设状态的(谁叫我DP没学好) \(dp(i,j)\)表示钦定选择\(i\)和\(j\)的LCS,然而你会发现这样钦定 ...

  5. 「POJ1734」Sightseeing trip

    「POJ1734」Sightseeing trip 传送门 这题就是要我们求一个最小环并且按顺序输出一组解. 考虑 \(O(n^3)\) 地用 \(\text{Floyd}\) 求最小环: 考虑 \( ...

  6. 【POJ 1734】 Sightseeing Trip

    [题目链接] 点击打开链接 [算法] floyd求最小环 输出路径的方法如下,对于i到j的最短路,我们记pre[i][j]表示j的上一步 在进行松弛操作的时候更新pre即可 [代码] #include ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【POJ3621】Sightseeing Cows

    Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8331   Accepted: 2791 ...

  9. poj1734 Sightseeing trip【最小环】

    Sightseeing trip Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:8588   Accepted:3224   ...

随机推荐

  1. delphi如何让程序最小化到任务栏(使用Shell_NotifyIcon API函数)

    现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供了这样的功 ...

  2. mediaxyz访谈录:ffmpeg的码率控制

    mediaxyz是一位研究ffmpeg有三年的高人了,这几天一直在折腾ffmpeg中的x264,就是不知道该如何控制码率,主要是参数太多,也不知道该如何设置,在 google上search了一下,这方 ...

  3. NSMutableURLRequest,在POST方式下传递参数

    1. [代码][C/C++]代码         NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];     NSUs ...

  4. UniCode转码

    <script type="text/javascript"> var GB2312UnicodeConverter = { ToUnicode: function ( ...

  5. PS基本操作

    1 安装 赢政天下2015大师版 安装失败, 删除一下文件夹再重新安装 2 工作界面 2.1 界面 菜单栏; 标题栏; 工具箱; 工具箱选项栏; 面板; 状态栏; 文档窗口; 选项卡 2.2 文档窗口 ...

  6. 一些常用的页面js收集

    //正则表达式 验证整数格式function checkInt(tint){ var re=/^[-]{0,1}[1-9]+[0-9]*]*$/; //判断字符串是否为数字 if (re.test(t ...

  7. 网络应用软件结构-----CS与BS结构(网络基本知识小结)

    1.网络的大致结构 2.网络编程 通过直接或间接地使用网络通讯的协议实现计算机与计算机之间的通讯.在TCP/IP协议层主要麦网络主机的定位,数据传输的路由,由IP地址可以唯一地确定Internet上的 ...

  8. 解除win10禁ping方法

    在局域网中,有一台电脑安装了win10操作系统,win10电脑连接网络没有问题,就是在其他电脑ping这台windows10电脑时就是ping不通,显示“请求超时.”.虽然这台windows10电脑联 ...

  9. 从使用os.system)在python命令(重定向标准输入输出

    从使用os.system)在python命令(重定向标准输入输出 python 标准输出stdout stdio os.system通常我可以通过改变sys.stdout的值在python更改标准输出 ...

  10. NHibernate从入门到精通系列——NHibernate环境与结构体系

    内容摘要 NHibernate的开发环境 NHibernate的结构体系 NHibernate的配置 一.NHibernate的开发环境 NHibernate的英文官方网站为:http://nhfor ...