POJ 2288 Islands and Bridges(状压dp)

http://poj.org/problem?id=2288

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.

Input
The input file starts with a number q (q<=20) on the first line, which is the number of test cases. Each test case starts with a line with two integers n and m, which are the number of islands and the number of bridges in the map, respectively. The next line contains n positive integers, the i-th number being the Vi value of island i. Each value is no more than 100. The following m lines are in the form x y, which indicates there is a (two way) bridge between island x and island y. Islands are numbered from 1 to n. You may assume there will be no more than 13 islands.

Output
For each test case, output a line with two numbers, separated by a space. The first number is the maximum value of a best triangular Hamilton path; the second number should be the number of different best triangular Hamilton paths. If the test case does not contain a Hamilton path, the output must be `0 0’.

Note: A path may be written down in the reversed order. We still think it is the same path.

Sample Input

2
3 3
2 2 2
1 2
2 3
3 1
4 6
1 2 3 4
1 2
1 3
1 4
2 3
2 4
3 4

Sample Output

22 3
69 1


解题思路:
dp[i][j][state]:当前访问岛屿i,上一个访问的岛屿是j,且当前的状态是state的哈密顿值。
way[i][j][state]:当前访问岛屿i,上一个访问的岛屿是j,且当前的状态是state的道路条数。
需要再开一个way数组是因为中间访问岛屿的过程中可能访问的边数和先后不同。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
using namespace std;

typedef long long LL;

LL dp[14][14][1<<13];
bool path[14][14];
LL wei[14];
LL way[14][14][1<<13];

int main()
{
    int cas;  cin>>cas;
    while( cas -- )
    {
        memset(path,false,sizeof(path));
        int n,m; scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)  scanf("%I64d",&wei[i]);
        while(m--)
        {
            int u,v ;  scanf("%d%d",&u,&v);
            path[u][v] = path[v][u] = true;
        }
        if(n==1) {
            cout<<wei[1]<<" "<<1<<endl;
            continue;
        }
        memset(dp,-1,sizeof(dp));        
        memset(way,0,sizeof(way));
        for(int i=1;i<(1<<n);i++)
        {
            for(int j=1;j<=n;j++)
            {
                if( i&(1<<(j-1)) ){
                    if( i==(1<<(j-1)))  dp[j][0][i] = wei[j],way[j][0][i] = 1;
                    for(int k=1;k<=n;k++)
                    {
                        if( i&(1<<(k-1)) ){
                             if(!path[j][k]) continue;
                             if( i == ( (1<<(j-1)) | (1<<(k-1)))) 
                                dp[k][j][i] = wei[j] + wei[k] + wei[k]*wei[j],way[k][j][i] = 1;
                             for(int q = 1;q<=n; q++)
                             {
                                if( !(i&(1<<(q-1)))){
                                     int sta = i|(1<<(q-1));

                                     if(path[k][q] && path[q][j] && dp[k][j][i] !=-1 )
                                     {
                                         if(dp[q][k][sta] < dp[k][j][i] + wei[q] + wei[k]*wei[q] + wei[q]*wei[k]*wei[j])
                                         {
                                            dp[q][k][sta] = dp[k][j][i] + wei[q] + wei[k]*wei[q] + wei[q]*wei[k]*wei[j];
                                            way[q][k][sta] = way[k][j][i];
                                         }
                                         else if(dp[q][k][sta] == dp[k][j][i] + wei[q] + wei[k]*wei[q] + wei[q]*wei[k]*wei[j] )
                                             way[q][k][sta] += way[k][j][i];
                                     }
                                     else if(path[k][q] && !path[q][j] && dp[k][j][i] !=-1)
                                         if(dp[q][k][sta] < dp[k][j][i] + wei[q] + wei[k]*wei[q])
                                         {
                                            dp[q][k][sta] = dp[k][j][i] + wei[q] + wei[k]*wei[q];
                                            way[q][k][sta] = way[k][j][i];
                                         }
                                         else if(dp[q][k][sta] == dp[k][j][i] + wei[q] + wei[k]*wei[q])
                                             way[q][k][sta] += way[k][j][i];
                                }
                             }
                         }
                  }
             }
           }
        }
       LL ans = 0,cnt = 0;
       for(int i=1;i<=n;i++)
       {
           for(int j=1;j<=n;j++)
           {
               if( dp[i][j][(1<<n)-1] > ans)
               {
                    ans = dp[i][j][(1<<n)-1];
                    cnt = way[i][j][(1<<n)-1];
               }
               else if( dp[i][j][(1<<n)-1] == ans)
                   cnt+=way[i][j][(1<<n)-1];
           }
       }
       if(cnt>1) cnt/=2;
       cout<<ans<<" "<<cnt<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值