Islands and Bridges
Time Limit: 4000ms
Memory Limit: 65536KB
This problem will be judged on HDU. Original ID: 1668
64-bit integer IO format: %I64d Java class name: Main
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.
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
Source
解题:一道状压dp题啊,dp[i][j][k]表示当前状态i且当前在k,上一个状态在j
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 14; 5 bool arc[maxn][maxn]; 6 int dp[1<<maxn][maxn][maxn],val[20],n,m; 7 LL cnt[1<<maxn][maxn][maxn]; 8 int main() { 9 int cs; 10 scanf("%d",&cs); 11 while(cs--) { 12 scanf("%d %d",&n,&m); 13 memset(arc,false,sizeof arc); 14 for(int i = 0; i < n; ++i) scanf("%d",val+i); 15 for(int u,v, i = 0; i < m; ++i) { 16 scanf("%d %d",&u,&v); 17 arc[u-1][v-1] = arc[v-1][u-1] = true; 18 } 19 if(n == 1) { 20 printf("%d 1\n",val[0]); 21 continue; 22 } 23 memset(dp,-1,sizeof dp); 24 memset(cnt,0,sizeof cnt); 25 for(int i = 0; i < n; ++i) 26 for(int j = 0; j < n; ++j) 27 if(i != j && arc[i][j]) { 28 dp[(1<<i)|(1<<j)][i][j] = val[i] + val[j] + val[i]*val[j]; 29 cnt[(1<<i)|(1<<j)][i][j] = 1; 30 } 31 for(int i = 0; i < (1<<n); ++i) { 32 for(int j = 0; j < n; ++j) { 33 if(i&(1<<j)) { 34 for(int k = 0; k < n; ++k) { 35 if(j != k && (i&(1<<k)) && arc[j][k] && dp[i][j][k] != -1) { 36 for(int t = 0; t < n; ++t) { 37 if((i&(1<<t)) == 0 && arc[k][t] && j != t && k != t) { 38 int tmp = dp[i][j][k] + val[t] + val[k]*val[t]; 39 if(arc[j][t]) tmp += val[j]*val[k]*val[t]; 40 if(dp[i|(1<<t)][k][t] == tmp) 41 cnt[i|(1<<t)][k][t] += cnt[i][j][k]; 42 else if(dp[i|(1<<t)][k][t] < tmp) { 43 dp[i|(1<<t)][k][t] = tmp; 44 cnt[i|(1<<t)][k][t] = cnt[i][j][k]; 45 } 46 } 47 } 48 } 49 } 50 } 51 } 52 } 53 int ret = 0; 54 LL ret2 = 0; 55 for(int i = 0; i < n; ++i) 56 for(int j = 0; j < n; ++j) 57 if(i != j && arc[i][j]) { 58 if(ret < dp[(1<<n)-1][i][j]) { 59 ret = dp[(1<<n)-1][i][j]; 60 ret2 = cnt[(1<<n)-1][i][j]; 61 } else if(ret == dp[(1<<n)-1][i][j]) 62 ret2 += cnt[(1<<n)-1][i][j]; 63 } 64 printf("%d %I64d\n",ret,ret2>>1); 65 } 66 return 0; 67 }