树形dp

涉及不重复背包组合求最小

从边长分段看不好入手

因为点数只有100点值<=2,总值<=200

可以对每个点的每个值进行dp

这里最后不回来肯定优于全回来

然后由于要分为回来和不回来两种情况要分别dp,因为不回来会要用到回来的

不回来的可以按不回来的最小+去掉不回来那个子节点的回来的最小进行dp

另外注意下上下限和初始值

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 205;
int vec[N];
struct Edge{
int v, len, nxt;
}edge[N];
int cnt;
int head[N];
int dp[2][N][N];
void addedge(int u, int v, int value) {
edge[cnt] = Edge{ v,value,head[u] };
head[u] = cnt++;
edge[cnt] = Edge{ u,value,head[v] };
head[v] = cnt++;
}
void dfs(int u,int p) {
dp[0][u][vec[u]] = 0;
dp[1][u][vec[u]] = 0;
for (int t = head[u]; t != -1; t = edge[t].nxt) {
Edge e = edge[t];
if (e.v == p)
continue;
dfs(e.v, u); int v = e.v;
for (int i = 200; i >= 0; i--)
{
for (int j = 0; j <= i; j++) {
dp[0][u][i] = min(dp[0][u][i],dp[0][u][j] + dp[0][v][i-j]+2*e.len);
}
}
}
for (int t = head[u]; t != -1; t = edge[t].nxt) {
Edge e = edge[t];
int v = e.v;
if (v == p)
continue;
int sum[N];
for (int i = 0; i <= 202; i++)
sum[i] = 1e9;
sum[vec[u]] = 0;
for (int t2 = head[u]; t2 != -1;t2=edge[t2].nxt) {
Edge e2 = edge[t2];
int v2 = e2.v;
if (v2==p || v2 == v)
continue; for (int i = 200; i >= 0; i--){
for (int j = 0; j <= i; j++) {
sum[i] = min(sum[i], sum[j] + dp[0][v2][i - j]+2*e2.len);
}
}
}
for (int i = 200; i >= 0; i--) {
for (int j = 0; j <= i; j++) {
dp[1][u][i] = min(dp[1][u][i], dp[1][v][j] + sum[i - j]+e.len);
}
}
}
}
int main() {
int n;
cin >> n;
cnt = 1;
for (int i = 1; i <= n; i++) {
cin >> vec[i];
}
memset(head, -1, sizeof head);
for (int i = 0; i < 2; i++)
for (int j = 0; j <= 200; j++)
for (int k = 0; k <= 200; k++)
dp[i][j][k] = 1e9; for (int i = 0; i < n - 1; i++) {
int sv, ev, w;
cin >> sv >> ev >> w;
addedge(sv,ev,w);
}
dfs(1, 0);
int m;
cin >> m;
while(m--){
int x;
cin >> x;
for (int i = 200; i >=0; i--)
if (dp[1][1][i] <= x)
{
printf("%d\n", i);
break;
}
}
return 0;
}

Hihocoder 1063 缩地的更多相关文章

  1. HihoCoder 1063 : 缩地 树形DP第二题(对象 边)

    时间限制:12000ms 单点时限:1000ms 内存限制:256MB 描述 编织者是 Dota 系列中的一个伪核,拥有很强的生存能力和线上消耗能力.编织者的代表性技能是缩地.缩地带来的隐身.极限移动 ...

  2. 树形DP专题

    DP是我的弱项, 此专题意在总结树形DP的解题思路. 最小代价遍历一棵树 给定一棵带边权的树 $T=(V,E)$ , 遍历它 (树的每个节点都访问至少一次) 所需的最小代价. 这里的代价由具体问题所定 ...

  3. hihocoder -1121-二分图的判定

    hihocoder -1121-二分图的判定 1121 : 二分图一•二分图判定 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 大家好,我是小Hi和小Ho的小伙伴Net ...

  4. Hihocoder 太阁最新面经算法竞赛18

    Hihocoder 太阁最新面经算法竞赛18 source: https://hihocoder.com/contest/hihointerview27/problems 题目1 : Big Plus ...

  5. hihoCoder太阁最新面经算法竞赛15

    hihoCoder太阁最新面经算法竞赛15 Link: http://hihocoder.com/contest/hihointerview24 题目1 : Boarding Passes 时间限制: ...

  6. 【hihoCoder 1454】【hiho挑战赛25】【坑】Rikka with Tree II

    http://hihocoder.com/problemset/problem/1454 调了好长时间,谜之WA... 等我以后学好dp再来看为什么吧,先弃坑(╯‵□′)╯︵┻━┻ #include& ...

  7. 【hihocoder#1413】Rikka with String 后缀自动机 + 差分

    搞了一上午+接近一下午这个题,然后被屠了个稀烂,默默仰慕一晚上学会SAM的以及半天4道SAM的hxy大爷. 题目链接:http://hihocoder.com/problemset/problem/1 ...

  8. 【hihoCoder】1148:2月29日

    问题:http://hihocoder.com/problemset/problem/1148 给定两个日期,计算这两个日期之间有多少个2月29日(包括起始日期). 思路: 1. 将问题转换成求两个日 ...

  9. 【hihoCoder】1288 : Font Size

    题目:http://hihocoder.com/problemset/problem/1288 手机屏幕大小为 W(宽) * H(长),一篇文章有N段,每段有ai个字,要求使得该文章占用的页数不超过P ...

随机推荐

  1. UVA 12544 - Beehives O(nm) 无向图最小环

    Bees are one of the most industrious insects. Since they collect nectarand pollen from flowers, they ...

  2. 【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别

    转自:http://blog.csdn.net/paincupid/article/details/47746341 一.HashMap和TreeMap区别 1.HashMap是基于散列表实现的,时间 ...

  3. Leetcode Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  4. BZOJ1493 [NOI2007]项链工厂

    未完待续... 终于改对了 热泪盈眶.jpg 错误原因:pushdown的时候没有判断是否有左右儿子,也没当x=0 return,于是出现一些奇怪的错误 #include<bits/stdc++ ...

  5. ibatis实现Iterate的使用

    <iterate property="" /*可选, 从传入的参数集合中使用属性名去获取值, 这个必须是一个List类型, 否则会出现OutofRangeException, ...

  6. TEST===>Sqlserver中获取年月日时分秒

    可以用两种方法获取 1. select GETDATE() as '当前日期', DateName(year,GetDate()) as '年', DateName(month,GetDate()) ...

  7. Tomcat启动后,从spring容器中获取Bean和ServletContext

    public static Object getBean(String beanName){ ApplicationContext context = ContextLoader.getCurrent ...

  8. php 跨服务器ftp移动文件

    $ftp_server = "120.25.1.1";$ftp_user_name = "p1111";$ftp_user_pass = "psa12 ...

  9. Random随机类(11选5彩票)BigInteger大数据类(华为面试题1000的阶乘)

    先上Java Web图 为了简化叙述,只写Java代码,然后控制台输出 使用[Random类]取得随机数 import java.util.Random; public class Fir { pub ...

  10. HDU 1166 敌兵布阵(分块)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...