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. 「 JavaScript 篇 」

    一.JavaScript 里有哪些数据类型,解释清楚 null 和 undefined,解释清楚原始数据类型和引用数据类型.比如讲一下 1 和 Number(1)的区别js中有5种数据类型:Undef ...

  2. HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容

    今天把一个.NET的网站部署到IIS上打开网页的时候出现了这个错误,刚开始以为是没有配置默认页,但是直接打开固定的页面地址也不行. 于是怀疑是.NET版本的问题,但是看了一下程序的目标框架是4.0没错 ...

  3. [OC][地图] 高德地图之定位初探(一)

    使用前的说明 高德地图开放平台的iOS定位模块网址-->http://lbs.amap.com/api/ios-location-sdk/summary/ 高德地图有Web端.android平台 ...

  4. 2014鸟人Birdman中文字幕文件下载

    下载后,解压缩,会得到 Birdman.2014.1080p.WEB-DL.DD5.1.H264-RARBG.srt 文件,把这个文件放到视频文件(mkv文件)相同的文件夹里. 用暴风影音播放,如果没 ...

  5. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. C语言习题(结构)

    实际应用中经常会用到二维平面上的点,点的操作包括设置点的位置( pointT setPoint(double x , double y ) ),显示第n个点的位置( void showPoint(po ...

  7. NodeJS 初体验

    console.log('%s: %d', 'Hello', 25);  // 可以像C语言格式一样输出//app.jsvar http = require('http');http.createSe ...

  8. Spring的xml文件配置方式实现AOP

    配置文件与注解方式的有很大不同,多了很多配置项. beans2.xml <?xml version="1.0" encoding="UTF-8"?> ...

  9. eclipse rcp 打包出适合不同操作系统和操作位数.

    http://blog.csdn.net/luoww1/article/details/8677999 http://blog.csdn.net/soszou/article/details/8053 ...

  10. flask笔记3-模板

    flask框架使用jinja2模板引擎.简单的说,模板就是一个纯html文件中夹杂着占位符,在渲染模板时用真实变量值替换占位符,就形成了最终的前台页面. 1.模板存放位置: 默认情况下,flask在根 ...