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.
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 ightarrow k$和$k ightarrow 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=0,f=1; char c=getchar(); for(;!isdigit(c);c=getchar()) if(c=='-') f=-1; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; 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,27,sizeof(dis)); memset(mp,27,sizeof(mp)); for(ll i=1;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=1;i<=N;i++) dis[i][i]=0,mp[i][i]=0; ll minans=INF; for(ll k=1;k<=N;k++){ for(ll i=1;i<k;i++) for(ll j=i+1;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=0,ans[++cnt]=i,add(i,j); ans[++cnt]=j,ans[++cnt]=k; } for(ll i=1;i<=N;i++) for(ll j=1;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. "); return 0; } printf("%d",ans[1]); for(ll i=2;i<=cnt;i++) printf(" %d",ans[i]); printf(" "); return 0; } //skeleton