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\n",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\n",ans,ansnum/2);
75     }
76     return 0;
77 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5641938.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值