poj 1734 Sightseeing trip (floyd求最小環並記錄路徑)

點擊打開題目鏈接

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

題意:

有n個城市,有m條路,每條路連接兩個城市而且有一定的花費,你的任務是找出一條由起點開始又回到起點的路徑,使得路徑上所有的花費最小,並且這條路徑至少有3個以上不同城市,假如從v1開始,那麼v1->v2->....->vk,vk再回到v1,k要大於2,輸出格式爲v1 v2 ....vk

其中n<=100,m<=10000,每條路徑花費不大於500;

分析:如果不輸出路徑,那麼此題就是一個floyd求最小環的問題,但是此題需要輸出的是最小環經過的點,所以我們就要在原來求最小環的基礎上加一個數組記錄路徑,我們另外開一個數組pa[i][j]=x表示的是i->......->x->j,意思就是pa[i][j]存的是i->j這條路徑上j的前面一個點是哪個。

很顯然,初始化時pa[i][j]=i;

那麼如何用dp轉移呢,回顧一下求floyd求最短路時轉移是這樣的:如果dis[i][j]>dis[i][k]+dis[k][j],那麼dis[i][j]=dis[i][k]+dis[k][j],我們可以在這個時候同時轉移pa,使pa[i][j]=pa[k][j],仔細想一下是不是這樣,i->j這條路徑上倒數第二個經過的點就是k->j上倒數第二個經過的點,因爲pa[i][k]和pa[k][j]已經求出來了的,所以直接轉移。

轉移完成了,接下來的工作就是該怎麼記錄答案並且輸出答案。我們只需要在floyd求最小環記錄答案的時候把路徑記錄下來。

具體操作代碼實現:

#include<Stdio.h>
#include<string.h>
#include<algorithm>
#define inf 1e8
using namespace std;
int dis[110][110];///記錄最短路
int pa[110][110];///記錄i>j這條路徑上倒數第二個經過的點
int ma[110][110];///記錄原圖
int ans[110];///記錄路徑(倒着的)
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)==2)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                dis[i][j]=ma[i][j]=inf;
                pa[i][j]=i;
            }
        }
        for(int i=1;i<=m;i++)
        {
            int x,y,c;
            scanf("%d%d%d",&x,&y,&c);
            if(ma[x][y]>c)///防止重邊
            {
                ma[x][y]=dis[x][y]=c;
                ma[y][x]=dis[y][x]=c;
            }
        }
        int minn=inf,num;///num記錄的是路徑上點的個數
        for(int k=1;k<=n;k++)
        {
            ///下面這兩個循環意思是以k爲起點,看看有沒有回到k點的路徑,i和j表示的是和k點直接相連的不同的兩個點,這樣保證了這個換有三個點
            for(int i=1;i<k;i++)
            {
                for(int j=i+1;j<k;j++)
                {
                    if(minn>dis[i][j]+ma[k][i]+ma[k][j])
                    {
                        num=0;///每次找到更好的答案時便要重新記錄路徑
                        minn=dis[i][j]+ma[k][i]+ma[k][j];
                        int posi=i,posj=j;
                        ans[++num]=j;
                        while(1)///倒着記錄路徑,畫個圖自然明白
                        {
                            posj=pa[posi][posj];
                            num++;
                            ans[num]=posj;
                            if(posi==posj) break;
                        }
                        ans[++num]=k;
                    }
                }
            }
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=n;j++)
                {
                    if(dis[i][j]>dis[i][k]+dis[k][j])
                    {
                        dis[i][j]=dis[i][k]+dis[k][j];
                        pa[i][j]=pa[k][j];
                    }
                }
            }
        }
        if(minn==inf)
        {
            puts("No solution.");
            continue;
        }
        for(int i=num;i>=2;i--) printf("%d ",ans[i]);
        printf("%d\n",ans[1]);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章