poj 2288 状态压缩dp

Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 9023 Accepted: 2338

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 CiC i+1 in the path, we add the product Vi*V i+1. And for the third part, whenever three consecutive islands CiC i+1C i+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and C i+2, we add the product Vi*V i+1*V i+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

【题目大意】求汉密尔顿的一道变形问题,中间每个点有权值,关于最后得分的描述如下

Suppose there are n islands. The value of aHamilton path C1C2...Cn is calculated as the sum of three parts. Let Vi be thevalue for the island Ci. As the first part, we sum over all the Vi values foreach 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 consecutiveislands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is abridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+2. 

这题要求让得分最高

【解析】发现每个点的状态由前面两个点确定,用DP(S,A,B)表示状态为S时,当前到达A,而上一个点是B时的最大得分,这个状态由DP(S',B,C)通过从B走到A得到,S'=S-(1<<A),即S'状态就是经过B和C但不经过A的一个状态,C是不同于A和B的一个点。

【状态转移】dp[S][A][B] =max(dp[S][A][B],dp[S'][B][C]+temp) 这里的temp指的是加上的得分即Vb*Va+Va,如果构成三角关系(即A和C间有边),temp就要再加上Vb*Va*Vc.

【边界条件】DP((1<<A)+(1<<B),A,B)=Va+Vb+Va*Vb(A和B间有边)表示

  • Source Code
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    long long int dp[1<<14][14][14];
    int edge[15][15];
    int ans;
    long long int way[1<<14][14][14];
    long long int val[15];
    
    int main()
    {
        int i, j, k, l, m, n, s;
        int t;
        cin >> t;
        while( t-- )
        {
            cin>> n >> m;
    
    
            memset(edge,0,sizeof(edge));
            memset(dp,-1,sizeof(dp));
            memset(way,0,sizeof(way));
            ans = -1;
    
            for ( i = 1; i <= n; i++ )
            {
                cin >> val[i];
            }
    
            for ( i = 1; i <= m; i++ )
            {
                cin >> j>> k;
                edge[j][k] = edge[k][j] = 1;
            }
            if(n==1)
            {
                cout << val[1] << " " << 1 <<endl;
                continue;
            }
            for ( i = 1; i <= n; i++ )
            {
                for ( j = 1; j <= n; j++ )//此处可优化
                {
                    if(i == j || edge[i][j] == 0)
                    continue;
                    int ii = 1<<(i-1);
                    int jj = 1<<(j-1);
                    s = ii + jj;
                    dp[s][i][j] = val[i] + val[j] + val[i]*val[j];
                    way[s][i][j] = 1;
                    //cout<<s<<" "<<i<<" "<<j<<" "<<dp[s][i][j]<<endl;
                }
            }
    
            for ( s = 0; s < (1<<n); s++ )
            {
                for ( i = 1; i <= n; i++ )
                {
                    if((s & (1 << (i-1))) == 0)
                    continue;
                    for ( j = 1; j <= n; j++ )
                    {
                        if((s & (1<<(j-1))) ==0||i == j || edge[i][j] == 0)
                        continue;
    
    
    
                        for ( k = 1; k <= n; k++ )
                        {
    
                            if( i == k || j == k ||(s & (1<<(k-1))) == 0 || edge[j][k] == 0)
                            continue;
    
                            int s1 = s - (1<<i-1);
    
                            if(dp[s1][j][k] == -1)
                            continue;
    
                           long long  int tmp = val[i] + val[j]*val[i] + dp[s1][j][k];
    
                            if( edge[i][k] )
                            tmp += val[i]*val[j]*val[k];
    
                           // dp[s][i][j] = max(dp[s][i][j],dp[s1][j][k] + tmp);
                            if( dp[s][i][j] < tmp)
                            {
                                dp[s][i][j] = tmp;
                                way[s][i][j] = way[s1][j][k];
                            }
                            else if( dp[s][i][j] == tmp )
                            {
                                way[s][i][j] += way[s1][j][k];
                            }
                           // cout << s<< " "<<i<<" "<<j<<" "<<dp[s][i][j]<<endl;
                        }
                    }
                }
            }
            long long int sum = 0;
            int end = (1<<n)-1;
            for ( i = 1; i <= n; i++ )
            {
                for ( j = 1; j <= n; j++ )
                {
                    if( i == j )
                    continue;
                    if(ans < dp[end][i][j])
                    {
                        ans = dp[end][i][j];
                        sum = way[end][i][j];
                    }
                    else if(ans == dp[end][i][j])
                    {
                        sum += way[end][i][j];
                    }
                }
            }
            if(ans == -1)
            {
                ans = sum = 0;
            }
            cout << ans << " " << sum/2 <<endl;
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值