zoukankan      html  css  js  c++  java
  • 【状压dp】Islands and Bridges

    Islands and Bridges
    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 11034   Accepted: 2866

    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
    

    Source

     
    题目大意:有N个岛屿,M条边,经过所有的岛屿使点权和最大。对于一个点i和上一个点j的贡献是C[i]+C[j]+C[i]*C[j],特别地,对于构成三角形的一个点i和上一个点j、上上个点k对答案的特别贡献是C[i]*C[j]*C[k](在原来的基础上),并输出有多少条不同的路径(倒过来的两条路径算作一条)
    试题分析:dp[S][i][j]表示当前走过的集合是S,现在在i,上一个在j的最大点权和,直接写就好了。需要注意long long以及N=1的情况。
     
    代码:
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    #define LL long long
    inline LL read(){
    	LL x=0,f=1;char c=getchar();
    	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
    	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    	return x*f;
    }
    
    const int MAXN=100001;
    const int INF=999999;
    int N,M;
    int T; 
    LL C[101];
    bool e[101][101];
    LL dp[9001][15][15];
    LL num[9001][15][15];
    
    int main(){
    	T=read();
    	while(T--){
    		LL ans=0;
    		memset(e,false,sizeof(e));
    		memset(dp,-1,sizeof(dp));
    		memset(num,0,sizeof(num));
    		N=read(),M=read();
    		for(int i=1;i<=N;i++) C[i]=read();
    	    for(int i=1;i<=M;i++){
    	    	int u=read(),v=read();
    	    	e[u][v]=e[v][u]=true;
    		}
    		if(N==1){
    			printf("%lld %lld
    ",C[1],1);
    			continue;
    		}
    		for(int i=1;i<=N;i++) 
    		    for(int j=1;j<=N;j++)
    		        if(i!=j&&e[i][j]) {
    				    dp[(1<<(i-1))+(1<<(j-1))][i][j]=(long long)C[i]+C[j]+C[i]*C[j];
    				    num[(1<<(i-1))+(1<<(j-1))][i][j]=1;
    				}
    		for(int i=0;i<(1<<N);i++){
    			for(int j=1;j<=N;j++){
    			    if(!((i>>(j-1))&1)) continue;
    			    for(int k=1;k<=N;k++){
    			    	if(k==j||!e[k][j]||!((i>>(k-1))&1)) continue;
    			    	for(int p=1;p<=N;p++){
    			    		if(!((i>>(p-1))&1)||p==k||p==j||!e[p][k]) continue;
    			    		if(dp[i-(1<<(j-1))][k][p]==-1) continue;
    			    		LL t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j];
    			    		if(e[p][j]) t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j]*C[k]*C[p]+C[j];
    			    		if(t1>dp[i][j][k]){
    			    			dp[i][j][k]=t1;
    			    			num[i][j][k]=num[i-(1<<(j-1))][k][p];
    						}
    						else if(dp[i][j][k]==t1) num[i][j][k]+=num[i-(1<<(j-1))][k][p];
    				    }
    				} 
    			}
    		}
    		LL tmp=0;
    		for(int i=1;i<=N;i++) 
    		    for(int j=1;j<=N;j++) 
    			    if(i!=j) 
    				    if(ans<dp[(1<<N)-1][i][j]) ans=dp[(1<<N)-1][i][j],tmp=num[(1<<N)-1][i][j];
    				    else if(ans==dp[(1<<N)-1][i][j]) tmp+=num[(1<<N)-1][i][j];
    		printf("%lld %lld
    ",ans,tmp/2);
    	}
    }
    

      

  • 相关阅读:
    XAML实例教程系列 依赖属性和附加属性
    分享Silverlight/Windows8/WPF/WP7/HTML5周学习导读(6月4日6月10日)
    QT GUI基本布局
    mqtt client libraries for c
    QT sqlite相关操作
    navicat 激活工具激活时必须断网 ,如果没有断网激活 激活过程中报如下错误 请卸载navicat 重新安装再行激活操作
    vmware 16 windows7企业版 tools安装不了 驱动签名验证
    虚拟机复制
    Install systemtap on Ubuntu 12.04
    DevOps的各个阶段
  • 原文地址:https://www.cnblogs.com/wxjor/p/7266112.html
Copyright © 2011-2022 走看看