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. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  2. UVA11892 ENimEN —— 博弈

    题目链接:https://vjudge.net/problem/UVA-11892 题意: 两人玩游戏,有n堆石子,每堆有ai块石子,两人轮流取,要求一次只能选择一堆石子取任意块.最后取完的获胜. 题 ...

  3. CI 模型公用查询函数

    /** * 多字段条件查询数据 * @param array $val array("name" => $value).name为要操作的字段,value为要操作的值 * @ ...

  4. ora-12519:TNS ,no appropriate service handler found

    今天有同事反应,数据库连不了,提示 ora-12519:TNS ,no appropriate service handler found: 一开始以为监听没有启动,排查后,发现正常:于是google ...

  5. JS错题整改

    获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div&quo ...

  6. Opencv— — Color Gradient

    // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...

  7. BZOJ_5415_[Noi2018]归程_kruscal重构树+倍增+最短路

    BZOJ_5415_[Noi2018]归程_kruscal重构树+倍增 Description www.lydsy.com/JudgeOnline/upload/noi2018day1.pdf 好久不 ...

  8. Appleman and a Sheet of Paper

    题意: 给一纸条,两种操作: 1.将左侧长度为$x$的纸条向右翻折. 2.询问位于$[l,r]$的纸条总长度. 解法: 考虑启发式,每一次一个小纸条折叠我们可以看做是一次合并,如果我们每一次将较小的纸 ...

  9. 解决“System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本”

    问题描述:远程访问该数据库(客户端同样是Oracle11g)提示“System.Data.OracleClient需要Oracle客户端软件8.1.7或更高版本”. 解决的办法: 1.一定要关闭Win ...

  10. Spring Boot 学习系列(07)—properties文件读取

    此文已由作者易国强授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 传统的properties读取方式 一般的,我们都可以自定义一个xxx.properties文件,然后在工程 ...