hdu 4003 Find Metal Mineral (树形dp)
2014-03-15 17:03
399 查看
Find Metal Mineral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 2122 Accepted Submission(s): 965
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each cases output one line with the minimal energy cost.
[align=left]Sample Input[/align]
3 1 1
1 2 1
1 3 1
3 1 2
1 2 1
1 3 1
[align=left]Sample Output[/align]
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;
[align=left]Source[/align]
The 36th ACM/ICPC
Asia Regional Dalian Site —— Online Contest
题意:
给你一棵树,有k个机器人从s点出发将树上的每个点都覆盖到,求最小花费。
思路:
dp[i][j] - 到i点用j个机器人覆盖以i为根的树时的最小花费。
dp[i][0] - 到i点用1个机器人覆盖以i为根的树并回到i点的最小花费。
那么有方程
dp[u][j]=dp[v][k]+dp[u][j-k]+k*w; (w为u-v的边的权值)
转移时要用到dp[u][0]为0进行转移,又要用到dp[u][0]的意义转移,dp[u][0]不能表示双重含义,故可用dp[v][0]代替后者。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 10005 #define MAXN 20005 #define mod 1000000007 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 typedef long long ll; using namespace std; int n,m,ans,cnt,tot,flag; int pp[maxn],dp[maxn][12],s; struct Node { int v,w,next; }edge[MAXN]; void addedge(int u,int v,int w) { cnt++; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=pp[u]; pp[u]=cnt; } void dfs(int u,int fa) { int i,j,k,t,v; for(i=pp[u];i;i=edge[i].next) { v=edge[i].v; if(v==fa) continue ; dfs(v,u); for(j=m;j>=0;j--) { dp[u][j]+=dp[v][0]+2*edge[i].w; for(k=0;k<=j;k++) { dp[u][j]=min(dp[u][j],dp[v][k]+dp[u][j-k]+k*edge[i].w); } } } } int main() { int i,j,t; while(~scanf("%d%d%d",&n,&s,&m)) { int u,v,w; cnt=0; memset(pp,0,sizeof(pp)); for(i=1;i<n;i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } memset(dp,0,sizeof(dp)); dfs(s,0); ans=dp[s][m]; printf("%d\n",ans); } return 0; }
相关文章推荐
- 字符设备(4)ioctl
- BZOJ 1001: [BeiJing2006]狼抓兔子
- 开辟动态数组、printf
- 【ubuntu 12.04 安装cmake】
- POJ 3067 Japan(树状数组:求逆序)
- ubuntu12.04下面codeblocks配置opencv2.4.6
- 2012-10-20 NOIP模拟赛
- PHP的多版本环境管理
- 安装JDK出现"Windows Installer程序包有问题,此安装需要的DLL不能运行"解决方法
- 数组中的某两个数只出现一次
- csdn第四届在线编程大赛·线上初赛:带通配符的数
- vim编辑器颜色主题color scheme
- Linux下C编写基本的多线程socket服务器
- TFS二次开发01——TeamProjectsPicher
- sql server2008安装图解
- jQuery扩展实现复选框批操作
- 在同一应用程序中同一窗体只打开一个
- WinDbg相关的一些资源
- solr4.7中文分词器(ik-analyzer)配置
- Websphere设置JVM时区解决程序、日志时间快8小时问题