(中等) 状态压缩dp HOJ 1894 Islands and Bridges

Islands and Bridges

My Tags  (Edit)
Source : ACM ICPC Shanghai Regional Contest 2004
Time limit : 5 secMemory limit : 32 M

Submitted : 168, Accepted : 45

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 Ci Ci+1 in the path, we add the product Vi * Vi+1. And for the third part, whenever three consecutive islands Ci Ci+1 Ci+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

Shanghai 2004

题意:从任意一个起点出发,要求每个点都要经过且仅能经过一次,经过一个点会相应的分数,到达一个点,能得到该点的分数+这个点与前一个点的分数的乘积+如果这个点与前两个点能形成三角形就能得到他们的乘积的分数,求最多能有多少分,以及有多少种路径能得到这个分数

思路:题目规模很小,n只有13,那么可能会用到状态压缩,这道题里面因为要判断三角形,所以我们想到用dp[s][i][j]目前点的到达状态是s,处于i点并且前一个点是j 所能得到的最大值,同理用sum[s][i][j]表示的是到达这个状态的方法数。
那么dp[to][i][j] = max(dp[to][i][j] , dp[s][j][k]+w[i]+w[j]*w[i]+三角形判定)      sum是一样的 如果能更新dp[to][i][j] 那么sum就直接等于,如果不能更新但是相等,那么sum 就加上。然后这个题目用 long long  

代码:
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<cstdio>
#include<string.h>
using namespace std;

const int inf = 0xfffffff;
const int maxn = (1<<13)+100;
typedef long long LL;
LL dp[maxn][14][14];
LL sum[maxn][14][14];
int T , n , m;
bool g[20][20];
int w[20];

inline int trian(int a,int b,int c)
{
    if (g[a][b] && g[b][c] && g[c][a]) return w[a]*w[b]*w[c];
    return 0;
}
int main()
{
    /*
    freopen("input.in","w",stdout);
    printf("100\n13 %d\n",13*6);
    for (int i = 0 ; i < 13 ; ++i)
        printf("100 ");
    printf("\n");
    for (int i = 1 ; i <= 13 ; ++i)
        for (int j = i+1 ; j <= 13 ; ++j)
            printf("%d %d\n",i,j);
    return 0;
    */
   // freopen("input.in","r",stdin);
    cin>>T;
    while (T--)
    {
       cin>>n>>m;
        for (int i = 0 ; i < n ; ++i)
        {
           scanf("%d",w+i);
        }
        memset(g,false,sizeof(g));
        while (m--)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            --u , --v;
            g[u][v] = g[v][u] = true;
        }
        if (n==1)
        {
           cout << w[0] << " " << 1 << endl;
            continue;
        }
        int max_state = 1<<n;
        for (int i = 0 ; i < max_state ; ++i)
            for (int j = 0 ; j <  n ; ++j)
                for (int k = 0 ; k < n ; ++k)
                    dp[i][j][k] = -inf;
        memset(sum,0,sizeof(sum));
        for (int i = 0 ; i < n ; ++i)
        {
            for (int j = 0 ; j < n ; ++j) if (i!=j && g[i][j])
            {
                int s = (1<<i)+(1<<j);
                dp[s][i][j]  = w[j]+w[i]+w[i]*w[j];
                sum[s][i][j]++;
            }
        }
        LL tem;
        for (int s = 0 ; s < max_state ; ++s)
        {
            for (int i = 0 ; i < n ; ++i) if (s&(1<<i))
            {
                for (int j = 0 ; j < n ; ++j) if (s&(1<<j))
                {
                    if (i==j || !g[i][j] || dp[s][i][j]==-inf) continue;
                    for (int k = 0 ; k < n ; ++k) if ((s&(1<<k))==0)
                    {
                        if (k==i || k==j || !g[i][k]) continue;
                        int to = s+(1<<k);
                        tem = dp[s][i][j]+w[k]+w[k]*w[i]+trian(i,j,k);
                        if (tem > dp[to][k][i])
                        {
                            dp[to][k][i] = tem;
                            sum[to][k][i] = sum[s][i][j];
                        }
                        else if (tem==dp[to][k][i])
                            sum[to][k][i] += sum[s][i][j];
                    }
                }
            }
        }
        LL ans_sum = 0 , ans_max = 0;
        for (int i = 0 ; i < n ; ++i)
        {
            for (int j = 0 ; j < n ; ++j) if (i!=j && g[i][j])
            {
                if (dp[max_state-1][i][j] > ans_max)
                {
                    ans_max = dp[max_state-1][i][j];
                    ans_sum = sum[max_state-1][i][j];
                }
                else if (ans_max==dp[max_state-1][i][j])
                    ans_sum += sum[max_state-1][i][j];
            }
        }
        printf("%lld %lld\n",ans_max,ans_sum/2);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值