HDOJ 4085 Peach Blossom Spring
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 和上一道斯坦纳树的很像啊,就是关键点一一对应变成了数量对应,其实总体上写法都差不多。
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
int f[][],n,m,T,ci[];
int ans[],dis[],k;
int q[],l,r,num,all;
int to[],ne[];
int val[],hd[];
bool iq[]; inline void init(){
num=;
memset(hd,,sizeof(hd));
memset(f,0x3f,sizeof(f));
memset(ans,0x3f,sizeof(ans));
memset(f[],,sizeof(f[]));
} int main(){
ci[]=;
for(int i=;i<=;i++) ci[i]=ci[i-]<<; scanf("%d",&T);
while(T--){
init();
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=k;i++) f[ci[i-]][i]=;
for(int i=n-k+;i<=n;i++) f[ci[n+k-i]][i]=; int uu,vv,ww;
for(int i=;i<=m;i++){
scanf("%d%d%d",&uu,&vv,&ww);
to[++num]=vv,ne[num]=hd[uu],hd[uu]=num,val[num]=ww;
to[++num]=uu,ne[num]=hd[vv],hd[vv]=num,val[num]=ww;
} all=ci[k<<]-;
for(int S=;S<=all;S++){
for(int i=;i<=n;i++)
for(int s=S&(S-);s;s=(s-)&S) f[S][i]=min(f[S][i],f[s][i]+f[s^S][i]); for(int i=;i<=n;i++) dis[i]=f[S][i],iq[i]=,q[i]=i;
l=,r=n;
while(l<=r){
int x=q[l++];
for(int i=hd[x];i;i=ne[i]) if(dis[x]+val[i]<dis[to[i]]){
dis[to[i]]=dis[x]+val[i];
if(!iq[to[i]]) iq[to[i]]=,q[++r]=to[i];
}
iq[x]=;
} for(int i=;i<=n;i++) f[S][i]=dis[i];
} for(int s=;s<=all;s++){
int num1=,num2=;
for(int i=;i<=k;i++) if(s&ci[i-]) num1++;
for(int i=k+;i<=(k<<);i++) if(s&ci[i-]) num2++;
if(num1!=num2) continue; for(int i=;i<=n;i++) ans[s]=min(ans[s],f[s][i]);
} for(int S=;S<=all;S++)
for(int s=S&(S-);s;s=(s-)&S) ans[S]=min(ans[S],ans[s]+ans[s^S]); if(ans[all]!=ans[all+]) printf("%d\n",ans[all]);
else puts("No solution");
} return ;
}
HDOJ 4085 Peach Blossom Spring的更多相关文章
- HDU 4085 Peach Blossom Spring
斯坦纳树. 最后可以是森林,在计算出每个联通状态的最小费用后,还需要进行一次$dp$. #include<bits/stdc++.h> using namespace std; const ...
- 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] ...
- hdu4085 Peach Blossom Spring
Peach Blossom Spring http://acm.hdu.edu.cn/showproblem.php?pid=4085 Time Limit: 10000/5000 MS (Java/ ...
- 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 ...
- HDU 4081 Peach Blossom Spring (最小生成树+dfs)
题意:给定一个 n 个点和相应的权值,要求你用 n-1 条边连接起来,其中一条边是魔法边,不用任何费用,其他的边是长度,求该魔法边的两端的权值与其他边费用的尽量大. 析:先求出最小生成树,然后再枚举每 ...
- 【转】并查集&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 =============================以下是最小生成树+并 ...
- hdu图论题目分类
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
- HDU图论题单
=============================以下是最小生成树+并查集====================================== [HDU] 1213 How Many ...
随机推荐
- CentOS 6.5 Linux 安装 openoffice
资源准备: Apache_OpenOffice_4.1.4_Linux_x86-64_install-rpm_zh-CN.tar.gz 编译安装: 本人资源包放在 /opt/moudles 中, 解压 ...
- oracle 包和包实现
包: create or replace package sp_pexam_clear as --定义结构体 /*type re_stu is record( rname student.name%t ...
- 使用vs2010编辑Unity脚本,配置方法
在Unity界面上.选择Edit->Preferences->External Tools,External Script Editor一项即为编译器. 以Unity3D 4.3.4 f1 ...
- final 的作用
1.修饰类 类不能被继承 2.修饰方法 目的有二: 1)禁止子类重写该方法 2)执行效率(JVM相关的东西,不用太关注) 3.修饰变量 final修饰原始类型的变量,该变量不能被修改 final修饰引 ...
- Codeforces Round #299 Div2 解题报告
这场比赛并没有打现场,昨天晚上做了ABCD四道题,今天做掉了E题 以前还没有过切完一场比赛的所有题呢~爽~ A. Tavas and Nafas Today Tavas got his test ...
- HDU 2105 The Center of Gravity (数学)
题目链接 Problem Description Everyone know the story that how Newton discovered the Universal Gravitatio ...
- 一道面试题:C++相比C#或者java的优势到底在哪里
被问到了这样一道面试题,当时就懵了,内心一直觉得C++肯定在很多方面要比C#或者java要牛b的. 但是真的不知道怎么回答. 问题是:你以前一直做得是.NET相关项目,现在为什么找C++开发相关工作呢 ...
- [转载]基于Redis的Bloomfilter去重(附Python代码)
前言: “去重”是日常工作中会经常用到的一项技能,在爬虫领域更是常用,并且规模一般都比较大.去重需要考虑两个点:去重的数据量.去重速度.为了保持较快的去重速度,一般选择在内存中进行去重. 数据量不大时 ...
- Python/CMD 文件备份
1.使用Python压缩文件并另存 import zipfile, os #备份文件ZIP格式: folder 目标文件夹 : Targetfolder:另存地址 def backuptozip(fo ...
- 同源、同源策略、跨域问题、django解决方案
什么是同源: URL由协议.域名.端口和路径组成,如果两个URL的协议.域名和端口相同,则表示他们同源. 注意:假如你的网站ip是123.123.123.123,网站的域名是www.abc.com. ...