• [poj2288] Islands and Bridges (状压dp)


    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 ViVi+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 ViVi+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

    Solution

    一看数据(13)就知道是状压。。。
    分析题目发现需要知道前两个岛是什么,那就暴力枚举就好了
    最后统计最大值与对应方案(方案数跟据题目要求要/2)
    注意特判1qwq

    Code

    //By Menteur_Hxy
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define M(a,b) memset(a,(b),sizeof(a))
    #define F(i,a,b) for(register int i=(a);i<=(b);i++)
    using namespace std;
    typedef long long LL;
    
    LL read() {
    	LL x=0,f=1; char c=getchar();
    	while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
    	while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
    	return x*f;
    }
    
    int n,m;
    int val[13],edg[13][13];
    LL dp[1<<13][13][13],num[1<<13][13][13];
    
    int main() {
    	int T=read();
    	while(T--) {
    		M(edg,0);M(dp,-1);M(num,0);
    		n=read(),m=read();
    		F(i,0,n-1) val[i]=read();
    		if(n==1) {printf("%d 1
    ",val[0]);continue;}// WA*1
    		F(i,1,m) {int u=read()-1,v=read()-1;
    			edg[u][v]=edg[v][u]=1;
    		}
    		F(i,0,n-1) F(j,0,n-1) if(i!=j && edg[i][j])
    			dp[(1<<i)|(1<<j)][i][j]=val[i]+val[j]+val[i]*val[j],num[(1<<i)|(1<<j)][i][j]=1;
    		F(i,0,(1<<n)-1) F(j,0,n-1) if((1<<j)&i) 
    			F(k,0,n-1) if(edg[j][k] && j!=k && (i&(1<<k)) && dp[i][j][k]!=-1) 
    				F(x,0,n-1) if(edg[k][x] && k!=x && j!=x && !(i&(1<<x))) {
    					int tmp=dp[i][j][k]+val[x]+val[k]*val[x];
    					if(edg[j][x]) tmp+=val[j]*val[k]*val[x];
    					if(dp[i|(1<<x)][k][x]<tmp) {
    						dp[i|(1<<x)][k][x]=tmp;
    						num[i|(1<<x)][k][x]=num[i][j][k];
    					} else if(dp[i|(1<<x)][k][x]==tmp) 
    						num[i|(1<<x)][k][x]+=num[i][j][k];
    				}
    		LL ans1=0,ans2=0;
    		F(i,0,n-1) F(j,0,n-1) if(i!=j && edg[i][j]) {
    			if(ans1<dp[(1<<n)-1][i][j]) ans1=dp[(1<<n)-1][i][j],ans2=num[(1<<n)-1][i][j];
    			else if(ans1==dp[(1<<n)-1][i][j]) ans2+=num[(1<<n)-1][i][j];
    		}
    		printf("%lld %lld
    ",ans1,ans2>>1);
    	}
    	return 0;
    }
    
    版权声明:本文为博主原创文章,未经博主允许不得转载。 博主:https://www.cnblogs.com/Menteur-Hxy/
  • 相关阅读:
    使用BackgroundWorker组件进行异步操作编程《转》
    C#多线程控制进度条之长任务操作《转》
    模态进度条窗体实现<转>
    dev xtraReports使用《转》
    客户端IP
    WebService获取服务端硬件信息和客户端IP,MAC,浏览器信息,所在城市《转》
    c#多线程 Invoke方法的使用<转>
    C# windowform进度条《转》
    XtraReports 打印控件的简单使用《转》
    hdu Marriage Match II
  • 原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9364100.html
Copyright © 2020-2023  润新知