• poj 2288 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

    写了这题 对与写状压DP 终于有了点感觉了
    if (j == k || !(i & (1 << k)) || !mp[j][k]|| dp[i][j][k]==-1) continue;

    这里面的
    dp[i][j][k]==-1 没写就WA了  一开始以为还没事 ,后面发现了这个非常重要
     1 #include <cstdio>
     2 #include <iostream>
     3 #include <algorithm>
     4 #include <cmath>
     5 #include <cstdlib>
     6 #include <cstring>
     7 #include <vector>
     8 #include <list>
     9 #include <map>
    10 #include <stack>
    11 #include <queue>
    12 using namespace std;
    13 typedef long long LL;
    14 int n, m, mp[15][15], t;
    15 LL dp[1 << 14][14][14], num[1 << 14][14][14],val[15];
    16 int main() {
    17     scanf("%d", &t);
    18     while(t--) {
    19         memset(dp, -1, sizeof(dp));
    20         memset(num, 0, sizeof(num));
    21         memset(mp,0,sizeof(mp));
    22         scanf("%d%d", &n, &m);
    23         for (int i = 0 ; i < n ; i++) scanf("%lld", &val[i]);
    24         for (int i = 0 ; i < m ; i++) {
    25             int u, v;
    26             scanf("%d%d", &u, &v);
    27             u--, v--;
    28             mp[v][u] = mp[u][v] = 1;
    29         }
    30         if(n==1){
    31             printf("%d 1
    ",val[0]);
    32             continue;
    33         }
    34         for (int i = 0 ; i < n ; i++)
    35             for (int j = 0 ; j < n ; j++)
    36                 if (i != j && mp[i][j]) {
    37                     dp[(1 << i) | (1 << j)][i][j] = val[i] * val[j] + val[i] + val[j];
    38                     num[(1 << i) | (1 << j)][i][j] = 1;
    39                 }
    40         for (int i = 0 ; i < (1 << n) ; i++) {
    41             for (int j = 0 ; j < n ; j++) {
    42                 if (!(i & (1 << j))) continue;
    43                 for (int k = 0 ; k < n ; k++) {
    44                     if (j == k || !(i & (1 << k)) || !mp[j][k]|| dp[i][j][k]==-1) continue;
    45                     for (int q = 0 ; q < n ; q++) {
    46                         LL cost;
    47                         if (q == j || q == k || !mp[k][q] || i & (1 << q)) continue;
    48                         cost = dp[i][j][k] +  val[q] + val[k] * val[q];
    49                         if (mp[j][q]) cost += val[j] * val[k] * val[q];
    50                         if (dp[i | (1 << q)][k][q] < cost)  {
    51                             dp[i | (1 << q)][k][q] = cost;
    52                             num[i | (1 << q)][k][q]=num[i][j][k];
    53                         }
    54                         else if (dp[i | (1 << q)][k][q] == cost)  num[i | (1 << q)][k][q] += num[i][j][k];
    55                     }
    56                 }
    57             }
    58         }
    59         LL ans1 = 0, ans2 = 0;
    60         for (int i = 0 ; i < n ; i++) {
    61             for (int j = 0 ; j < n ; j++) {
    62                 if (i == j || !mp[i][j] ) continue;
    63                 if (dp[(1 << n) - 1][i][j] > ans1) {
    64                     ans1 = dp[(1 << n) - 1][i][j];
    65                     ans2 = num[(1 << n) - 1][i][j];
    66                 } else if(dp[(1 << n) - 1][i][j] == ans1) ans2 += num[(1 << n) - 1][i][j];
    67             }
    68         }
    69         printf("%lld %lld
    ", ans1, ans2/2);
    70     }
    71     return 0;
    72 }
  • 相关阅读:
    练习题
    练习题
    作业 —— day77
    解决:Django项目no such table django_session
    解决:A server error occurred. Please contact the administrator
    作业24
    元类
    类的内置方法
    反射
    考试错题
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9347000.html
Copyright © 2020-2023  润新知