• 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 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+1i+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
    

    Source

     

    二进制表示点的到达状态。

    状态压缩求哈密顿回路,基本思路如下:

    F[i][j] (0<=i<2^n,0<=j<n) 表示所有点的访问状态为i并且目前处于点j时的最短路径。
    在i的二进制表示下,第k(0<=k<n)位为1表示已经访问过点k。
    F[0][0]=0,Others=+∞,求F[2^n-1][n-1]。
    F[i][j]=Min{F[i^1<<k][k]+w(k,j) | 0<=k<n-1且(i>>k&1)=1}

    在本题中由于要考虑“三角形”关系,故须开三维,f[到达状态][上一个到达的点][本次到达的点]=最优解

    同时要统计方案数,由于方案可能很多,需要开LL

     1 /*by SilverN*/
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 using namespace std;
     8 int f[1<<13][13][13];
     9 long long num[1<<13][13][13];
    10 int mp[13][13];
    11 int v[16];
    12 int n,m;
    13 int main(){
    14     int Q;
    15     scanf("%d",&Q);
    16     while(Q--){
    17         memset(f,-1,sizeof(f));
    18         memset(num,0,sizeof(num));
    19         memset(mp,0,sizeof(mp));
    20         scanf("%d%d",&n,&m);
    21         int i,j,k,s;
    22         for(i=0;i<n;i++){
    23             scanf("%d",&v[i]);
    24         }
    25         int x,y;
    26         for(i=1;i<=m;i++){
    27             scanf("%d%d",&x,&y);
    28             x--;y--;
    29             mp[x][y]=mp[y][x]=1;
    30         }
    31         if(n==1){//单点特判 
    32             printf("%d 1
    ",v[0]);
    33             continue;
    34         }
    35         for(i=0;i<n;i++)//边界预处理 
    36           for(j=0;j<n;j++)
    37               if(i!=j  && mp[i][j]){
    38                   f[(1<<i)|(1<<j)][i][j]=v[i]+v[j]+v[i]*v[j];
    39                   num[(1<<i)|(1<<j)][i][j]=1;
    40               }
    41         for(i=0;i<(1<<n);i++)//连通状况 
    42           for(j=0;j<n;j++)//枚举各岛 
    43               if((i&(1<<j)))
    44                 for(k=0;k<n;k++)
    45                   if(mp[j][k] && j!=k)
    46                   if((i&(1<<k)) && f[i][j][k]!=-1)//j和k枚举的岛都在i枚举范围内,且有上一个状态
    47                   for(s=0;s<n;s++){
    48                       if(mp[k][s] && k!=s && !(i&(1<<s)))
    49                       //k到s联通      s之前没走过
    50                       {
    51                           int val=f[i][j][k]+v[s]+v[k]*v[s];
    52                           if(mp[j][s])val+=v[j]*v[k]*v[s];//三角形特判
    53                         if(f[i|(1<<s)][k][s]<val){//更新状态 
    54                             f[i|(1<<s)][k][s]=val;
    55                             num[i|(1<<s)][k][s]=num[i][j][k];
    56                         }else if(f[i|(1<<s)][k][s]==val)
    57                             num[i|(1<<s)][k][s]+=num[i][j][k];
    58                       }
    59                   }
    60         int ans=0;
    61         long long ansnum=0;//数据很大! 
    62         for(j=0;j<n;j++)
    63           for(k=0;k<n;k++){    
    64               if(k!=j && mp[j][k]){
    65                   s=(1<<n)-1;
    66                   if(ans<f[s][j][k]){
    67                       ans=f[s][j][k];
    68                       ansnum=num[s][j][k];
    69                 }
    70                 else if(ans==f[s][j][k])//解相同则累加方案数 
    71                     ansnum+=num[s][j][k];
    72               }
    73           }
    74         printf("%d %lld
    ",ans,ansnum/2);
    75     }
    76     return 0;
    77 }
  • 相关阅读:
    apache开启.htaccess及.htaccess的使用方法
    如何解决PHP startup: Unable to load dynamic library './php_mysql.dll 找不到指定的模块
    html判断IE版本
    php.ini 配置详解
    检测apache是否支持htaccess文件
    MySql my.ini 中文详细说明
    "安装SQL2005时出现“以前的某个程序安装在计算机上创建挂起文件操作,运行安装程序之前必须重新启动计算机
    iOS开发笔记-两种单例模式的写法
    SQL 2005此计算机上已经安装了同名实例
    win7(windows 7)系统下安装SQL2005(SQL Server 2005)图文教程
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5641938.html
Copyright © 2020-2023  润新知