Peach Blossom Spring

http://acm.hdu.edu.cn/showproblem.php?pid=4085

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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 
 
题意:让前k个点分别与后k个点联通的最小代价
因为是分别连通,所以是斯坦纳森利
合并的时候判断是否前面的点与后面的点相等
#include<queue>
#include<cstdio>
#include<cstring>
#define M (1<<10)+2
using namespace std;
int n,m,k,t,now,mx;
int front[],to[],nxt[],tot,val[];
int f[][M],g[M];
bool v[];
int sum;
queue<int>q;
void add(int u,int v,int w)
{
to[++tot]=v;nxt[tot]=front[u];front[u]=tot; val[tot]=w;
to[++tot]=u;nxt[tot]=front[v];front[v]=tot; val[tot]=w;
}
bool check(int s)
{
sum=;
for(int i=;i<=k;i++)
if(s&(<<(i-))) sum++;
for(int i=k+;i<=mx;i++)
if(s&(<<(i-))) sum--;
return !sum;
}
int main()
{
scanf("%d",&t);
int u,vv,w;
while(t--)
{
memset(front,,sizeof(front));
tot=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=m;i++)
{
scanf("%d%d%d",&u,&vv,&w);
add(u,vv,w);
}
memset(f,0x3f3f3f3f,sizeof(f));
memset(g,0x3f3f3f3f,sizeof(g));
mx=k<<;
for(int i=;i<=k;i++) f[i][<<(i-)]=;
for(int i=k+;i<=mx;i++) f[n-(mx-i)][<<(i-)]=;
for(int S=;S<(<<mx);S++)
{
for(int i=;i<=n;i++)
{
for(int T=S-;T;T=(T-)&S)
f[i][S]=min(f[i][S],f[i][T]+f[i][S^T]);
if(f[i][S]<f[][]) q.push(i),v[i]=true;
}
while(!q.empty())
{
now=q.front(); q.pop(); v[now]=false;
for(int i=front[now];i;i=nxt[i])
if(f[to[i]][S]>f[now][S]+val[i])
{
f[to[i]][S]=f[now][S]+val[i];
if(!v[to[i]])
{
q.push(to[i]);
v[to[i]]=true;
}
}
}
if(!check(S)) continue;
for(int i=;i<=n;i++) g[S]=min(g[S],f[i][S]);
for(int T=S-;T;T=(T-)&S) g[S]=min(g[S],g[T]+g[S^T]);
}
if(g[(<<mx)-]==0x3f3f3f3f) printf("No solution\n");
else printf("%d\n",g[(<<mx)-]);
} }

hdu4085 Peach Blossom Spring的更多相关文章

  1. 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 ...

  2. HDOJ 4085 Peach Blossom Spring

    discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...

  3. HDU 4085 Peach Blossom Spring

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

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

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

  5. 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]  ...

  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. 【HDOJ图论题集】【转】

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

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. Appium基础环境搭建(windows)---基于python

    1  JDK安装 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 安装注意:安装 ...

  2. POJ 3845 Fractal(计算几何の旋转缩放)

    Description Fractals are really cool mathematical objects. They have a lot of interesting properties ...

  3. HDU 4568 Hunter(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description One day, a hunter named James went to a mysterious area to find the treasures. J ...

  4. Graph Theory

    Description Little Q loves playing with different kinds of graphs very much. One day he thought abou ...

  5. 2017秋-软件工程第四次作业(2)-结对使用TDD框架完成单元测试

    第一次接触“单元测试”这个要求,我和队友学习了一些示例后开始操作.如下展示一些建立单元测试的过程.Step1:右键单击[解决方案]->左键单击[添加(D)]->[新建项目(N)]. Ste ...

  6. c#,mysql,读取乱码问题

    1.首先保证数据库的表是UTF8类型:数据库是否是utf8无关紧要: 2.c#连接数据库语句添加“charset=utf8”一句:.exe.config是否添加这一句也无关紧要: 3.访问数据库数据用 ...

  7. Swift-map()跟flatMap()区别

    map()方法介绍 map() 是  Array 提供的方法,通过接收一个函数作为传入参数,对数组中每个元素进行函数变换得到新的结果值.这样只需要提供  X->Y 的映射关系,就能将数组  [X ...

  8. Debian 7 amd64--TP-LINK TL-WN725N 2.0源码驱动编译安装

    租房用的是无线网络,在新安装的Debian 7 amd64使用的无线网卡型号是TP-LINK TL-WN725N 2.0,发现驱动安装还是有些问题,折腾了很久,特意在此记录一下. TL-WN725N ...

  9. mysql指定编码格式创建数据库

    CREATE DATABASE `dev` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

  10. CheckStateChanged(复选框选中状态更改事件)和 CheckedChanged(单选按钮选中状态更改事件)二者区别?

    CheckStateChanged(复选框选中状态更改事件)和 CheckedChanged(单选按钮选中状态更改事件)二者区别: 复选框控件(CheckBox)提供了CheckedChanged控件 ...