http://acm.timus.ru/problem.aspx?space=1&num=1472

题目大意:

一颗树,根节点(1) 的值为 1.0,所有叶子节点的值为 0.0 ,其他节点值任意

最后要求的是 所有相邻两个节点的值差的总和最小。

思路:

假设一个叶子节点为 c ,他的父节点为 b ,b的父节点为 a,那么 c 的值为 0.0,

此子树的最优结果为 (value[a]-value[b])*cost[a->b] + (value[b]-value[c])*cost[b->c] 的最小值

因为value[c]=0.0 确定,那么value[b]的值要么为 0.0 要么为value[a] 这要取决于 cost[a->b] 和 cost[b->c]

的大小关系,value[] 差值为0的那一段可以去掉,然后如果a还有其他枝叶,要累加。

然后以这种思想逐步向上递归,直到根节点。看代码可以一目了然。

刚开始一直WA9,后来把多组输入改成单组就对了,URAL上的题多数为单组数据测试,但平时写的时候为了自己测试方便

就习惯写成多组的,一般也不会出问题,这题的第9组测试数据,应该是有多余的数据在最后,所有才会出错。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring> using namespace std; const int INF=0x3f3f3f3f;
const int N=100005;
int k[N],c[N];
int dp[N];
bool loaf[N];
int main()
{
//freopen("data.in","r",stdin);
int n;
scanf("%d",&n);
memset(loaf,true,sizeof(loaf));
for(int i=2;i<=n;++i)
{
scanf("%d %d",&k[i],&c[i]);
loaf[k[i]]=false;
}
for(int i=1;i<=n;++i)
if(loaf[i]) dp[i]=INF;
else dp[i]=0;
for(int i=n;i>=2;--i)
dp[k[i]]+=min(dp[i],c[i]);
printf("%.2f\n",(double)dp[1]);
return 0;
}

1472. Martian Army的更多相关文章

  1. poj 3069 Saruman's Army

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8477   Accepted: 4317 De ...

  2. poj3069 Saruman's Army

    http://poj.org/problem?id=3069 Saruman the White must lead his army along a straight path from Iseng ...

  3. POJ 2948 Martian Mining

    Martian Mining Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2251 Accepted: 1367 Descri ...

  4. R2D2 and Droid Army(多棵线段树)

    R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. UVA 1366 九 Martian Mining

    Martian Mining Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Sta ...

  6. POJ 3069 Saruman's Army(萨鲁曼军)

    POJ 3069 Saruman's Army(萨鲁曼军) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] Saruman ...

  7. 【LCA】CodeForce #326 Div.2 E:Duff in the Army

    C. Duff in the Army Recently Duff has been a soldier in the army. Malek is her commander. Their coun ...

  8. TZC 1472 逆置正整数,去前导零 (java一句话秒杀)

    逆置正整数 http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1472 时间限制(普通/Java ...

  9. URAL 1774 A - Barber of the Army of Mages 最大流

    A - Barber of the Army of MagesTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/v ...

随机推荐

  1. java(2)之前往对象村

    这次说一说面向对象与面向过程的区别以及面向对象的优点.

  2. 关于IONIC 报错 XX is not a function

    刚开始 做一个项目,总是报错"XX is not  a function"   最后发现 原因 ,   原来是 服务的 注入位置 有问题. angular.module(" ...

  3. spring监听器

    1,web.xml中配,因为tomcat启动web项目时先加载web.xml. 2,spring需要启动IOC容器才能为其他框架提供服务,spring是通过监听器来启动容器,初始化 这边启动它,也得告 ...

  4. 绝对炫的3D幻灯片-SLICEBOX

    绝对炫的3D幻灯片-SLICEBOX http://www.jq22.com/jquery-info31

  5. hadoop2.7.3配置文件中过时的属性

    过时的属性:Deprecated Properties 该列表保存于:hadoop-2.7.3-src\hadoop-common-project\hadoop-common\src\site\mar ...

  6. 在ionic/cordova中使用Form模型验证(w5cValidator)

    在构建ionic项目过程中,当我们创建一个类似表单提交的页面时,可能会对用户的输入内容做某些规则验证,通过后再执行提交处理. 在验证的过程中,为了提供较好的用户体验,可能希望有类似于jquery Va ...

  7. Mac 安装 MySQL

    在 Mac 下用 Homebrew 安装 MySQL, 网上的教程倒是很多,不过大多数都很默契地雷同.如果稍有点定制要求,就无从下手了. 我先也不免俗,从基本的开始: 一.首先安装 Homebrew ...

  8. 关于django访问默认后台admin的时候提示403错误,

    Forbidden (403) CSRF verification failed. Request aborted. You are seeing this message because this ...

  9. 特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?

    特许金融分析师 (CFA) 持证人现在一般在做什么工作?职业分布是怎样的?  陈雨桐 1. 全球范围: 根据 CFA 协会 2014 年 6 月的报告: CFA Institute has over ...

  10. Connection broken for id 62, my id = 70, error =

    启动费zokeeper失败,报错如下:Connection broken for id 62, my id = 70, error = 原因是因为zoo.cfg中server.id不正确. serve ...