POJ 2288 Islands and Bridges

Description
Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with each island. We call a Hamilton path the best triangular Hamilton path if it maximizes the value described below.
Suppose there are n islands. The value of a Hamilton path C1C2…Cn is calculated as the sum of three parts. Let Vi be the value for the island Ci. As the first part, we sum over all the Vi values for each island in the path. For the second part, for each edge CiCi+1 in the path, we add the product Vi*Vi+1. And for the third part, whenever three consecutive islands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+2.
Most likely but not necessarily, the best triangular Hamilton path you are going to find contains many triangles. It is quite possible that there might be more than one best triangular Hamilton paths; your second task is to find the number of such paths.


【题目分析】
哈密尔顿回路+动态规划(代码真丑)。

F[i][j] (0<=i< 2^n,0<=j< n) 表示所有点的访问状态为i并且目前处于点j时的最短路径。 在i的二进制表示下,第k(0<=k< n)位为1表示已经访问过点k。 F[0][0]=0,Others=+∞,求F[2^n-1][n-1]。 F[i][j]=Min{F[i^1<< k][k]+w(k,j) | 0<=k< n-1且(i>>k&1)=1}


【代码】

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m;
int val[15],map[13][13];
int dp[1<<13][13][13];
long long num[1<<13][13][13];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++) scanf("%d",&val[i]);
        memset(map,0,sizeof(map));
        int u,v;
        while(m--)scanf("%d%d",&u,&v),u--,v--,map[u][v]=map[v][u]=1;
        if(n==1){
            printf("%d 1\n",val[0]);
            continue;
        }
        memset(dp,-1,sizeof(dp));
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                if(i!=j && map[i][j]){
                    dp[(1<<i)|(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j];
                    num[(1<<i)|(1<<j)][i][j]=1;
                }
        for(int i=0;i<(1<<n);i++)
            for(int j=0;j<n;j++)
                if((i&(1<<j))!=0)
                for(int k=0;k<n;k++)
                    if(map[j][k] && j!=k && (i&(1<<k))!=0 && dp[i][j][k]!=-1)
                    for(int x=0;x<n;x++)
                        if(map[k][x] && j!=x && k!=x && (i&(1<<x))==0){
                            int tmp=dp[i][j][k]+val[x]+val[k]*val[x];
                            if(map[j][x]) tmp+=val[j]*val[k]*val[x];
                            if(dp[i|(1<<x)][k][x]<tmp)dp[i|(1<<x)][k][x]=tmp,num[i|(1<<x)][k][x]=num[i][j][k];
                            else if(dp[i|(1<<x)][k][x]==tmp)num[i|(1<<x)][k][x]+=num[i][j][k];
                        }
        int ans1=0;
        long long ans2=0;
        for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            if(i!=j && map[i][j]){
            if(ans1<dp[(1<<n)-1][i][j])ans1=dp[(1<<n)-1][i][j],ans2=num[(1<<n)-1][i][j];
            else if(ans1==dp[(1<<n)-1][i][j])
            ans2+=num[(1<<n)-1][i][j];
        }
        cout<<ans1<<" "<<ans2/2<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值