hdu 4085 Peach Blossom Spring(斯坦纳树)


Peach Blossom Spring

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2368    Accepted Submission(s): 906


Problem Description

Tao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous works is "Peach Blossom Spring", which is a fable about a chance
discovery of an ethereal village where the people lead an ideal existence in harmony with nature, unaware of the outside world for centuries. So in Chinese, "Peach Blossom Spring" means "utopia".
In the story of "Peach Blossom Spring", there was a mysterious place. In Qin dynasty, some people escaped to that place during the civil unrest and built a village. They and their descendants never left and never had any contact with the outside world since then, until centuries latter a fisherman of Jin dynasty found them.
Recently, some Chinese ACMers happened to find the relics of the village mentioned in"Peach Blossom Spring".
They also found a document about building hiding places to escape from Qin army. The document said:
There were n houses and m roads in the village. Each road connected two houses. These houses were numbered from 1 to n. There were k families, each living in a different house. 
The houses they lived were house 1, house 2, … , house k. There were also k broken houses: house n-k+1, house n-k+2, ... , house n, with secret basements so that those houses could be used as hiding places.
The problem was that all roads were broken. People wanted to repair some roads so that every family could reach a hiding place through the repaired roads. Every hiding place could only hold one family. Each road cost some labor to be repaired. The head of the village wanted to find out the minimum cost way of repairing the roads, but he didn't know how to do.
Would you solve the problem which the ancient village head never solved?
 

Input
The input begins with a line containing an integer T(T<=50), the number of test cases. For each case, the first line begins with three integers ---- the above mentioned n (4<=n<=50), m (0<=m<=1000) and k (1<=k<=5, 2k<=n). Then m lines follow, each containing three integers u,v and w, indicating that there is a broken road connecting house u an d v, and the cost to repair that road is w(1<=w<=1000).
 

Output
For each test case, if you cannot find a proper way to repair the roads, output a string "No solution" in a line. Otherwise, output the minimum cost to repair the roads in a line.
 

Sample Input
      
      
2 4 3 1 4 2 10 3 1 9 2 3 10 6 7 2 1 5 1000 2 6 1000 1 3 1 2 3 1 3 4 1 4 5 1 4 6 1
 

Sample Output
      
      
29 5
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4089  4081  4082  4090  4087 
 

Statistic |  Submit |  Discuss |  Note


题目大意:给你n,m,k ,分别表示有n个点,m条边,每条边有一个权值,表示修复这条边需要的代价,从前k个点中任取一个使其和后k个点中的某一个点,通过边连接,并且必须是一一对应,问最小的代价是多少。

题解:刚开始的时候看到某些点必须选并使其联通,所以想到了斯坦纳树,但是需要注意的是这道题是让k 对点满足一一对应的联通关系,也就是说最后生成的不一定是一颗,而有可能是一片森林。那么我们可以先按照斯坦纳树的方式来进行DP,求出所有的状态,然后再进行一次状压dp,dp[sta]=min(dp[sta],dp[s]+dp[sta-s]),用两棵树表示当前状态。

#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<algorithm>
#include<queue>  
#include<cmath>  
#define N 6003  
#define inf 1000000000  
using namespace std;  
int n,m,k,tot;  
int point[N],next1[N],v[N],len[N];  
int f[53][(1<<11)],mi[20],can[N],dp[(1<<11)];  
queue<int> p;
void add(int x,int y,int z)  
{  
    tot++; next1[tot]=point[x]; point[x]=tot; v[tot]=y; len[tot]=z;  
    tot++; next1[tot]=point[y]; point[y]=tot; v[tot]=x; len[tot]=z;  
}  
void spfa(int sta)
{
	while(!p.empty())
	{
		int now=p.front(); p.pop();
		for (int i=point[now];i;i=next1[i])
		{
			if (f[v[i]][sta]>f[now][sta]+len[i])
			{
				f[v[i]][sta]=f[now][sta]+len[i];
				if (!can[v[i]]){
					can[v[i]]=1;
					p.push(v[i]);
				}
			}
		}
		can[now]=0;
	}
}
bool check(int sta)//判断当前的状态是否满足一一对应关系
{
	int ans=0;
	for (int i=0;i<k;i++)
	{
	  if (sta&(1<<i))  ans++;
	  if (sta&(1<<(i+k))) ans--;
	}
	return (ans==0);
}
int main()  
{  
    int t; scanf("%d",&t);  
    mi[0]=1;  
    for (int i=1;i<=11;i++)  mi[i]=mi[i-1]*2; 
    for (int T=1;T<=t;T++)  
    {  
        scanf("%d%d%d",&n,&m,&k);  
        tot=0; 
        memset(point,0,sizeof(point));
        memset(next1,0,sizeof(next1));
		for (int i=1;i<=m;i++)  
        {  
            int x,y,z;  scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);  
        }  
        for (int i=1;i<=n;i++)  
         for (int j=0;j<mi[10];j++)
		   f[i][j]=inf;
		for (int i=1;i<=k;i++)
		  f[i][mi[i-1]]=0;
		int t=k;
		for (int i=n-k+1;i<=n;i++)
		 f[i][mi[t]]=0,t++;
		for (int sta=1;sta<mi[t];sta++)
		{
			for (int i=1;i<=n;i++)
			{
				for (int s=sta&(sta-1);s;s=sta&(s-1))
				{
					int t=f[i][sta-s]+f[i][s];
					f[i][sta]=min(f[i][sta],t);
				}
				if (f[i][sta]!=inf)  p.push(i),can[i]=1;
			}
			spfa(sta);
		}  
		for (int sta=1;sta<mi[t];sta++)
		{
			dp[sta]=inf;
			for (int i=1;i<=n;i++)
			 dp[sta]=min(dp[sta],f[i][sta]);
		}
		for (int sta=1;sta<mi[t];sta++)
		 if (check(sta))
		  for (int s=sta&(sta-1);s;s=sta&(s-1))
		   if (check(s))  dp[sta]=min(dp[sta],dp[s]+dp[sta-s]);
		if (dp[mi[t]-1]==inf)  printf("No solution\n");
		else   printf("%d\n",dp[mi[t]-1]);
    }  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值