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. Cannot retrieve repository metadata (repomd.xml) for repository: base. Please verify its path and try again YUM报错

        1.挂盘 ----- 2.# mount /dev/sr0 /media/ mount: block device /dev/sr0 is write-protected, mounting ...

  2. 重构 之 总结代码的坏味道 Bad Smell (一) 重复代码 过长函数 过大的类 过长参数列 发散式变化 霰弹式修改

    膜拜下 Martin Fowler 大神 , 开始学习 圣经 重构-改善既有代码设计 . 代码的坏味道就意味着需要重构, 对代码的坏味道了然于心是重构的比要前提; . 作者 : 万境绝尘 转载请注明出 ...

  3. Android 开发 之 JNI入门 - NDK从入门到精通

    NDK项目源码地址 : -- 第一个JNI示例程序下载 : GitHub - https://github.com/han1202012/NDKHelloworld.git -- Java传递参数给C ...

  4. TFS持续集成

    TFS持续集成的就是跟踪代码变更,合并,能够自定义脚本,任务进行自动化测试,发版,部署,有点像docker的味道.在这个代理服务器分布式中tfsserver起着能够随时拿去最新代码能够统一执行任务的角 ...

  5. dedecms 后台登录地址

    dedecms  后台登录地址 http://www.域名.com/member/index.php

  6. C#操作access练习

    ORM框架使用dapper,dapper不仅能操作sqlserver,也能操作access,下面为基本代码: OleDbConnection strConnection = new OleDbConn ...

  7. asp.net MVC4在Action间跳转 RedirectToAction 传值参数问题

    return RedirectToAction("Test", new { cw = cw, firstdirectoryid = firstdirectoryid }); 上式中 ...

  8. [BZOJ4942] [NOI2017]整数

    题目背景 在人类智慧的山巅,有着一台字长为1048576位(此数字与解题无关)的超级计算机,著名理论计算机科 学家P博士正用它进行各种研究.不幸的是,这天台风切断了电力系统,超级计算机 无法工作,而 ...

  9. Android <Android应用开发实战> 资源类型<一>

    1.字符串资源>>1.普通字符串>>2.字符串数组 <resources> <string-array name="planets_array&qu ...

  10. POJ3090:Visible Lattice Points——题解

    http://poj.org/problem?id=3090 题目大意:你站在(0,0)的点上看向第一向限的点,点和点会互相阻挡,问最多看到多少点. 很容易想到,我们能看到的点,它的横纵坐标一定是互质 ...