[Ioi2005]River
设f[i][j][k]表示i上游最近的一个伐木场为j且在i所在的子树里共建了k个伐木场(不包含在i的)的最小运费和
设v为u的儿子,dist[u]为u到0号点的距离。
则当i>=j时 f[u][last][i]=max{f[u][last][i-j]+dist[v][last][j]+w[v]*(dist[v]-dist[last])} 即在v不放伐木场
当i>j时 f[u][last][i]=max{f[u][last][i-j-1]+f[v][v][j]} 即在v放伐木场
code:
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 105
#define inf 1061109567
using namespace std;
char ch;
int n,k,a,b,c,tot,w[maxn],now[maxn],son[maxn],pre[maxn],val[maxn];
int dist[maxn],f[maxn][maxn][maxn],tmp[maxn][maxn][maxn];
bool ok,bo[maxn][maxn];
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void put(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
int turn(int x){if (x==inf) return ;return x;}
void dfs(int u,int last){
if (bo[u][last]) return;
bo[u][last]=;
for (int p=now[u],v=son[p];p;p=pre[p],v=son[p]){
dist[v]=dist[u]+val[p];
dfs(v,last),dfs(v,v);
for (int i=;i<=k;i++){
f[u][last][i]=inf;
for (int j=;j<=i;j++){
if (i>j) f[u][last][i]=min(f[u][last][i],tmp[u][last][i-j-]+f[v][v][j]);
f[u][last][i]=min(f[u][last][i],tmp[u][last][i-j]+f[v][last][j]+w[v]*(dist[v]-dist[last]));
}
}
memcpy(tmp[u][last],f[u][last],sizeof(tmp[u][last]));
}
}
int main(){
read(n),read(k);
for (int i=;i<=n;i++) read(w[i]),read(b),read(c),put(b,i,c);
dfs(,);
printf("%d\n",f[][][k]);
return ;
}
[Ioi2005]River的更多相关文章
- BZOJ1812 [IOI2005]river
传送门: 很常规的一道树规,转为左儿子右兄弟. 然后$f[node][anc][K]$表示在node节点上,最近的有贡献祖先在anc上,在node的儿子和兄弟上有k个有贡献节点的最优值. 然后得出以下 ...
- [IOI2005]River 河流
题目大意: 给定n个点的有根树,每条边有边权,每个点有点权w, 你要在k个点上建立伐木场,对于每个没有建伐木场的点x,令与它最近的祖先.有伐木场的点,为y,你需要支付dis(x,y)*w[x]的代价. ...
- [学习笔记]对未来做出承诺的DP小结
这是一种DP状态设计方法. 有些题,当你必须以一个顺序往后填的话,然而后面的填法会对之前产生影响,那么,不妨在之前就对未来怎么填做出承诺. 通俗的讲,就是对未来打一个表. 然后后面填的时候,直接查表转 ...
- Moon River
读书笔记系列链接地址http://www.cnblogs.com/shoufengwei/p/5714661.html. 昨晚无意中听到了一首英文歌曲,虽不知其意,但是瞬间就被优美的旋律 ...
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- Crossing River
Crossing River 题目链接:http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=26251 题意: N个人希望去过 ...
- BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)
蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...
- POJ 3258 River Hopscotch
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11031 Accepted: 4737 ...
- River Hopscotch(二分POJ3258)
River Hopscotch Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9263 Accepted: 3994 Descr ...
随机推荐
- Spring 3.x企业应用开发实战(9-1)----依赖注入
Spring中的依赖注入方式:属性注入.构造函数注入和工厂方式注入. 1.属性注入 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象. 属性注入要求Bean提供一个默认的构造函数,在J ...
- python获取本机IP、mac地址、计算机名
在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: 1 2 3 4 import uuid de ...
- UNIX编程之冲洗内存流与null追加策略(APUE F5-15)
博文链接:http://haoyuanliu.github.io/2016/04/29/mysql/ 对,我是来骗访问量的!O(∩_∩)O~~ 最近一直在拜读APUE(Advanced Program ...
- 解决"the currently displayed page contains invalid values"
原因是你的工程的根目录少了default.properties(有点项目工程这个文件名称是project.properties)这个文件,导致不能选择target: 解决办法: 在工程根目录下建立 ...
- Android--图片的三级缓存策略
三级缓存缓存(内存)--->本地磁盘---->网络1.首先看一下图片存储到本地磁盘 public class FileUtils { String path;//文件存储的地方 publi ...
- sdaf
- php 链式操作的实现 学习记录
php 面向对象中实现链式操作的关键部分:调用的方法中返回当前对象 ,从而实现链式操作: <?php namespace commom; class db { public function w ...
- javascript MD5加密
/* * Javascript MD5 library - version 0.4 * * Coded (2011) by Luigi Galli - LG@4e71.org - * http://f ...
- 生成package.json和bower.json
1.安装nodejs 2.安装bower工具 cmd:npm bower install 3.生成package.json cmd:npm init 4.生成bower.json cmd:bow ...
- Android开发手记(24) Log的使用及颜色的更改
在程序开发过程中,LOG是广泛使用的用来记录程序执行过程的机制,它既可以用于程序调试,也可以用于产品运营中的事件记录.在Android系统中,提供了简单.便利的LOG机制,开发人员可以方便地使用.本文 ...