首页 > 代码库 > POJ2288Islands and Bridges(状态压缩DP,求最大路和走条数)
POJ2288Islands and Bridges(状态压缩DP,求最大路和走条数)
Islands and Bridges
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 8845 | Accepted: 2296 |
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.
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.
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
题意:有n个点,每个点都有一个价值,无论从哪个点走,要求每个点只能走一次,求出怎么走使得到的价值最大,且求出最大价值的路有多少条。假设有4个点,它的最大走法是1-->4-->2-->3,且1,4,2三点可以形成三角形,4,2,3也可以形成三角形,那么最大价值为:v[1]+v[4]+v[2]+v[3]+v[1]*v[4]+v[4]*v[2]+v[1]*v[4]*v[2]+v[2]*v[3]+v[4]*v[2]*v[3]。
解题:dp[state][i][j]表示状态state以j点结尾且j点的前一个点是i。具体的看代码。
#include<stdio.h> #include<string.h> #define FOR(i,l,r) for(i=l;i<=r;i++) #define mulit(j) (1<<j) typedef struct nnn { __int64 sum,k; }node; node dp[mulit(13)+5][14][14]; int map[14][14]; __int64 v[14]; int main() { int t,n,m,a,b,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); FOR(i,0,mulit(n)-1)//初始化 FOR(j,0,n-1) for(int e=0;e<n;e++) dp[i][j][e].sum=-1; memset(map,0,sizeof(map)); FOR(i,0,n-1) scanf("%I64d",&v[i]); while(m--) { scanf("%d%d",&a,&b); a--,b--; map[a][b]=map[b][a]=1; } if(n==1) { printf("%I64d %d\n",v[0],1); continue; } for( i=0;i<n;i++)//处理两个点的状态 for( j=i+1;j<n;j++) if(map[i][j]) { int state=mulit(i)+mulit(j); dp[state][i][j].sum=dp[state][j][i].sum=v[i]+v[j]+v[i]*v[j]; dp[state][i][j].k=dp[state][j][i].k=1; } for(int state=1;state<mulit(n);state++)//枚举状态,处理两个点以上 for( j=0;mulit(j)<=state;j++)//状态以j点结尾 if(state&mulit(j)) for( i=0;mulit(i)<=state;i++)//状态结尾点j的前一个点i,从i--->j. if(i!=j&&(mulit(i)&state)) { if(dp[state][i][j].sum==-1)continue;//没有该状态是从i--->j,以j为结尾点的状态 for(int e=0; e<n; e++)//找到一个点e,存在从j--->e,e没有走过,不在该状态 if((mulit(e)&state)==0&&map[j][e]) { int tstat=state+mulit(e),ss=0; if(map[i][e])//状态tstat以i,j,e三点结尾的可以形成三角形 ss=v[i]*v[j]*v[e]; if(dp[tstat][j][e].sum<dp[state][i][j].sum+v[e]+v[j]*v[e]+ss)//更新 { dp[tstat][j][e].sum=dp[state][i][j].sum+v[e]+v[j]*v[e]+ss; dp[tstat][j][e].k=dp[state][i][j].k; } else if(dp[tstat][j][e].sum==dp[state][i][j].sum+v[e]+v[j]*v[e]+ss) dp[tstat][j][e].k+=dp[state][i][j].k; } } __int64 maxsum=-1,k=0; for(i=0; i<n;i++) for(j=0;j<n;j++) if(i!=j&&dp[mulit(n)-1][i][j].sum!=-1) { if(dp[mulit(n)-1][i][j].sum>maxsum) { maxsum=dp[mulit(n)-1][i][j].sum; k=dp[mulit(n)-1][i][j].k; } else if(dp[mulit(n)-1][i][j].sum==maxsum) k+=dp[mulit(n)-1][i][j].k; } if(maxsum==-1)maxsum=0;//没有把所有的点只走一次就能全部点都走到 printf("%I64d %I64d\n",maxsum,k/2); /* k/2是因为:假设有4个点,最大走法是3-->1-->4-->2,那么2-->4-->1-->3也是最大走法, 但走法都是算一条走法,因为路的条数和走的方向没有关系。 */ } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。