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 ...
随机推荐
- com技术学习
百度百科概念 COM是微软公司为了计算机工业的软件生产更加符合人类的行为方式开发的一种新的软件开发技术.在COM构架下,人们可以开发出各种各样的功能专一的组件,然后将它们按照需要组合起来,构成复杂的应 ...
- lintcode-160-寻找旋转排序数组中的最小值 II
160-寻找旋转排序数组中的最小值 II 假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2). 你需要找到其中最小的元素. 数组中可能存在 ...
- dwarf是怎样处理的栈帧?
dwarf是如何处理的栈帧呢? 首先看下非dwarf的情况是如何处理栈帧的: 1 3623804982590 0x3e90 [0xb0]: PERF_RECORD_SAMPLE(IP, 0x1): 1 ...
- centos7编译安装redis遇坑
编译redis时:make cc Command not found 原因分析:没有安装gcc,执行: yum install gcc 编译redis时:error: jemalloc/jemallo ...
- InstallShield Limited Edition for Visual Studio 国内注册时国家无下拉框解决方法
注册地址:http://learn.flexerasoftware.com/content/IS-EVAL-InstallShield-Limited-Edition-Visual-Studio 火狐 ...
- 在a标签的href用户#name 的可以实现页面 上下跳转
- poj1065-Wooden Sticks
题目 有很多小木棍需要机器处理.每个小木棍有重量和长度两个属性.不断把小木棍放入机器中,如果小木棍\(a\)放完后放入小木棍\(b\),那么如果\(a.weight<=b.weight\ and ...
- P1365 WJMZBMR打osu! / Easy
题目背景 原 维护队列 参见P1903 题目描述 某一天WJMZBMR在打osu~~~但是他太弱逼了,有些地方完全靠运气:( 我们来简化一下这个游戏的规则 有 nnn 次点击要做,成功了就是o,失败了 ...
- python 深浅copy的例子
1. copy.copy 浅拷贝 只拷贝父对象,不会拷贝对象的内部的子对象.2. copy.deepcopy 深拷贝 拷贝对象及其子对象一个很好的例子:import copya = [1, 2, 3, ...
- POJ3461:Oulipo——题解
http://poj.org/problem?id=3461 KMP板子,好久以前学过了,直接把板子粘上去即可. #include<cstdio> #include<cstring& ...