渐渐对状态DP有点头绪了,这一题要我们求存不存在一条路线,经过每一个点一次,如果存在那么求出最大值,并且求出有多少种走法。
这一题数据要用long long或者__int64保存,另外求出的走法要除以2,并且注意n=1的情况
状态转移方程 dp[s][i][j]=max(dp[s'][j][k]+point,dp[s][i][j]) 此处dp[s][i][j] 表示状态s 最后处于i,之前处于j 的情况下的最大评分。
num[s][i][j]=num[s][i][j]+num[s'][j][k] ; dp[s'][j][k]+point==dp[s][i][j]
num[s][i][j]=num[s'][j][k]; dp[s'][j][k]>dp[s][i][j];
Islands and Bridges
|
Time Limit: 4000MS |
|
Memory Limit: 65536K |
|
Total Submissions: 7256 |
|
Accepted: 1847 |
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
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #include<string> #include<bitset> using namespace std; #define INF 0xFFFFFF int n,m; long long dp[1<<14][14][14]; long long num[1<<14][14][14]; int maps[14][14]; int v[14]; bool jud(int s,int i) { if(i==0) return true; if(s&(1<<(i-1))) return true; return false; } void solve() { int x,y,tmps,tmp; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&v[i]); memset(maps,0,sizeof(maps)); for(int i=1;i<=m;i++) { scanf("%d%d",&x,&y); maps[x][y]=maps[y][x]=1; } if(n==1) { cout<<v[1]<<" "<<1<<endl; return; } memset(dp,-1,sizeof(dp)); memset(num,0,sizeof(num)); for(int i=1;i<=n;i++) { int j=0; j|=(1<<(i-1)); dp[j][i][0]=v[i]; num[j][i][0]=1; } for(int i=0;i<(1<<n);i++) { for(int j=1;j<=n;j++) { if(!jud(i,j)) continue; tmps=i^(1<<(j-1)); for(int k=1;k<=n;k++) { if(k==j) continue; if(!jud(i,k)) continue; if(!maps[k][j]) continue; for(int q=0;q<=n;q++) { if(q==k || q==j) continue; if(!maps[q][k] && q) continue; if(!jud(tmps,q)) continue; if(dp[tmps][k][q]==-1) continue; tmp=v[j]+v[j]*v[k]+dp[tmps][k][q]; if(maps[q][j]) tmp+=v[q]*v[j]*v[k]; if(tmp==dp[i][j][k]) num[i][j][k]+=num[tmps][k][q]; if(tmp>dp[i][j][k]) { dp[i][j][k]=tmp; num[i][j][k]=num[tmps][k][q]; } } } } } long long ans1=-1,ans2=0; for(int s=(1<<n)-1,i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i==j) continue; if(ans1==dp[s][i][j]) ans2+=num[s][i][j]; if(dp[s][i][j]>ans1) { ans1=dp[s][i][j]; ans2=num[s][i][j]; } } } if(n==1) { cout<<ans1<<" "<<1<<endl; return; } if(ans1==-1) printf("0 0\n"); else cout<<ans1<<" "<<ans2/2<<endl; } int main() { int t; scanf("%d",&t); while(t--) solve(); return 0; }
本文介绍了一种使用状态动态规划解决特定图论问题的方法:寻找一条最佳三角形Hamilton路径,即经过每个顶点恰好一次并最大化路径值的路径。文章详细阐述了解决方案的实现过程,包括状态定义、状态转移方程以及代码示例。
1030

被折叠的 条评论
为什么被折叠?



