poj 2288 状态压缩dp Islands and Bridges
2014-08-09 10:40
567 查看
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.
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
Sample Output
Source
Shanghai 2004'
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int v[14];
__int64 dp[1<<14][14][14];
__int64 way[1<<14][14][14];
int Map[14][14];
int n,m;
int main()
{
__int64 Q;
int x,y;
int i,j;
int r,p,q;
int s;
int T;
int t;
scanf("%d",&T);
while(T--)
{
memset(Map,0,sizeof(Map));
memset(v,0,sizeof(v));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&v[i]);
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
Map[x-1][y-1]=Map[y-1][x-1]=1;
}
if(n==1)
{
printf("%d 1\n",v[0]);
continue;
}
memset(dp,-1,sizeof(dp));
memset(way,0,sizeof(way));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(j==i)
continue;
if(Map[j][i]==false)
continue;
t=(1<<j)|(1<<i);
dp[t][i][j]=v[i]*v[j]+v[i]+v[j];
way[t][i][j]=1;
}
for(s=0;s<(1<<n);s++)
{
for(r=0;r<n;r++)
{
if((s&(1<<r))==0)
continue;
for(p=0;p<n;p++)
{
if(r==p)
continue;
if((s&(1<<p))==0)
continue;
if(dp[s][r][p]==-1)
continue;
for(q=0;q<n;q++)
{
if(r==q)
continue;
if(p==q)
continue;
if(s&(1<<q))
continue;
if(Map[p][q]==0)
continue;
t=s|(1<<q);
Q=dp[s][r][p];
Q=Q+v[q]+v[p]*v[q];
if(Map[q][r])
Q+=v[r]*v[p]*v[q];
if(Q>dp[t][p][q])
{
dp[t][p][q]=Q;
way[t][p][q]=way[s][r][p];
}
else
if(Q==dp[t][p][q])
{
way[t][p][q]+=way[s][r][p];
}
}
}
}
}
__int64 sum=-1;
__int64 ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
continue;
if(sum<dp[(1<<n)-1][i][j])
{
sum=dp[(1<<n)-1][i][j];
ans=way[(1<<n)-1][i][j];
}
else
if(sum==dp[(1<<n)-1][i][j])
{
ans+=way[(1<<n)-1][i][j];
}
}
if(sum!=-1)
printf("%I64d %I64d\n",sum,ans/2);
else
printf("0 0\n");
}
return 0;
}/*
#include <stdio.h>
#include <string.h>
__int64 dp[14][14][1<<14];
__int64 way[14][14][1<<14];
int Map[14][14];
int v[14];
int n,m;
int main()
{
__int64 Q;
int j,k;
int t;
int x,y;
int i;
int s,r,p,q;
scanf("%d",&t);
while(t--)
{
memset(Map,0,sizeof(Map));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&v[i]);
}
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
Map[x-1][y-1]=Map[y-1][x-1]=1;
}
if (n==1)
{
printf("%d 1\n",v[0]);
continue;
}
memset(dp,-1,sizeof(dp));
memset(way,0,sizeof(way));
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i==j)
continue;
if(Map[i][j]==false)
continue;
int x=(1<<i)|(1<<j);
dp[i][j][x]=v[i]+v[j]+v[i]*v[j];
way[i][j][x]= 1;
}
}
for (s=0;s<(1<<n);s++)
{
for (r=0;r<n;r++)
{
if((s&(1<<r))==0)
continue;
for (p=0;p<n;p++)
{
if (r==p)
continue;
if((s&1<<p)==0)
continue;
if(dp[r][p][s]==-1)
continue;
for(q=0;q<n;q++)
{
if(r==q)
continue;
if(p==q)
continue;
if (s&1<<q)
continue;
if(Map[p][q]==0)
continue;
int x=s|(1<<q);
Q=dp[r][p][s]+v[q]+v[p]*v[q];
if(Map[r][q])
{
Q+=v[r]*v[p]*v[q];
}
if(Q>dp[p][q][x])
{
dp[p][q][x]=Q;
way[p][q][x]=way[r][p][s];
}
else
if(Q==dp[p][q][x])
{
way[p][q][x]+=way[r][p][s];
}
}
}
}
}
__int64 sum=-1;
__int64 ans=0;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i==j)
continue;
if(dp[i][j][(1<<n)-1]>sum)
{
sum=dp[i][j][(1<<n)-1];
ans=way[i][j][(1<<n)-1];
}
else
if(dp[i][j][(1<<n)-1]==sum)
{
ans+=way[i][j][(1<<n)-1];
}
}
}
if(sum==-1)
printf("0 0\n");
else
printf("%I64d %I64d\n", sum, ans/2);
}
return 0;
}*/
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.
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'
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int v[14];
__int64 dp[1<<14][14][14];
__int64 way[1<<14][14][14];
int Map[14][14];
int n,m;
int main()
{
__int64 Q;
int x,y;
int i,j;
int r,p,q;
int s;
int T;
int t;
scanf("%d",&T);
while(T--)
{
memset(Map,0,sizeof(Map));
memset(v,0,sizeof(v));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
scanf("%d",&v[i]);
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
Map[x-1][y-1]=Map[y-1][x-1]=1;
}
if(n==1)
{
printf("%d 1\n",v[0]);
continue;
}
memset(dp,-1,sizeof(dp));
memset(way,0,sizeof(way));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(j==i)
continue;
if(Map[j][i]==false)
continue;
t=(1<<j)|(1<<i);
dp[t][i][j]=v[i]*v[j]+v[i]+v[j];
way[t][i][j]=1;
}
for(s=0;s<(1<<n);s++)
{
for(r=0;r<n;r++)
{
if((s&(1<<r))==0)
continue;
for(p=0;p<n;p++)
{
if(r==p)
continue;
if((s&(1<<p))==0)
continue;
if(dp[s][r][p]==-1)
continue;
for(q=0;q<n;q++)
{
if(r==q)
continue;
if(p==q)
continue;
if(s&(1<<q))
continue;
if(Map[p][q]==0)
continue;
t=s|(1<<q);
Q=dp[s][r][p];
Q=Q+v[q]+v[p]*v[q];
if(Map[q][r])
Q+=v[r]*v[p]*v[q];
if(Q>dp[t][p][q])
{
dp[t][p][q]=Q;
way[t][p][q]=way[s][r][p];
}
else
if(Q==dp[t][p][q])
{
way[t][p][q]+=way[s][r][p];
}
}
}
}
}
__int64 sum=-1;
__int64 ans;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
continue;
if(sum<dp[(1<<n)-1][i][j])
{
sum=dp[(1<<n)-1][i][j];
ans=way[(1<<n)-1][i][j];
}
else
if(sum==dp[(1<<n)-1][i][j])
{
ans+=way[(1<<n)-1][i][j];
}
}
if(sum!=-1)
printf("%I64d %I64d\n",sum,ans/2);
else
printf("0 0\n");
}
return 0;
}/*
#include <stdio.h>
#include <string.h>
__int64 dp[14][14][1<<14];
__int64 way[14][14][1<<14];
int Map[14][14];
int v[14];
int n,m;
int main()
{
__int64 Q;
int j,k;
int t;
int x,y;
int i;
int s,r,p,q;
scanf("%d",&t);
while(t--)
{
memset(Map,0,sizeof(Map));
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&v[i]);
}
for(i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
Map[x-1][y-1]=Map[y-1][x-1]=1;
}
if (n==1)
{
printf("%d 1\n",v[0]);
continue;
}
memset(dp,-1,sizeof(dp));
memset(way,0,sizeof(way));
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i==j)
continue;
if(Map[i][j]==false)
continue;
int x=(1<<i)|(1<<j);
dp[i][j][x]=v[i]+v[j]+v[i]*v[j];
way[i][j][x]= 1;
}
}
for (s=0;s<(1<<n);s++)
{
for (r=0;r<n;r++)
{
if((s&(1<<r))==0)
continue;
for (p=0;p<n;p++)
{
if (r==p)
continue;
if((s&1<<p)==0)
continue;
if(dp[r][p][s]==-1)
continue;
for(q=0;q<n;q++)
{
if(r==q)
continue;
if(p==q)
continue;
if (s&1<<q)
continue;
if(Map[p][q]==0)
continue;
int x=s|(1<<q);
Q=dp[r][p][s]+v[q]+v[p]*v[q];
if(Map[r][q])
{
Q+=v[r]*v[p]*v[q];
}
if(Q>dp[p][q][x])
{
dp[p][q][x]=Q;
way[p][q][x]=way[r][p][s];
}
else
if(Q==dp[p][q][x])
{
way[p][q][x]+=way[r][p][s];
}
}
}
}
}
__int64 sum=-1;
__int64 ans=0;
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
if(i==j)
continue;
if(dp[i][j][(1<<n)-1]>sum)
{
sum=dp[i][j][(1<<n)-1];
ans=way[i][j][(1<<n)-1];
}
else
if(dp[i][j][(1<<n)-1]==sum)
{
ans+=way[i][j][(1<<n)-1];
}
}
}
if(sum==-1)
printf("0 0\n");
else
printf("%I64d %I64d\n", sum, ans/2);
}
return 0;
}*/
相关文章推荐
- POJ 2288 Islands and Bridges (TSP 状态压缩DP)
- poj 2288 Islands and Bridges解题报告-状态压缩dp
- poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题
- 状态压缩DP-Islandsand Bridges(POJ 2288)
- poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题
- poj 2288 Islands and Bridges_状态压缩dp_哈密尔顿回路问题
- POJ 2288 Islands and Bridges (状态压缩DP)
- 【汉密尔顿、DP|状态压缩】POJ-2288 Islands and Bridges
- poj 2288 Islands and Bridges解题报告-状态压缩dp
- poj 2288 Islands and Bridges 状态压缩dp(附详细注释)
- POJ 2288 Islands and Bridges 哈密尔顿路 状态压缩DP
- POJ 2288 Islands and Bridges(状态压缩)
- POJ-2288 Islands and Bridges 状态压缩DP TSP
- pku 2288 Islands and Bridges(状态压缩DP)
- DP:Islands and Bridges(POJ 2288)
- POJ 2288 Islands and Bridges(状压dp)
- POJ 2288 Islands and Bridges (状压DP)
- 状压DP poj 2288 Islands and Bridges
- PKU 2288 Islands and Bridges 状态dp
- POJ 2288 Islands and Bridges (状压DP)