程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu 4003 樹形dp+分組背包

hdu 4003 樹形dp+分組背包

編輯:C++入門知識

Find Metal Mineral

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2106 Accepted Submission(s): 953


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

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


題意:給定一棵樹,起點,機器人個數,求遍歷所有節點的最小路徑和。

ddp[u][i]表示訪問結束後以u為根節點的子樹上機器人個數,假如有0個機器人,那麼肯定是一個機器人去訪問完後又返回了,因此需要特殊考慮,

代碼:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-6 16:15:10
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define INF 1000000
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=40009;
int head[maxn],tol;
struct node{
    int next,to,val;
    node(){};
    node(int _next,int _to,int _val):next(_next),to(_to),val(_val){}
}edge[5*maxn];
void add(int u,int v,int val){
    edge[tol]=node(head[u],v,val);
    head[u]=tol++;
}
int K,dp[maxn][13];
void dfs(int u,int fa){
	for(int i=head[u];i!=-1;i=edge[i].next){
		int v=edge[i].to;
		if(v==fa)continue;
		dfs(v,u);
		for(int j=K;j>=0;j--){
			dp[u][j]+=dp[v][0]+2*edge[i].val;
			for(int k=0;k<=j;k++)
				dp[u][j]=min(dp[u][j],dp[u][j-k]+dp[v][k]+k*edge[i].val);
		}
	}
}
int main(){
	int i,j,k,m,n,s;
    while(~scanf("%d%d%d",&n,&s,&K)){
		memset(head,-1,sizeof(head));tol=0;
		for(i=1;i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved