Find Metal Mineral

Problem Description
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.
 
Input
There are multiple cases in the input.
In each
case:
The first line specifies three integers N, S, K specifying the numbers
of metal mineral, landing site and the number of robots.
The next n‐1 lines
will give three integers x, y, w in each line specifying there is a path
connected point x and y which should cost w.
1<=N<=10000,
1<=S<=N, 1<=k<=10, 1<=x, y<=N, 1<=w<=10000.
 
Output
For each cases output one line with the minimal energy
cost.
 
Sample Input
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
 
Sample Output
3
2

Hint

In the first case: 1->2->1->3 the cost is 3;
In the second case: 1->2; 1->3 the cost is 2;

 

题意:

题解:

我们设dp[i][j] 表示已i为根节点,放j个机器人的最小话费

那么:

dp(i,j)=(dp(son,0)+cast[son]*2)(子树没有停留机器人)+min(dp[p][m-y]+y*cost[son]+dp[son][y])(子树停留了y个机器人)

//meek
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** const int N=+;
const ll inf = 1ll<<;
const int mod= ; vector< pair<int ,int> >G[N];
int dp[N][],n,S,K,u,v,w;
void dfs(int x,int pre) {
for(int i=;i<G[x].size();i++) {
if(G[x][i].fi==pre) continue;
int son=G[x][i].fi,cost=G[x][i].se;
dfs(G[x][i].fi,x);
for(int k=K;k>=;k--) {
dp[x][k]+=dp[son][]+cost*;
for(int y=;y<=k;y++) {
dp[x][k]=min(dp[x][k],dp[x][k-y]+dp[son][y]+cost*y);
}
}
}
}
void init() { for(int i=;i<=n;i++) G[i].clear();
mem(dp);
}
int main() {
while(~(scanf("%d%d%d",&n,&S,&K))) {
init();
for(int i=;i<n;i+=) {
scanf("%d%d%d",&u,&v,&w);
G[u].pb(MP(v,w));
G[v].pb(MP(u,w));
}
dfs(S,-);
cout<<dp[S][K]<<endl;
} return ;
}

daima

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

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

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

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

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

  3. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

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

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

  5. hdu4003Find Metal Mineral(树形DP)

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

  6. HDU4003 Find Metal Mineral

    看别人思路的 树形分组背包. 题意:给出结点数n,起点s,机器人数k,然后n-1行给出相互连接的两个点,还有这条路线的价值,要求最小花费 思路:这是我从别人博客里找到的解释,因为很详细就引用了 dp[ ...

  7. hdu4003详解(树形dp+多组背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Find Metal Mineral Time Limit: 2000/1000 MS (Jav ...

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

    题目大意:用m个机器人去遍历有n个节点的有根树,边权代表一个机器人通过这条边的代价,求最小代价. 题目分析:定义状态dp(root,k)表示最终遍历完成后以root为根节点的子树中有k个机器人时产生的 ...

  9. 【树形dp】Find Metal Mineral

    [HDU4003]Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (J ...

随机推荐

  1. 三星Galaxy Note 10.1 N8010 最后的救赎 Andorid 5.0.2 ROM

    上市日期为2012年的三星Galaxy Note N8010 10.1采用10.1英寸TFT屏幕,分辨率为1280×800,支持10点触控,支持S pen手写笔功能.,拥有一颗1.4GHz Exyno ...

  2. 内部类&匿名内部类

    内部类:如果A类需要直接访问B类中的成员,而B类又需要建立A类的对象.这时,为了方便设计和访问,直接将A类定义在B类中.就可以了.A类就称为内部类.内部类可以直接访问外部类中的成员.而外部类想要访问内 ...

  3. P2763: [JLOI2011]飞行路线

    然而WA了呀,这道分层图,也是不明白为什么WA了=-= ; maxn=; points=; type node=record f,t,l:longint; end; var n,m,k,i,j,u,v ...

  4. 微软职位内部推荐-SDEII for Windows Phone Apps

    微软近期Open的职位: Job title: Software Design Engineer II Location: China, Beijing Division: Operations Sy ...

  5. Scrum仪式之Sprint计划会议

    会议时间:4.15.晚八点 会议地点:基础教学楼二楼 会议进程 • 首先我们讨论了实验第一个Sprint1要实现的功能,我们的初期目标.•  然后我们进一步梳理了第一阶段的任务和需求.•  之后对任务 ...

  6. C#制作高仿360安全卫士窗体(四)- 水晶按钮

    项目越来越紧,我也乐此不疲.自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西.我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用.所以只 ...

  7. 你所必须掌握的三种异步编程方法callbacks,listeners,promise

    目录: 前言 Callbacks Listeners Promise 前言 coder都知道,javascript语言运行环境是单线程的,这意味着任何两行代码都不能同时运行.多任务同时进行时,实质上形 ...

  8. Java 调用 Javascript 函数的范例

    在Java 7 以后,可以在Java代码中调用javascript中的函数,请看下面的例子: package com.lee; import java.io.FileNotFoundException ...

  9. 一个关于ExtJS4具体控件的详细教程

    发现一遍介绍ExtJS控件介绍的比较好的系列文章,在此做总结 ExtJs4 笔记(1) ExtJs大比拼JQuery:Dom文档操作 ExtJs4 笔记(2) ExtJs对js基本语法扩展支持 Ext ...

  10. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...