HDU4085 Peach Blossom Spring

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 
 
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

正解:斯坦纳树

解题报告:

  斯坦纳树的略微加强版——斯坦纳森林。

  考虑有可能最优方案是一个森林,我们先求出斯坦纳树的f数组之后,再用g[s]表示连通状态至少为s的最小代价,然后枚举一个子集,将若干个斯坦纳树合并,注意只能合并合法的状态!

  也就是前k位和后k位1的个数相等的状态!

  最后,不要像我一样忘记判no solution…

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
const int MAXN = 52;
const int MAXM = 2017;
const int MAXS = 1024;
int n,m,k,ecnt,f[MAXN][MAXS],S,head,tail,dui[MAXM*100];
int first[MAXN],to[MAXM],Next[MAXM],w[MAXM],g[MAXS];
bool vis[MAXN];
inline void link(int x,int y,int z){ Next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; w[ecnt]=z; }
inline int calc(int s){ int tot=0; while(s) tot+=s&1,s>>=1; return tot; }
inline int getint(){
    int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
    if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
}

inline void SPFA(int s){
	head=tail=0; for(int i=1;i<=n;i++) if(f[i][s]!=f[0][0]) dui[++tail]=i,vis[i]=1;
	int u;
	while(head<tail) {
		head++; u=dui[head]; vis[u]=0;
		for(int i=first[u];i;i=Next[i]) {
			int v=to[i];
			if(f[v][s]>f[u][s]+w[i]) {
				f[v][s]=f[u][s]+w[i];
				if(!vis[v]) { vis[v]=1; dui[++tail]=v; }
			}
		}
	}
}

inline void work(){
	int T=getint(); int x,y,z,now;
	while(T--) {
		n=getint(); m=getint(); k=getint();
		ecnt=0; memset(first,0,sizeof(first));
		for(int i=1;i<=m;i++) {
			x=getint(); y=getint(); z=getint();
			link(x,y,z); link(y,x,z);
		}
		memset(f,0x3f,sizeof(f)); S=(1<<(k*2))-1;
		for(int i=1;i<=k;i++) f[i][(1<<(i-1))]=0;
		for(int i=n-k+1;i<=n;i++) f[i][(1<<(i+k*2-n-1))]=0;
		for(int s=1;s<=S;s++) {
			for(int i=1;i<=n;i++) {
				for(int from=(s-1)&s;from;from=(from-1)&s) {
					if(f[i][from]==f[0][0] || f[i][s-from]==f[0][0]) continue;
					now=f[i][from]+f[i][s-from];
					if(f[i][s]>now) f[i][s]=now;
				}
			}
			SPFA(s);
		}

		memset(g,0x3f,sizeof(g)); int ss=(1<<k)-1;
		//可能是森林,考虑将几棵树组合
		for(int s=1;s<=S;s++) {
			if(calc(s&ss)!=calc(s>>k)) continue;
			for(int i=1;i<=n;i++)
				g[s]=min(g[s],f[i][s]);
		}

		for(int s=1;s<=S;s++) {
			for(int from=(s-1)&s;from;from=(from-1)&s) {
				if(g[from]==g[0] || g[s-from]==g[0]) continue;
				g[s]=min(g[s],g[from]+g[s-from]);
			}
		}
		if(g[S]==g[0]) puts("No solution");
		else printf("%d
",g[S]);
	}
}

int main()
{
    work();
    return 0;
}

  

原文地址:https://www.cnblogs.com/ljh2000-jump/p/6418474.html