hdu4085 Peach Blossom Spring
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)
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?
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).
repair the roads, output a string "No solution" in a line. Otherwise, output the
minimum cost to repair the roads in a line.
#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的更多相关文章
- 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 ...
- HDOJ 4085 Peach Blossom Spring
discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...
- HDU 4085 Peach Blossom Spring
斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...
- HDU 4081 Peach Blossom Spring (最小生成树+dfs)
题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大. 析:先求出最小生成树,然后再枚举每 ...
- 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] ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- 【HDOJ图论题集】【转】
=============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- HADOOP docker(七):hive权限管理
1. hive权限简介1.1 hive中的用户与组1.2 使用场景1.3 权限模型1.3 hive的超级用户2. 授权管理2.1 开启权限管理2.2 实现超级用户2.3 实现hiveserver2用户 ...
- 《梦断代码Dreaming In Code》阅读笔记(三)
最后这几章感觉上更多是从软件完成整体上来讲的.比如说技术.方法等. 在我看来,其实一个团队一直坚持一种好的.先进的方法是不可少的.如果一个优秀的团队刚愎自用,只随着成员们喜好发展,那不能长久.比如说, ...
- Mac OS安装Scrapy失败
报错: DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be re ...
- TCP系列24—重传—14、F-RTO虚假重传探测
一.虚假重传 在一些情况下,TCP可能会在没有数据丢失的情况下初始化一个重传,这种重传就叫做虚假重传(Spurious retransmission).发生虚假重传的原因可能是包传输中重排序.传输中发 ...
- 3ds Max学习日记(四)
下午去实验室见了师姐,人还挺好,给我安排了任务,和3ds max没有半毛钱关系. 附上今日的劳动成果: 板子(牌匾) 简约吊灯(看上去比较单调) 高脚杯(喝酒用的) 沙发(沙发) ...
- 利用Vue v-model实现一个自定义的表单组件
原文请点此链接 http://blog.csdn.net/yangbingbinga/article/details/61915038
- centOS 6.5命令方式配置静态IP
想自己做个centOS玩一下,然后通过FTP访问操作,首先查看是否开启了SSH,命令如下: rpm -qa | grep ssh 这个时候看到的是centOS的ssh已经打开!要是通过FTP工具访问还 ...
- 简易js调试
1.console显示信息的命令: console.log() console.info() console.error() console.warn() 2.console信息分组 cons ...
- 第三章 AOP
什么是AOP AOP的编写方式 什么是AOP? 是一种面向切面的思想,关注的是切面中的相似功能,将这些功能抽离出来,提高代码的复用性 AOP术语 advice-通知:要执行的任务 Spring切面有5 ...
- BZOJ4869 六省联考2017相逢是问候(线段树+欧拉函数)
由扩展欧拉定理,a^(a^(a^(……^x)))%p中x作为指数的模数应该是φ(φ(φ(φ(……p)))),而p取log次φ就会变为1,也即每个位置一旦被修改一定次数后就会变为定值.线段树维护区间剩余 ...