POJ2288: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.

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
 
这道题因为括号的问题纠察了好久,主要还是因为运算符的优先级问题。。。
 
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

int n,m;
int edge[15][15];
__int64 dp[1<<14][15][15],way[1<<14][15][15],tem,v[15];

int main()
{
    int t,i,j,k,x,y,s;
    scanf("%d",&t);
    while(t--)
    {
        memset(edge,0,sizeof(edge));
        scanf("%d%d",&n,&m);
        for(i = 1; i<=n; i++)
            scanf("%I64d",&v[i]);
        if(n == 1)
        {
            printf("%I64d 1\n",v[1]);
            continue;
        }
        memset(way,0,sizeof(way));
        memset(dp,-1,sizeof(dp));
        for(i = 0; i<m; i++)
        {
            scanf("%d%d",&x,&y);
            edge[x][y] = edge[y][x] = 1;
        }
        for(i = 1; i<=n; i++)//所有两两相连的路dp储存价值,way储存路数
        {
            for(j = 1; j<=n; j++)
            {
                if(i == j || !edge[i][j]) continue;
                int ii = 1<<(i-1);
                int jj = 1<<(j-1);
                tem = v[i]+v[j]+v[i]*v[j];
                dp[ii+jj][i][j] = tem;
                way[ii+jj][i][j] = 1;
            }
        }
        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]) continue;
                    for(k = 1; k<=n; k++)
                    {
                        if((s&(1<<(k-1))) == 0 || j == k || i == k ) continue;
                        int news = s - (1<<(i-1));//找出i不在状态内但是j,k在状态内的状况
                        if(dp[news][j][k]==-1) continue;
                        if(!edge[j][k]) continue;
                        tem = v[i]+v[j]*v[i]+dp[news][j][k];
                        if(edge[i][k])//如果i与k相连,加上其价值
                            tem+=v[i]*v[j]*v[k];
                        if(tem>dp[s][i][j])//tem大于dp的话,更新dp同时更新way
                        {
                            dp[s][i][j] = tem;
                            way[s][i][j] = way[news][j][k];
                        }
                        else if(tem == dp[s][i][j])//dp相等,则加上路数
                            way[s][i][j] += way[news][j][k];
                    }
                }
            }
        }
        __int64 ans = -1,sum = 0;
        int p = (1<<n)-1;
        for(i = 1; i<=n; i++)
        {
            for(j = 1; j<=n; j++)
            {
                if(i == j) continue;
                if(ans < dp[p][i][j])
                {
                    ans = dp[p][i][j];
                    sum = way[p][i][j];
                }
                else if(ans == dp[p][i][j])
                    sum += way[p][i][j];
            }
        }
        if(ans == -1)
            printf("0 0\n");
        else
            printf("%I64d %I64d\n",ans,sum/2);
    }

    return 0;
}


POJ2288:Islands and Bridges(状态压缩)

时间: 2024-11-05 11:25:16

POJ2288:Islands and Bridges(状态压缩)的相关文章

POJ 2288 Islands And Bridges 状态压缩dp+哈密顿回路

题意:n个点 m条边的图,路径价值定义为相邻点乘积,若路路径c[i-1]c[i]c[i+1]中c[i-1]-c[i+1]有边 则价值加上三点乘积找到价值最大的哈密顿回路,和相应的方法数n<=13.暴力dfs O(13!) TLE 由于n<13 经典的状态压缩dp [状态] [当前点位置 ][前一点位置] 注意上一个状态必须合法才能进行转移设状态dp[s][i][j] 当前状态为s,当前点为i上一个点为j的最大价值, ans=max(dp[(1<<n)-1][i])dp[s][i][

POJ2288Islands and Bridges(状态压缩DP,求最大路和走条数)

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 8845   Accepted: 2296 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 i

poj2288(Islands and Bridges) 状压DP

题目链接:http://poj.org/problem?id=2288 题意:每个点有一个权值Vi,找一条哈密顿路径,路径的权值来自三条:1 路径上的Vi之和 2 所有相邻点对ij的Vi*Vj之和 3 相邻连续三点i,j,k(并且三点要构成三角形)Vi*Vj*Vk之和. 解法:dp[st][i][j]表示从j走到i并且剩下集合st没有走的最大权值.关于路径书,在转移的时候顺便计算即可:这道题令自己恶心了好久,最后原因是自己犯了一个严重错误,题目读错了,没有读到Vi*Vj*Vk要保证ijk能够构成

poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题

题目链接 题目描写叙述:哈密尔顿路问题.n个点,每个点有权值,设哈密尔顿路为 C1C2...Cn,Ci的权值为Vi,一条哈密尔顿路的值分为三部分计算: 1.每个点的权值之和2.对于图中的每一条CiCi+1,加上Vi*Vi+1 3.对于路径中的连续三个点:CiCi+1Ci+2,若在图中,三点构成三角形,则要加上Vi*Vi+1*Vi+2 求一条汉密尔顿路能够获得的最大值,而且还要输出有多少条这种哈密尔顿路. 这道题的状态感觉不是非常难想,由于依据一般的哈密尔顿路问题,首先想到的是设计二维状态,dp[

【汉密尔顿、DP|状态压缩】POJ-2288 Islands and Bridges

Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K       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.

POJ2288 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 e

POJ 2288 Islands and Bridges 哈密尔顿路 状态压缩DP

找最长的其实是很裸的状态压缩DP,棘手的地方是要统计数量,其实只要再来一个数组存就好. 不过代码比较长,细节要注意的地方毕较多,wa了很多发,还是要仔细啊 用递推和记忆化搜索分别写了一遍 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <

HDU 1668 Islands and Bridges

Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Original ID: 166864-bit integer IO format: %I64d      Java class name: Main Given a map of islands and bridges that connect these islands, a Hamilton pat

POJ 2288 Islands and Bridges(状压dp)

Language: Default Islands and Bridges Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 9312   Accepted: 2424 Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the b