poj 2288 Islands and Bridges

Islands and Bridges
Time Limit: 4000MS Memory Limit: 65536K
Total Submissions: 7140 Accepted: 1823

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

给自己提醒:类此题目不要忘记检查非法状态

是一道不错的状态压缩dp,设计状态table[i][j][k],其中i为压缩状态2进制从低到高表示没一个城市的访问情况,

k表示当前所在城市,j表示访问的上一个城市

转改转移为table[i][j][k] max(table[i^(1 << k)][x][j]+value) 其中(i^(1 << k))&(1 << x)为1且table[i^(1 << k)][x][j]为有效状态,有路径j -> k

注意一个点的情况和 统计条数用64位整数 


#include <iostream>

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>


using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::getline;


int pre(int sour)
{
int ret = 0;
while(sour)
{
++ret;
sour ^= sour&(-sour);
}
return ret;
}


int parr[256];


inline int cal_bit1(int sour)
{
unsigned char *p = (unsigned char *)&sour;
return parr[p[0]]+parr[p[1]]+parr[p[2]]+parr[p[3]];
}


bool can[15][15];
int table[1 << 14][15][15];
long long cways[1 << 14][15][15];
int value1[15];
int value2[15][15];
int value3[15][15][15];


int main()
{
for(int i = 0; i <= 255; ++i)
parr[i] = pre(i);
int n_case;
scanf("%d", &n_case);
while(n_case--)
{
memset(can, 0, sizeof(can));
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
scanf("%d", value1+i);
int tu, tv;
for(int i = 0; i < m; ++i)
{
scanf("%d%d", &tu, &tv);
--tu;
--tv;
can[tu][tv] = true;
can[tv][tu] = true;
}
if(n == 1)
{
printf("%d 1\n", value1[0]);
continue;
}
memset(value2, 0, sizeof(value2));
memset(value3, 0, sizeof(value3));
for(int i = 0; i < n; ++i)
can[i][i] = false;
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(can[i][j])
{
value2[i][j] = value1[i]*value1[j];
for(int k = 0; k < n; ++k)
if(can[j][k] && can[i][k])
value3[i][j][k] = value2[i][j]*value1[k];
}
int limit = 1 << n;
memset(table, -1, sizeof(table[0])*limit);
memset(cways, 0, sizeof(cways[0])*limit);
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j)
if(can[i][j])
{
int ind = (1 << i)|(1 << j);
table[ind][i][j] = value1[i]+value1[j]+value2[i][j];
cways[ind][i][j] = 1LL;
}
for(int i = 0; i < limit; ++i)
{
if(cal_bit1(i) > 2)
{
for(int j = 0; j < n; ++j)
{
if((i&(1 << j)) != 0)
{
int temp = i^(1 << j);
for(int k1 = 0; k1 < n; ++k1)
if((temp&(1 << k1)) != 0)
{
for(int k2 = 0; k2 < n; ++k2)
if((temp&(1 << k2)) != 0 && can[k1][k2] && can[k2][j] && table[temp][k1][k2] != -1) //千万别忘了检查转台是否合法,丢了table[temp][k1][k2],调试了整整一上午
{
int tt = table[temp][k1][k2]+value1[j]+value2[k2][j]+value3[k1][k2][j];
if(tt == table[i][k2][j])
{
cways[i][k2][j] += cways[temp][k1][k2];
}
else
if(tt > table[i][k2][j])
{
table[i][k2][j] = tt;
cways[i][k2][j] = cways[temp][k1][k2];
}
}
}
}
}
}
}
int ans1 = 0;
long long ans2 = 0LL;
int temp = limit-1;
for(int j = 0; j < n; ++j)
for(int k = 0; k < n; ++k)
if(can[j][k])
{
if(table[temp][j][k] == ans1)
{
ans2 += cways[temp][j][k];
}
else
if(table[temp][j][k] > ans1)
{
ans1 = table[temp][j][k];
ans2 = cways[temp][j][k];
}
}
printf("%d %I64d\n", ans1, (ans2 >> 1));
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值