poj2288(Islands and Bridges) 狀壓DP

來源:互聯網
上載者:User

標籤:style   blog   http   get   2014   os   

題目連結:http://poj.org/problem?id=2288

題意:每個點有一個權值Vi,找一條哈密頓路徑,路徑的權值來自三條:1 路徑上的Vi之和 2 所有相鄰點對ij的Vi*Vj之和 3 相鄰連續三點i,j,k(並且三點要構成三角形)Vi*Vj*Vk之和。


解法:dp[st][i][j]表示從j走到i並且剩下集合st沒有走的最大權值。關於路徑書,在轉移的時候順便計算即可;這道題令自己噁心了好久,最後原因是自己犯了一個嚴重錯誤,題目讀錯了,沒有讀到Vi*Vj*Vk要保證ijk能夠構成一個三角形,在程式改一點判斷ik是否有邊即可。當然還要注意路徑數可能超int,以及特判一個點的情況;


代碼:

/******************************************************* @author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <queue>#include <vector>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <string.h>//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8#define zero(_) (abs(_)<=eps)const double pi=acos(-1.0);typedef long long LL;const int Max=14;const int INF=1e9+7;int num[Max];LL dp[1<<Max][Max][Max];LL cnt[1<<Max][Max][Max];bool rem[Max][Max];vector<int> vec[Max];int n,m;LL getdp(int st,int i,int j){    if(dp[st][i][j]!=-1)        return dp[st][i][j];    LL ans=-INF;    LL sum=0;    for(int l=0; l<vec[i].size(); l++)    {        int k=vec[i][l];        if((st&(1<<k))==0) continue;        if()        LL tool=getdp(st-(1<<k),k,i)+num[k]*num[i]+num[k]*num[i]*num[j];        if(tool==ans)            sum+=cnt[st-(1<<k)][k][i];        if(tool>ans)        {            sum=cnt[st-(1<<k)][k][i];            ans=tool;        }    }    cnt[st][i][j]=sum;    return dp[st][i][j]=ans;}int main(){    int t;    cin>>t;    while(t--)    {        memset(rem,0,sizeof rem);        scanf("%d%d",&n,&m);        int sum=0;        for(int i=0; i<n; i++)            scanf("%d",num+i),sum+=num[i],vec[i].clear();        for(int i=0; i<m; i++)        {            int a,b;            scanf("%d%d",&a,&b);            a--,b--;            if(a==b)                continue;            vec[a].push_back(b);            vec[b].push_back(a);            rem[a][b]=1;            rem[b][a]=1;        }        if(n==1)        {            printf("%d %d\n",num[0],1);            continue;        }        memset(dp,-1,sizeof dp);        memset(cnt,0,sizeof cnt);        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)            {                dp[0][i][j]=-INF;                if(!rem[i][j])continue;                dp[0][i][j]=0;                cnt[0][i][j]=1;            }        LL ans=-INF;        LL out=0;        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)            {                if(!rem[i][j])                    continue;                LL tool=getdp((1<<n)-(1<<i)-(1<<j)-1,i,j)+num[i]*num[j];                if(ans==tool)                    out+=cnt[(1<<n)-(1<<i)-(1<<j)-1][i][j];                if(ans<tool)                {                    ans=tool;                    out=cnt[(1<<n)-(1<<i)-(1<<j)-1][i][j];                }            }        if(out==0||ans<=0)            printf("0 0\n");        else            cout<<ans+sum<<" "<<out/2<<endl;    }    return 0;}/*6 151 1 1 1 1 11 21 31 41 51 62 32 42 52 63 43 53 64 54 65 6*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.