discription
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 和上一道斯坦纳树的很像啊,就是关键点一一对应变成了数量对应,其实总体上写法都差不多。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int f[][],n,m,T,ci[];
int ans[],dis[],k;
int q[],l,r,num,all;
int to[],ne[];
int val[],hd[];
bool iq[]; inline void init(){
num=;
memset(hd,,sizeof(hd));
memset(f,0x3f,sizeof(f));
memset(ans,0x3f,sizeof(ans));
memset(f[],,sizeof(f[]));
} int main(){
ci[]=;
for(int i=;i<=;i++) ci[i]=ci[i-]<<; scanf("%d",&T);
while(T--){
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++) f[ci[i-]][i]=;
for(int i=n-k+;i<=n;i++) f[ci[n+k-i]][i]=; int uu,vv,ww;
for(int i=;i<=m;i++){
scanf("%d%d%d",&uu,&vv,&ww);
to[++num]=vv,ne[num]=hd[uu],hd[uu]=num,val[num]=ww;
to[++num]=uu,ne[num]=hd[vv],hd[vv]=num,val[num]=ww;
} all=ci[k<<]-;
for(int S=;S<=all;S++){
for(int i=;i<=n;i++)
for(int s=S&(S-);s;s=(s-)&S) f[S][i]=min(f[S][i],f[s][i]+f[s^S][i]); for(int i=;i<=n;i++) dis[i]=f[S][i],iq[i]=,q[i]=i;
l=,r=n;
while(l<=r){
int x=q[l++];
for(int i=hd[x];i;i=ne[i]) if(dis[x]+val[i]<dis[to[i]]){
dis[to[i]]=dis[x]+val[i];
if(!iq[to[i]]) iq[to[i]]=,q[++r]=to[i];
}
iq[x]=;
} for(int i=;i<=n;i++) f[S][i]=dis[i];
} for(int s=;s<=all;s++){
int num1=,num2=;
for(int i=;i<=k;i++) if(s&ci[i-]) num1++;
for(int i=k+;i<=(k<<);i++) if(s&ci[i-]) num2++;
if(num1!=num2) continue; for(int i=;i<=n;i++) ans[s]=min(ans[s],f[s][i]);
} for(int S=;S<=all;S++)
for(int s=S&(S-);s;s=(s-)&S) ans[S]=min(ans[S],ans[s]+ans[s^S]); if(ans[all]!=ans[all+]) printf("%d\n",ans[all]);
else puts("No solution");
} return ;
}

HDOJ 4085 Peach Blossom Spring的更多相关文章

  1. HDU 4085 Peach Blossom Spring

    斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...

  2. HDU 4085 Peach Blossom Spring 斯坦纳树 状态压缩DP+SPFA

    状态压缩dp+spfa解斯坦纳树 枚举子树的形态 dp[i][j] = min(dp[i][j], dp[i][k]+dp[i][l]) 当中k和l是对j的一个划分 依照边进行松弛 dp[i][j]  ...

  3. hdu4085 Peach Blossom Spring

    Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...

  4. hdu4085 Peach Blossom Spring 斯坦纳树,状态dp

    (1)集合中元素表示(1<<i), i从0开始 (2)注意dp[i][ss] = min(dp[i][ss], dp[i][rr | s[i]] + dp[i][(ss ^ rr) | s ...

  5. HDU 4081 Peach Blossom Spring (最小生成树+dfs)

    题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大. 析:先求出最小生成树,然后再枚举每 ...

  6. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. hdu图论题目分类

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

  9. HDU图论题单

    =============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...

随机推荐

  1. makefile使用笔记(一)入门

    By francis_hao    Mar 2,2017 makefile makefile一个很简单的例子如下,该实例完成在执行make时,将main.c编译成可执行文件main的功能. 各项的含义 ...

  2. 编译 openssl 0.9.8zc 出现 error C2220: warning treated as error - no 'object' file generated

    remove the /WX option from CFLAG

  3. [05] css优先级

    1.优先级计算规则(特殊性) 在css中,有不同的方式编写css,如果想给同一个标签设置样式,选择器的写法有很多种,那么当多个样式都应用于同一个标签,标签优先选择哪个样式呢?按照以下规则: 现有 0, ...

  4. 图论:Stoer-Wagner算法

    利用Stoer-Wagner算法求无向图最小割 直接给出算法描述和过程实现: 算法步骤: . 设最小割cut=INF, 任选一个点s到集合A中, 定义W(A, p)为A中的所有点到A外一点p的权总和. ...

  5. shell之流程控制

    一.if语句.while.for循环各种小例题 1.用户验证 [root@bogon ~]# cat buer.sh #!/bin/bash #user='buer' #password='1234. ...

  6. php2go - Go 实现 PHP 常用内置函数

    [转]http://www.syyong.com/Go/php2go-Use-Golang-to-implement-PHP-s-common-built-in-functions.html 使用Go ...

  7. COGS2085 Asm.Def的一秒

    时间限制:1 s   内存限制:256 MB [题目描述] “你们搞的这个导弹啊,excited!” Asm.Def通过数据链发送了算出的疑似目标位置,几分钟后,成群结队的巡航导弹从“无蛤”号头顶掠过 ...

  8. ZOJ1450 Minimal Circle

    You are to write a program to find a circle which covers a set of points and has the minimal area. T ...

  9. bootstrap-table不分页时对数值类型数据的排序

    html中的代码 <table id="table"></table> sortData.json的数据如下 [ {"name":&qu ...

  10. 【LYOI2016】EasyRound1

    这可能是给新高一写的吧……随手写着玩玩…… A:随便模拟 #include<cstdio> #include<cstring> using namespace std; int ...