您的位置:首页 > 其它

Islands and Bridges - POJ 2288 状压dp

2014-07-09 12:39 393 查看
Islands and Bridges

Time Limit: 4000MSMemory Limit: 65536K
Total Submissions: 8646Accepted: 2230
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
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


题意:找到权值最大的哈密顿圈,权值为路径上每个地点的价值,加上相邻两个点的价值的积,加上路径上能形成三角形的三个价值的积。

思路:状压dp。dp[s][i][j]表示在s状态下,最后两个地点是i和j的最大权值。way[s][i][j]表示其路径数量,且因为ij和ji是一样的,所以最后结果除以二。注意特殊判断只有一个点的情况。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
long long dp[1<<14][14][14],way[1<<14][14][14],maxv,maxw,value[14];
bool link[14][14];
int main()
{ int t,n,m,i,j,k,a,b,S,newS,S2,temp;
  scanf("%d",&t);
  while(t--)
  { scanf("%d%d",&n,&m);
    for(i=0;i<n;i++)
     scanf("%d",&value[i]);
    memset(dp,-1,sizeof(dp));
    memset(way,0,sizeof(way));
    memset(link,false,sizeof(link));
    for(i=1;i<=m;i++)
    { scanf("%d%d",&a,&b);
      a--;b--;
      link[a][b]=true;
      link[b][a]=true;
      dp[(1<<a)|(1<<b)][a][b]=value[a]+value[b]+value[a]*value[b];
      dp[(1<<a)|(1<<b)][b][a]=value[a]+value[b]+value[a]*value[b];
      way[(1<<a)|(1<<b)][a][b]=1;
      way[(1<<a)|(1<<b)][b][a]=1;
    }
    if(n==1)
    { printf("%d 1\n",value[0]);
      continue;
    }
    S2=(1<<n)-1;
    for(S=0;S<=S2;S++)
     for(i=0;i<n;i++)
     { if((S&(1<<i))==0)
        continue;
       for(j=0;j<n;j++)
       { if( i==j || (S&(1<<j))==0 || dp[S][i][j]<0)
          continue;
         for(k=0;k<n;k++)
         { if((S&(1<<k))==1 || !link[j][k])
            continue;
           temp=dp[S][i][j]+value[k]+value[j]*value[k];
           if(link[i][k])
            temp+=value[i]*value[j]*value[k];
           newS=S+(1<<k);
           if(temp>dp[newS][j][k])
           { dp[newS][j][k]=temp;
             way[newS][j][k]=way[S][i][j];
           }
           else if(temp==dp[newS][j][k])
            way[newS][j][k]+=way[S][i][j];
         }
       }
     }
    maxv=-1;
    maxw=0;
    for(i=0;i<n;i++)
     for(j=0;j<n;j++)
     { if(!link[i][j])
        continue;
       if(dp[S2][i][j]>maxv)
       { maxv=dp[S2][i][j];
         maxw=way[S2][i][j];
       }
       else if(dp[S2][i][j]==maxv)
        maxw+=way[S2][i][j];
     }
    if(maxv==-1)
     maxv=0;
    printf("%I64d %I64d\n",maxv,maxw/2);
  }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: