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 ...
随机推荐
- 如何把阿里云的服务器配置为mac的共享文件夹(亲测有效)
写在开头的就是,我只能百分之九十确定这个是真的有效....毕竟试了太多的方法,最后莫名其妙的就好了.. - -# 基础的步骤就不说了,网上一搜一大把,大家可能follow了所有的步骤以后发现还是连接不 ...
- hadoop删除节点和添加节点
从hadoop移除机器把需要移除的机器增加到exclueds文件中,强制刷新datanode列表,等待decommission 状态正常后,即可停机下架,如有必要在namenode执行balancer ...
- CSS垂直居中小结
1.设置子元素: { ... position :absolute; margin:auto; top:; right:; bottom:; left:; } 2.设置子元素:(height必须是固定 ...
- POJ1182 食物链---(经典种类并查集)
题目链接:http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- [bzoj1031][JSOI2007]字符加密Cipher——后缀数组
Brief Description 给定一个长度为n的字符串,你需要对其进行加密. 把字符串围成一个环 显然从任意一个位置开始都可以有一个长度为n的串 把产生的n个串按字典序排序,把这n个串的最后一个 ...
- bzoj 1025 DP
这道题根据群论的基础知识,我们可以转化成将n拆分成若干数,求这些数 的lcm的方案数 先筛下素数表prime 那么我们可以用DP来解决这个问题,用W[I,J]代表I这个数,拆成若干个数, 其中质因数最 ...
- Django【进阶】信号
-信号 Django中提供了“信号调度”,用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 问题:如何对所有数据库添加操作进行日志记录? 问题:信 ...
- git应用基础配置
1.注册github账号.注册的时候会要求输入用户名和email这是配置git必须的要素 2.在使用git的时候首先需要把注册时候配合的用户名和密码填在git仓库的配置文件里,如下 harvey@ha ...
- win32 读取文本文件,并进行字符串分割和存储
//分割字符 char *p;//p存放临时行指针 q存放临时每一行的列指针 char *hang[100]={0};//存储第一行 char *lie[300]={0} ...
- Selenium2+python自动化43-判断title(title_is)【转载】
前言 获取页面title的方法可以直接用driver.title获取到,然后也可以把获取到的结果用做断言. 本篇介绍另外一种方法去判断页面title是否与期望结果一种,用到上一篇Selenium2+p ...