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 ...
随机推荐
- codeforces 295C Greg and Friends(BFS+DP)
One day Greg and his friends were walking in the forest. Overall there were n people walking, includ ...
- Python中函数的参数-arguments
归纳起来,Python中函数的定义形式和调用形式主要有如下几种形式: # 函数的定义形式 def func(name) # 匹配positional参数或者keyword参数 def func(nam ...
- java---Map接口实现类
Map是一个双列集合接口,如果实现了Map接口,特点是数据以键值对形式存在,键不可重复,值可以重复.java中主要有HashMap.TreeMap.Hashtable.本文主要介绍Map的接口方法: ...
- 第三章——供机器读取的数据(XML)
本书使用的文件.代码:https://github.com/huangtao36/data_wrangling 机器可读(machine readable)文件格式: 1.逗号分隔值(Comma-Se ...
- iOS- 优化与封装 APP音效的播放
1.关于音效 音效又称短音频,是一个声音文件,在应用程序中起到点缀效果,用于提升应用程序的整体用户体验. 我们手机里常见的APP几乎都少不了音效的点缀. 显示实现音效并不复杂,但对我们App很 ...
- nuget程序包还原失败:未能解析此远程名称
一个简便的方法就是取消下载缺少的程序包. 步骤如下: 1,工具--NuGet程序包管理器--程序包管理器设置 2,NuGet Package Manager--常规,取消勾选.
- SpringData——HelloWorld
1.背景 最开始了解SpringData的时候,以为他不就是ORM的一种实现方式嘛,还能有什么新的东西.从hibernate到ibatis.mybatis,也许他只不过是spring想整合一个更方便的 ...
- 【Linux】CentOS安装redis
CENTOS7下安装REDIS 安装完成之后使用:redis-cli命令连接,如图: 提示:/var/run/redis_6379.pid exists, process is already run ...
- java得到当前时间
SimpleDateFormat timeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date ...
- 每个zone的low memory是怎么计算出来的
内核都是试图让活动页和不活动页的数量均衡 在分配内存时每次都会唤醒wakeup_swapd,这个函数会在 现在是不是已经没有全局的LRU表了?已经都变成per cgroup级别的LRU表了吗? ina ...