poj 3311 Hie with the Pie 【旅行商+回原點】

來源:互聯網
上載者:User

標籤:壓縮   最佳化   二進位   algorithm   tsp   

題目:poj 3311 Hie with the Pie


題意:就是批薩點小二要送批薩,然後給你每個點的距離,有向的,然後讓你就走一次回到原點的最短路。


分析:因為給出的是稠密圖,所以要處理一下最短路,floyd

然後TSP就好。

枚舉每個狀態,對於目前狀態的每一個已經走過的點,枚舉是從那個點走過來的,更新最短路

狀態:dp【st】【i】 :st狀態下走到點 i 的最短路

轉移方程:dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);

然後最後在加上回去的距離即可。注意要更新和,這裡wa了一次。


AC代碼:

#include <cstdio>#include <algorithm>#include <cstring>#include <string>#include <iostream>#include <vector>using namespace std;const int inf = 0x3f3f3f3f;const int N = 12;int mp[N][N];int n;int dp[1<<N][N];int main(){    //freopen("Input.txt","r",stdin);    while(~scanf("%d",&n) && n)    {        n++;        for(int i=0;i<n;i++)        {            for(int j=0;j<n;j++)                scanf("%d",&mp[i][j]);        }        for(int k=0;k<n;k++)        {            for(int i=0;i<n;i++)            {                for(int j=0;j<n;j++)                 mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);            }        }        for(int st=0;st<(1<<n);st++)        {            for(int i=0;i<n;i++)            {                if((st&(1<<i))==0)  //為0                    continue;                if(st==(1<<i)){                    dp[st][i]=mp[0][i];continue;                }                dp[st][i]=inf;                for(int j=0;j<n;j++)                {                    if((st&(1<<j)) && i!=j)//為1                    {                        dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);                    }                }            }        }        int ans=inf;        for(int i=0;i<n;i++){            ans=min(ans,dp[(1<<n)-1][i]+mp[i][0]);        }        printf("%d\n",ans);    }    return 0;}


poj 3311 Hie with the Pie 【旅行商+回原點】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.