poj 2288 状态压缩dp
来源:互联网 发布:nba个人数据 编辑:程序博客网 时间:2024/06/06 01:13
Description
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
Output
Note: A path may be written down in the reversed order. We still think it is the same path.
Sample Input
23 32 2 21 22 33 14 61 2 3 41 21 31 42 32 43 4
Sample Output
22 369 1
【题目大意】求汉密尔顿的一道变形问题,中间每个点有权值,关于最后得分的描述如下
Suppose there are n islands. The value of aHamilton path C1C2...Cn is calculated as the sum of three parts. Let Vi be thevalue for the island Ci. As the first part, we sum over all the Vi values foreach 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 consecutiveislands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is abridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+2.
这题要求让得分最高
【解析】发现每个点的状态由前面两个点确定,用DP(S,A,B)表示状态为S时,当前到达A,而上一个点是B时的最大得分,这个状态由DP(S',B,C)通过从B走到A得到,S'=S-(1<<A),即S'状态就是经过B和C但不经过A的一个状态,C是不同于A和B的一个点。
【状态转移】dp[S][A][B] =max(dp[S][A][B],dp[S'][B][C]+temp) 这里的temp指的是加上的得分即Vb*Va+Va,如果构成三角关系(即A和C间有边),temp就要再加上Vb*Va*Vc.
【边界条件】DP((1<<A)+(1<<B),A,B)=Va+Vb+Va*Vb(A和B间有边)表示
- Source Code
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;long long int dp[1<<14][14][14];int edge[15][15];int ans;long long int way[1<<14][14][14];long long int val[15];int main(){ int i, j, k, l, m, n, s; int t; cin >> t; while( t-- ) { cin>> n >> m; memset(edge,0,sizeof(edge)); memset(dp,-1,sizeof(dp)); memset(way,0,sizeof(way)); ans = -1; for ( i = 1; i <= n; i++ ) { cin >> val[i]; } for ( i = 1; i <= m; i++ ) { cin >> j>> k; edge[j][k] = edge[k][j] = 1; } if(n==1) { cout << val[1] << " " << 1 <<endl; continue; } for ( i = 1; i <= n; i++ ) { for ( j = 1; j <= n; j++ )//此处可优化 { if(i == j || edge[i][j] == 0) continue; int ii = 1<<(i-1); int jj = 1<<(j-1); s = ii + jj; dp[s][i][j] = val[i] + val[j] + val[i]*val[j]; way[s][i][j] = 1; //cout<<s<<" "<<i<<" "<<j<<" "<<dp[s][i][j]<<endl; } } for ( s = 0; s < (1<<n); s++ ) { for ( i = 1; i <= n; i++ ) { if((s & (1 << (i-1))) == 0) continue; for ( j = 1; j <= n; j++ ) { if((s & (1<<(j-1))) ==0||i == j || edge[i][j] == 0) continue; for ( k = 1; k <= n; k++ ) { if( i == k || j == k ||(s & (1<<(k-1))) == 0 || edge[j][k] == 0) continue; int s1 = s - (1<<i-1); if(dp[s1][j][k] == -1) continue; long long int tmp = val[i] + val[j]*val[i] + dp[s1][j][k]; if( edge[i][k] ) tmp += val[i]*val[j]*val[k]; // dp[s][i][j] = max(dp[s][i][j],dp[s1][j][k] + tmp); if( dp[s][i][j] < tmp) { dp[s][i][j] = tmp; way[s][i][j] = way[s1][j][k]; } else if( dp[s][i][j] == tmp ) { way[s][i][j] += way[s1][j][k]; } // cout << s<< " "<<i<<" "<<j<<" "<<dp[s][i][j]<<endl; } } } } long long int sum = 0; int end = (1<<n)-1; for ( i = 1; i <= n; i++ ) { for ( j = 1; j <= n; j++ ) { if( i == j ) continue; if(ans < dp[end][i][j]) { ans = dp[end][i][j]; sum = way[end][i][j]; } else if(ans == dp[end][i][j]) { sum += way[end][i][j]; } } } if(ans == -1) { ans = sum = 0; } cout << ans << " " << sum/2 <<endl; }}
- poj 2288 状态压缩dp
- poj 2288(状态压缩dp + TSP问题)
- POJ 2288(状态压缩dp)
- 【状态压缩DP】POJ 1170
- 【状态压缩DP】POJ 1185
- POJ 3254 状态压缩DP
- poj 3254 状态压缩dp
- POJ 2923(dp + 状态压缩)
- poj 3254 状态压缩DP
- poj 1185(状态压缩dp)
- poj 3254(状态压缩dp)
- poj 3311(状态压缩dp)
- poj-2411-状态压缩DP
- POJ 3254 状态压缩DP
- Poj 2404 状态压缩DP
- poj 2411 状态压缩dp
- poj 2411 状态压缩DP
- poj 1038 状态压缩dp
- android服务组件之生命周期1
- URAL 1647 Divide an Island!
- sleep和wait的区别
- URAL 1648 Yachts
- dynamic与var
- poj 2288 状态压缩dp
- Android学习:Activity四种启动模式
- Linux下高并发socket最大连接数所受的各种限制
- vector最基本的常识
- [iOS6]如何在Xcode6设置UIView的圆角显示
- UVALive 6172 Hailstone HOTPO (水
- android service 生命周期2
- 【android】SurfaceFlinger (surface composer)
- GIT远程仓库的使用