题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003

Humans have discovered a kind of new metal mineral on Mars which are distributed in point‐like with paths connecting each of them which formed a tree. Now Humans launches k robots on Mars to collect them, and due to the unknown reasons, the landing site S of all robots is identified in advanced, in other word, all robot should start their job at point S. Each robot can return to Earth anywhere, and of course they cannot go back to Mars. We have research the information of all paths on Mars, including its two endpoints x, y and energy cost w. To reduce the total energy cost, we should make a optimal plan which cost minimal energy cost. 
 
题意描述:火星上有很多矿山(n<=10000),人们发射k(k<=10)个机器人着陆在火星上的S矿山上,目的就是采取每座矿山上的资源。一些矿山之间相互连接着,从一个矿山到另一个与其相连的矿山要消耗能量,问其最少的消耗能量是多少。
算法分析:树形DP,dp[u][i]定义为以u为根的子树中分配了i个机器人消耗的最少能量,特别的是,dp[u][0]表示为以u为树根的子树中分配了一个机器人并且机器人要在走完这颗子树后要回到u点(就相当于没有给子树分配)的最少消耗能量。
那么我们可以列出式子:dp[u][i]=min(dp[u][i],dp[u][i-j]+dp[v][j]+j*cost)(v为u的相邻节点,w为这条路的能量消耗)。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,s,k;
int father[maxn],dp[maxn][];
vector<pair<int,int> > G[maxn]; void dfs(int u,int f)
{
father[u]=f;
int num=G[u].size();
for (int i= ;i<num ;i++)
{
int v=G[u][i].first;
int cost=G[u][i].second;
if (v==f) continue;
dfs(v,u);
for (int j=k ;j>= ;j--)
{
dp[u][j] += dp[v][]+*cost;
for (int q= ;q<=j ;q++)
dp[u][j]=min(dp[u][j],dp[u][j-q]+dp[v][q]+q*cost);
}
}
} int main()
{
while (scanf("%d%d%d",&n,&s,&k)!=EOF)
{
memset(father,-,sizeof(father));
memset(dp,,sizeof(dp));
for (int i= ;i<=n ;i++) G[i].clear();
int a,b,c;
for (int i= ;i<n- ;i++)
{
scanf("%d%d%d",&a,&b,&c);
G[a].push_back(make_pair(b,c));
G[b].push_back(make_pair(a,c));
}
dfs(s,-);
printf("%d\n",dp[s][k]);
}
return ;
}
 

hdu 4003 Find Metal Mineral 树形DP的更多相关文章

  1. hdu 4003 Find Metal Mineral 树形dp ,*****

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  2. HDU 4003 Find Metal Mineral(分组背包+树形DP)

    题目链接 很棒的一个树形DP.学的太渣了. #include <cstdio> #include <string> #include <cstring> #incl ...

  3. HDU4003Find Metal Mineral[树形DP 分组背包]

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  4. HDU4003 Find Metal Mineral 树形DP

    Find Metal Mineral Problem Description Humans have discovered a kind of new metal mineral on Mars wh ...

  5. HDU-4003 Find Metal Mineral 树形DP (好题)

    题意:给出n个点的一棵树,有k个机器人,机器人从根节点rt出发,问访问完整棵树(每个点至少访问一次)的最小代价(即所有机器人路程总和),机器人可以在任何点停下. 解法:这道题还是比较明显的能看出来是树 ...

  6. HDU 4003 Find Metal Mineral (树形DP,经典)

    题意:给定一棵树图,n个节点,有边权,要派k<11个机器人从节点s出发,遍历所有的点,每当1只机器人经过1条边时就会花费该边的边权,边是可重复走的.问遍历完所有点的最小花费? 思路: 非常经典, ...

  7. hdu4003Find Metal Mineral(树形DP)

    4003 思维啊 dp[i][j]表示当前I节点停留了j个机器人 那么它与父亲的关系就有了 那条边就走了j遍 dp[i][j] = min(dp[i][j],dp[child][g]+dp[i][j- ...

  8. HDU 4003 Find Metal Mineral

    这个题是POJ1849的加强版. 先说一个很重要的结论,下面两种方法都是从这个结论出发的. 一个人从起点遍历一颗树,如果最终要回到起点,走过的最小权值就是整棵树的权值的2倍. 而且K个人的情况也是如此 ...

  9. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

随机推荐

  1. Uva 1588 Kickdown

    这道题思路并不难想,在做题过程中主要遇到的困难有: 因为没有仔细的考虑边界情况,没有分析全面,导致因=没有取到而得不出正确结果,浪费的大量时间. 今后在做这类题目时,一定要先进行一个比较全面的分析+模 ...

  2. win7防火墙打不开(无法启动windows firewall服务)

    点击windows 7控制面板中防火墙的“推荐配置”没有反应:打开“服务”,无法启动windows firewall,并报错.  可能很多的win7用户都碰到过这样的一种情况,那就是win7的防火墙打 ...

  3. Oracle 直方图实例测试

    --创建表 SQL> create table tab (a number, b number); Table created. --插入数据 SQL> begin .. loop ins ...

  4. 线程操作API

    线程操作API 1.currentThread 2.getId() .getName().getPriority().getStart.isAlive().isDaemon().isInterrupt ...

  5. 刀哥多线程之一次性代码gcd-11-once

    一次性执行 有的时候,在程序开发中,有些代码只想从程序启动就只执行一次,典型的应用场景就是"单例" // MARK: 一次性执行 - (void)once { static dis ...

  6. 【转】C# 子窗体如何调用父窗体的方法

    网络上有几种方法,先总结如下: 调用窗体(父):FormFather,被调用窗体(子):FormSub. 方法1: 所有权法       //FormFather:        //需要有一个公共的 ...

  7. MAC下安装与配置MySQL [转]

    一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL Community Server”下方有一个“download” ...

  8. Environment 类

    提供有关当前环境和平台的信息以及操作它们的方法. 此类不能被继承. using System; using System.Collections; using System.Collections.G ...

  9. db2新建数据库

    一.建表空间和数据库 1.在db2ad.db2db和db2ap上均执行: [sql] view plaincopyprint? db2set db2comm=tcpip db2set db2codep ...

  10. mvvm 模式

    MVC = Massive View Controller ? 有笑话称MVC为重量级的试图控制器.仔细一想,确实存在这个问题.以UITableViewController和UITableView举个 ...