首页 > ACM题库 > HDU-杭电 > HDU 1668 Islands and Bridges -动态规划-[解题报告] C++
2013
12-21

HDU 1668 Islands and Bridges -动态规划-[解题报告] C++

Islands and Bridges

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

22 3
69 1

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<bitset>

using namespace std;

#define INF 0xFFFFFF 

int n,m;
long long  dp[1<<14][14][14];
long long num[1<<14][14][14];
int maps[14][14];
int v[14];

bool jud(int s,int i)
{
	if(i==0) return true;
	if(s&(1<<(i-1))) return true;
	return false;
}

void solve()
{
	int x,y,tmps,tmp;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&v[i]);
	memset(maps,0,sizeof(maps));
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x,&y);
		maps[x][y]=maps[y][x]=1;
	}
	if(n==1)
	{
		cout<<v[1]<<" "<<1<<endl;
		return;
	}
	memset(dp,-1,sizeof(dp));
	memset(num,0,sizeof(num));
	for(int i=1;i<=n;i++)
	{
		int j=0;
		j|=(1<<(i-1));
		dp[j][i][0]=v[i];
		num[j][i][0]=1;
	}
	for(int i=0;i<(1<<n);i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(!jud(i,j)) continue;
			tmps=i^(1<<(j-1));
			for(int k=1;k<=n;k++)
			{
				if(k==j) continue;
				if(!jud(i,k)) continue;
				if(!maps[k][j]) continue;
				for(int q=0;q<=n;q++)
				{
					if(q==k || q==j) continue;
					if(!maps[q][k] && q) continue;
					if(!jud(tmps,q)) continue;
					if(dp[tmps][k][q]==-1) continue;
					tmp=v[j]+v[j]*v[k]+dp[tmps][k][q];
					if(maps[q][j]) tmp+=v[q]*v[j]*v[k];
					if(tmp==dp[i][j][k])
						 num[i][j][k]+=num[tmps][k][q];
					if(tmp>dp[i][j][k])
					{
						dp[i][j][k]=tmp;
						num[i][j][k]=num[tmps][k][q];	
					}
				}
			}
		}
	}
	long long ans1=-1,ans2=0;
	for(int s=(1<<n)-1,i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(ans1==dp[s][i][j])
				ans2+=num[s][i][j];
			if(dp[s][i][j]>ans1)
			{
				ans1=dp[s][i][j];
				ans2=num[s][i][j];
			}
		}
	}
	if(n==1)
	{
		cout<<ans1<<" "<<1<<endl;
		return;
	}
	if(ans1==-1) printf("0 0\n");
	else 
		cout<<ans1<<" "<<ans2/2<<endl; 
}

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
		solve();
	return 0;
}

解题报告转自:http://blog.csdn.net/lenleaves/article/details/7981788


  1. [email protected]

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。

  3. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  4. Good task for the group. Hold it up for every yeara??s winner. This is a excellent oppotunity for a lot more enhancement. Indeed, obtaining far better and much better is constantly the crucial. Just like my pal suggests on the truth about ab muscles, he just keeps obtaining much better.

  5. Gucci New Fall Arrivals

    This is really nice to know. I hope it will be successful in the future. Good job on this and keep up the good work.