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. dhcpcd守护进程分析【转】

    本文转载自;http://blog.csdn.net/lishanmin11/article/details/37930073 最近在调android ethernet功能,android本身不带 e ...

  2. Appium——Error while obtaining UI hierarchy XML file:com.android.ddmlib.SyncException:

    使用uiautomatorviewer查看页面元素时报这个错误,解决办法 cmd: adb root ok  解决

  3. 解决LoadRunner超时错误

    在录制Web协议脚本回放时超时情况经常出现,产生错误的原因也有很多,解决的方法也不同. 错误现象1:Action.c(16): Error -27728: Step download timeout ...

  4. codeforces C. New Year Ratings Change 解题报告

    题目链接:http://codeforces.com/problemset/problem/379/C 题目意思:有n个users,每个user都有自己想升的rating.要解决的问题是给予每个人不同 ...

  5. php filter 安全过滤函数

    转自:http://www.blags.org/archives/741.html php 利用filter 扩展编写的参数处理静态类,欢迎使用.希望大家看得开心,用得放心. <?php /** ...

  6. excel 基本用法

  7. BZOJ_1095_[ZJOI2007]Hide 捉迷藏_动态点分治+堆

    BZOJ_1095_[ZJOI2007]Hide 捉迷藏_动态点分治+堆 Description 捉迷藏 Jiajia和Wind是一对恩爱的夫妻,并且他们有很多孩子.某天,Jiajia.Wind和孩子 ...

  8. codevs 4768跳石头

    传送门 4768 跳石头  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold  题目描述 Description 一年一度的“跳石头”比赛又要开始了! 这项比赛将在 ...

  9. SqlSession

  10. 《Objective-C高级编程》の内存管理の学习笔记

    此日志用于记录下学习过程中碰到的问题 转载请注明出处: http://www.cnblogs.com/xdxer/p/4069650.html <Objective-C高级编程> 人民邮电 ...