Poj2288 (islands and bridges) Pressure DP

Source: Internet
Author: User

Question link: http://poj.org/problem? Id = 2288

Each vertex has a weight VI and finds a Hamilton path. The weight of the path comes from three: 1. The sum of VI in the path 2 the sum of VI * VJ of all adjacent points to IJ 3 the sum of VI * VJ * VK of adjacent three consecutive I, j, and K (and the three points must form a triangle.


Solution: DP [st] [I] [J] indicates the maximum weight from J to I and the rest of the Set st is not taken. The path book can be computed by the way during the transfer. This question has been disgusting for a long time. The final reason is that you have made a serious mistake and read the wrong question, if you do not read VI * VJ * VK, you must ensure that ijk can form a triangle and check whether ik has edges by modifying the code. Of course, pay attention to the situations where the number of paths may exceed int and a specific point;


Code:

/******************************************************* @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*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.