Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 7685 | Accepted: 1968 |
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+1C
i+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
Shanghai 2004 本题是简单的状态压缩的题目,经过每个点一次。而且需要记录路径数。 还有个要注意的地方就是最后输出的路径数要用long long 还要除2,因为反转过来是一样的。
//============================================================================ // Name : POJ.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ /* * POJ 2288 * 路径数最多有13!,要用long long,这里坑了好久 */ #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int MAXN=13; int v[MAXN]; int g[MAXN][MAXN]; int dp[1<<MAXN][MAXN][MAXN]; long long num[1<<MAXN][MAXN][MAXN]; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int T; int n,m; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=0;i<n;i++)scanf("%d",&v[i]); memset(g,0,sizeof(g)); int a,b; while(m--) { scanf("%d%d",&a,&b); a--;b--; g[a][b]=g[b][a]=1; } if(n==1) { printf("%d 1\n",v[0]); continue; } memset(dp,-1,sizeof(dp)); memset(num,0,sizeof(num)); for(int i=0;i<n;i++)//初始化 for(int j=0;j<n;j++) if(i!=j && g[i][j]) { dp[(1<<i)|(1<<j)][i][j]=v[i]+v[j]+v[i]*v[j]; num[(1<<i)|(1<<j)][i][j]=1; } int tot=(1<<n); for(int i=0;i<tot;i++) for(int j=0;j<n;j++) if( (i&(1<<j))!=0) for(int k=0;k<n;k++) if( j!=k && (i&(1<<k))!=0 && dp[i][j][k]!=-1 ) { for(int x=0;x<n;x++) if(g[k][x] && x!=j && x!=k && (i&(1<<x))==0) { int news=(i|(1<<x)); int tmp=dp[i][j][k]+v[x]+v[k]*v[x]; if(g[j][x])tmp+=v[j]*v[k]*v[x]; if(tmp>dp[news][k][x]) { dp[news][k][x]=tmp; num[news][k][x]=num[i][j][k]; } else if(tmp==dp[news][k][x]) num[news][k][x]+=num[i][j][k]; } } int ans1=0; long long ans2=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) if(i!=j && g[i][j]) { if(ans1<dp[tot-1][i][j]) { ans1=dp[tot-1][i][j]; ans2=num[tot-1][i][j]; } else if(ans1==dp[tot-1][i][j]) ans2+=num[tot-1][i][j]; } printf("%d %I64d\n",ans1,ans2/2); } return 0; }