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. 【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型

    最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那 ...

  2. codeforces 1015E1&&E2

    E1. Stars Drawing (Easy Edition) time limit per test 3 seconds memory limit per test 256 megabytes i ...

  3. Nginx 默认配置解析

    # For more information on configuration, see: # * Official English Documentation: http://nginx.org/e ...

  4. HDU 1395

    2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  5. wiki1285

    2013-09-21 16:50 裸 //By BLADEVIL var n :longint; i :longint; x, y :longint; t, tot :longint; key, s, ...

  6. vc6.0里使用lib(静态库)的方法

    vc6.0 中使用lib文件 使用库的方法如下:1. 包含库的头文件(把库的头文件包含到项目中)在应用程序工程中使用#include "file path"file path可以为 ...

  7. npm install 报node-sass错误

    Node Sass could not -bit with Node.js .x Found bindings for the following environments: - OS X -bit ...

  8. python判断操作系统

    https://www.crifan.com/python_get_current_system_os_type_and_version_info/ 参考:https://stackoverflow. ...

  9. web前端 CSS基础

    简单的CSS文件 <style type="text/css"> a{ color:rebeccapurple; font-size: larger; font-wei ...

  10. HTTP===http首部字段

    HTTP 首部字段 HTTP 首部字段是构成 HTTP 报文的要素之一.在客户端与服务器之间以 HTTP 协议进行通信的过程中,无论是请求还是响应都会使用首部字段,它能起到传递额外重要信息的作用. 使 ...