Islands and Bridges POJ - 2288 (状压dp)

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

题意:

给出n个点,m条边。每个点有一个权值w。找出一条汉密尔顿路径,使它的值最大。一条汉密尔顿路径的值由三部分组成:

1) 路径上每个点的权值之和

2) 路径上每条边u-v,将其权值的积累加起来。即w[u]*w[v]

3) 如果三个点形成一个三角形,例如i、i+1、i+2,那么将w[i]*w[i+1]*w[i+2]累加起来

一条汉密尔顿路径可能包含多个三角形,一张图中也可能包含多个最好的汉密尔顿路径。输出最大的汉密尔顿路径的值,以及这样的汉密尔顿路径的个数。同一条汉密尔顿路径的两种走法算作一种。

思路:先预处理出所有相连的点的状态及初值,枚举每种状态,在枚举状态的同时,找到可以连接的新状态,并判断是否成三角行等。

找最大值,只需要知道最后一步走的是哪两个 点,并且开始找下一个新的就行,与找最小的汉密尔顿回路不同(找最小路,需要在已有的状态下调整)。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#include <bitset>
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=1e5+10;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
int n,m;
int v[20];
int mp[20][20];
int dp[1<<13][13][13];
ll num[1<<13][13][13];
void solve()
{
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(mp[i][j])
            {
                dp[(1<<i)|(1<<j)][i][j]=v[i]+v[j]+v[i]*v[j];
                num[(1<<i)|(1<<j)][i][j]=1;
            }
        }
    }
    int up=(1<<n)-1;
    for(int s=0;s<=up;s++)
    {
        for(int i=0;i<n;i++)
        {
            if(s&(1<<i))
            {
                for(int j=0;j<n;j++)
                {
                    if((s&(1<<j))&&mp[i][j]&&dp[s][i][j]!=-1)
                    {
                        for(int k=0;k<n;k++)
                        {
                            if(k!=i&&!((1<<k)&s)&&mp[j][k])
                            {
                                int tmp=dp[s][i][j]+v[k]+v[k]*v[j];
                                if(mp[i][k])
                                {
                                    tmp+=v[i]*v[j]*v[k];
                                }
                                if(dp[s|(1<<k)][j][k]<tmp)
                                {
                                    dp[s|(1<<k)][j][k]=tmp;
                                    num[s|(1<<k)][j][k]=num[s][i][j];
                                }
                                else if(dp[s|(1<<k)][j][k]==tmp)
                                {
                                    num[s|(1<<k)][j][k]+=num[s][i][j];
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&v[i]);
        }
        memset(mp,0,sizeof(mp));
        memset(num,0,sizeof(num));
        memset(dp,-1,sizeof(dp));
        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            x-=1;
            y-=1;
            mp[x][y]=1;
            mp[y][x]=1;
        }
        if(n==1)
        {
            printf("%d 1\n",v[0]);
            continue;
        }
        solve();
        int M=0;
        ll ans=0;
        int up=(1<<n)-1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                if(mp[i][j])
                {
                    if(M<dp[up][i][j])
                    {
                        M=dp[up][i][j];
                        ans=num[up][i][j];
                    }
                    else if(M==dp[up][i][j])
                    {
                        ans+=num[up][i][j];
                    }
                }
            }
        }
        printf("%d %lld\n",M,ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81748546
今日推荐