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程序操作hbase的Maven配置pom.xml文件

    由于工作需要,用到Java连接Hbase读写数据.原来的方式是直接将依赖的jar包全部下载下来,然后工程里面引用. 现想重构下,改为使用Maven管理,遇到了各种坑,各种不好使,特总结下. <d ...

  2. 头一次试验angularjs

    <!DOCTYPE html> <head> <meta charset="utf-8" /> <title>angular< ...

  3. 长时间停留在calculating requirements and dependencies 解决方案

    如果Eclipse花费了很长的时间calculating requirements and dependencies(计算需求和依赖性 ) 这个问题通常就是在点击安装之后显示“Calculating ...

  4. 16-网易-intership

    1.多选 //HTML <p>很长的一段文字,很长的一段文字,很长的一段文字,特别长的文字</p> //CSS p{ width:100px; white-space:nowr ...

  5. Linux中设置服务自启动的三种方式

    有时候我们需要Linux系统在开机的时候自动加载某些脚本或系统服务 主要用三种方式进行这一操作: ln -s                       在/etc/rc.d/rc*.d目录中建立/e ...

  6. Java输入输出流进阶

    输入输出的内容是文本内容,考虑使用字符流. 输入输出的内容是二进制内容,考虑使用字节流. 凡是能用记事本打开并查看的内容称为文本文件,反之则为二进制文件. package ch15; import j ...

  7. HTML的基本骨架

    HTML:生命html网页当前范围,所有内容写在该标签中 head:用来设置当前页面的一些属性及样式等等.这部分是用户看不到的,title除外. body:网页的内容部分,网页中所有布局写在该标签中. ...

  8. 基于Eclipse+Cordova的Android Hybrid应用开发环境搭建

    环境说明 操作系统:Windows 7 64位 Eclipse版本:4.5.2 Release(eclipse-jee-mars-2) JDK版本:1.8 搭建步骤 1.从http://www.ecl ...

  9. [转]Android开源项目第二篇——工具库篇

    本文为那些不错的Android开源项目第二篇--开发工具库篇,主要介绍常用的开发库,包括依赖注入框架.图片缓存.网络相关.数据库ORM建模.Android公共库.Android 高版本向低版本兼容.多 ...

  10. .net 导出Excel功能

    将DataSet对象导出成Excel文档 一.不带格式控制 void btnExport_Click(object sender, EventArgs e) { IList<string> ...