poj 1734 Sightseeing trip(floyd求最小环并输出方案)

来源:互联网 发布:全知单恋视角 编辑:程序博客网 时间:2024/06/05 23:54

题目链接

Sightseeing trip
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5736 Accepted: 2220 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 71 4 11 3 3003 1 101 2 162 3 1002 5 155 3 20

Sample Output

1 3 5 2

题解:floyd求最小环,在floyd的同时求解出最小环。floyd的原理,dp[k][i][j] 表示i到j只经过前k个点的最短距离。
转移就是:dp[k][i][j]=min(dp[k-1][i][j] , dp[k-1][i][k]+dp[k-1][k][j] ) .

在转移的过程中处理出最小环,在求解dp[k][i][j]之前,枚举以k为最大编号的环即可。

记录转移,递归输出解即可。

复杂度O(n^3)

详情见代码:

#include<stdio.h>#include<iostream>#include<algorithm>#include<string>#include<string.h>#include<vector>#include<queue>#include<set>using namespace std;const int nn = 110;const int inf = 100000010;const int mod = 10000003;typedef long long LL;int n,m;int tu[nn][nn];//原图两点间的最小距离int dp[nn][nn][nn];int key[nn][nn][nn];//记录转移void solve(int k,int u,int v)//输出只经过前k个点u到v的最短路径上的点(不输出u){    if(k==0)    {        printf("%d",v);        return ;    }    if(key[k][u][v]==-1)    {        solve(k-1,u,v);        return ;    }    solve(k-1,u,key[k][u][v]);    printf(" ");    solve(k-1,key[k][u][v],v);}int main(){    int i,j,k;    while(scanf("%d%d",&n,&m)!=EOF)    {        for(i=1;i<=n;i++)        {            tu[i][i]=0;            for(j=1;j<i;j++)            {                tu[i][j]=tu[j][i]=inf;            }        }        for(i=1;i<=m;i++)        {            int u,v,l;            scanf("%d%d%d",&u,&v,&l);            tu[u][v]=tu[v][u]=min(tu[u][v],l);        }        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)            {                dp[0][i][j]=tu[i][j];            }        }        memset(key,-1,sizeof(key));        int mincirle=inf;        int cirlei,cirlej,cirlek;        /*        floyd的同时处理出最小环        */        for(k=1;k<=n;k++)        {            for(i=1;i<k;i++)            {                for(j=1;j<k;j++)                {                    if(i==j)                        continue;                    if(dp[k-1][i][j]+tu[j][k]+tu[k][i]<mincirle)                    {                        mincirle=dp[k-1][i][j]+tu[j][k]+tu[k][i];                        cirlei=i,cirlej=j,cirlek=k;                    }                }            }            for(i=1;i<=n;i++)            {                for(j=1;j<=n;j++)                {                    dp[k][i][j]=dp[k-1][i][j];                    if(dp[k][i][j]>dp[k-1][i][k]+dp[k-1][k][j])                    {                        dp[k][i][j]=dp[k-1][i][k]+dp[k-1][k][j];                        key[k][i][j]=k;                    }                }            }        }        if(mincirle==inf)        {            puts("No solution.");        }        else        {            printf("%d %d ",cirlek,cirlei);            solve(cirlek-1,cirlei,cirlej);            puts("");        }    }    return 0;}


0 0