Peach Blossom Spring
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
题解
斯坦纳树板题
但是有可能不连通(好像叫斯坦纳森林)
我们先把2*k个点做一个斯坦纳树
当居住点和隐蔽点相等时,这个连通块是合法的
我们把这些合法的状态拿出来,再做一个简单状压DP即可
(注意,相等的时候选的两组点的集合可能是不同的,所以不能用g[1<<5]来DP,否则一直WA)
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 55
#define M 1005
const int INF=0x3f3f3f3f;
int fir[N],to[2*M],nxt[2*M],cd[2*M],cnt;
int f[N][1<<10],g[1<<10],con[1<<10];
bool inq[N];
queue<int> q;
void adde(int a,int b,int c)
{
to[++cnt]=b;nxt[cnt]=fir[a];fir[a]=cnt;cd[cnt]=c;
to[++cnt]=a;nxt[cnt]=fir[b];fir[b]=cnt;cd[cnt]=c;
}
int main()
{
int T,n,m,k,i,u,v,w,p;
int all,s,t,hf;
for(i=1;i<=1023;i++)con[i]=con[i>>1]+(i&1);
scanf("%d",&T);
while(T--){
memset(f,0x3f,sizeof(f));
memset(g,0x3f,sizeof(g));
memset(fir,0,sizeof(fir));
cnt=0;
scanf("%d%d%d",&n,&m,&k);
for(i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);
}
all=(1<<(2*k))-1;
for(i=1;i<=n;i++)f[i][0]=0;
for(i=1;i<=k;i++)f[i][1<<(i-1)]=0;
for(i=1;i<=k;i++)f[n-k+i][1<<(k+i-1)]=0;
for(s=1;s<=all;s++){
for(i=1;i<=n;i++)
for(t=(s-1)&s;t;t=(t-1)&s)
f[i][s]=min(f[i][t]+f[i][s^t],f[i][s]);
memset(inq,0,sizeof(inq));
for(i=1;i<=n;i++)if(f[i][s]<INF)q.push(i),inq[i]=1;
while(!q.empty()){
u=q.front();q.pop();inq[u]=0;
for(p=fir[u];p;p=nxt[p]){
v=to[p];
if(f[u][s]+cd[p]<f[v][s]){
f[v][s]=f[u][s]+cd[p];
if(!inq[v])q.push(v),inq[v]=1;
}
}
}
}
hf=(1<<k)-1;
for(s=1;s<=all;s++){
if(con[s>>k]!=con[s&hf])continue;
for(i=1;i<=n;i++)
g[s]=min(g[s],f[i][s]);
}
for(s=1;s<=all;s++)
for(t=(s-1)&s;t;t=(t-1)&s)
g[s]=min(g[s],g[t]+g[s^t]);
if(g[all]==INF)printf("No solution\n");
else printf("%d\n",g[all]);
}
}