HDU 1668 Islands and Bridges

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

2004 Asia Regional Shanghai

解题:一道状压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 }

时间: 2024-10-06 17:59:42

HDU 1668 Islands and Bridges的相关文章

HDU 4738 Caocao&#39;s Bridges tarjan求桥

Caocao's Bridges Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Chan

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

hdu 4738 Caocao&#39;s Bridges tarjan

Caocao's Bridges Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good

HDU 4738 Caocao&#39;s Bridges(求价值最小的桥)

Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army still was not good at water battles, so he came up with another idea. He built many islands in the Changjiang river, and

HDU 4738 Caocao&#39;s Bridges

Caocao's Bridges Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 473864-bit integer IO format: %I64d      Java class name: Main Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wo

HDU 4738 --Caocao&#39;s Bridges 【无向图边双联通 &amp;&amp; 求权值最小的桥 &amp;&amp; 模板】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2641    Accepted Submission(s): 855 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

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

HDU 4738 Caocao&#39;s Bridges(割边)

乍一看一个模板题,仔细一看还是模板题,但是三个坑.1,不是连通图,放0个.2 守卫为0,放1个. 3注意重边. #include<iostream> #include<cstdio> #include<vector> #include<queue> #include<algorithm> #include<stack> #include<cstring> using namespace std; #define maxn

HDU 4738 Caocao&#39;s Bridges(找割边)

HDU 4738 Caocao's Bridges 题目链接 注意几个坑,可能重边,至少要派一个人去炸,没有连通的时候就不用炸了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 1005; const int INF = 0x3f3f3f3f; int pre[N], low[N