蓝桥杯 算法提高 金属采集 (树形动态规划)
2014-07-15 16:38
211 查看
// 蓝桥杯 算法提高 金属采集 (树形动态规划) #include<iostream> #include<cstdio> #include<cstring> #include<vector> using namespace std; const int N=100010; const int M=12; struct Node { int child; int value; }s; int visit ={0};//判断是否遍历的节点 int dp [M]={0}; int cost ={0}; int n,dir,m;//n节点数,dir位置,m机器人数 vector<Node> no ; int Min(int a,int b) { if(a<b) { return a; } return b; } void pr() { for(int i=0;i<=n;i++) { for(int j=0;j<=m;j++) { cout<<dp[i][j]<<" "; } cout<<endl; } } void DP(int root)//把子的状态都移到父的状态 { int i; for(i=0;i<=m;i++) { dp[root][i]=0; } visit[root]=1; for(i=0;i<no[root].size();i++) { int son=no[root][i].child; if(visit[son]) continue; DP(son); cost[son]=no[root][i].value; for(int mi=m;mi>=0;mi--)//节点拥有的火星数 { dp[root][mi]+=dp[son][0]+cost[son]*2; for(int mj=1;mj<=mi;mj++)//进行动态的转移 { dp[root][mi]=Min(dp[root][mi],dp[root][mi-mj]+mj*cost[son]+dp[son][mj]); } } } } int main() { scanf("%d%d%d",&n,&dir,&m); int a1,a2,dis; while(scanf("%d%d%d",&a1,&a2,&dis)==3) { s.child=a1; s.value=dis; no[a2].push_back(s); s.child=a2; s.value=dis; no[a1].push_back(s); } DP(dir); printf("%d\n",dp[dir][m]); return 0; }
相关文章推荐
- 蓝桥杯_算法提高_金属采集(树形动态规划)
- 蓝桥杯 算法提高 金属采集 [ 树形dp 经典 ]
- hdu4003&&蓝桥杯, 算法提高 金属采集 (树形DP,经典。。。)
- 算法提高 金属采集_树形dp
- 算法笔记_166:算法提高 金属采集(Java)
- 算法提高 金属采集 树形DP
- 蓝桥杯 金属采集 树形动态规划
- 1587:算法提高 金属采集(dfs+dp)
- 蓝桥杯-算法提高-Cowboys
- 蓝桥杯——算法提高 大数加法&大数乘法&冒泡排序
- C++ - 蓝桥杯 - 算法提高 学霸的迷宫 (bfs+记录路径)
- 【ShawnZhang】带你看蓝桥杯——算法提高 勾股数
- 蓝桥杯 算法提高 8-1因式分解(Java解题)
- 蓝桥杯 ADV-158 算法提高 新建Microsoft Word文档
- 蓝桥杯 算法提高 P0101(Java解题)
- 蓝桥杯 ADV-80 算法提高 选最大数
- 蓝桥杯 ADV-96 算法提高 复数求和
- 算法-蓝桥杯-算法提高 7-1用宏求球的体积(JAVA)
- 算法笔记_100:蓝桥杯练习 算法提高 三个整数的排序(Java)
- 算法笔记_104:蓝桥杯练习 算法提高 新建Microsoft Word文档(Java)