Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 9023 | Accepted: 2338 |
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 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
Output
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
【题目大意】求汉密尔顿的一道变形问题,中间每个点有权值,关于最后得分的描述如下
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; } }