[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=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch==‘-‘) ok=1;
    for (x=0;isdigit(ch);x=x*10+ch-‘0‘,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 0;return x;}
void dfs(int u,int last){
    if (bo[u][last]) return;
    bo[u][last]=1;
    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=0;i<=k;i++){
            f[u][last][i]=inf;
            for (int j=0;j<=i;j++){
                if (i>j) f[u][last][i]=min(f[u][last][i],tmp[u][last][i-j-1]+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=1;i<=n;i++) read(w[i]),read(b),read(c),put(b,i,c);
    dfs(0,0);
    printf("%d\n",f[0][0][k]);
    return 0;
}
时间: 2025-01-11 20:28:17

[Ioi2005]River的相关文章

BZOJ1812 [IOI2005]river

传送门: 很常规的一道树规,转为左儿子右兄弟. 然后f[node][anc][K]表示在node节点上,最近的有贡献祖先在anc上,在node的儿子和兄弟上有k个有贡献节点的最优值. 然后得出以下转移方程. f[node][anc][K]=min{f[son[node]][anc][k]+f[bro[node]][anc][K-k]}+Value[node]*(dis[node]-dis[anc]); 无贡献 f[node][anc][K]=min{f[son[node]][node][k]+f

树形动态规划专题

1.OJ1278战略游戏 f[u][0]代表以u为根的子树,u不放时,最少放置节点数. f[u][1]代表以u为根的子树,u放时,最少放置节点数. f[u][0]=Σf[son][1]. f[u][1]=Σmin(f[son][1],f[son][0]). ans=min(f[root][0],f[root][1]). #include<cstdio> #include<iostream> using namespace std; const int maxn=1500; int

[BZOJ1617][Usaco2008 Mar]River Crossing渡河问题

1617: [Usaco2008 Mar]River Crossing渡河问题 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1102  Solved: 801 [Submit][Status][Discuss] Description Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏. 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木筏上.在这个基础上,木筏上的奶牛数

洛谷—— P2904 [USACO08MAR]跨河River Crossing

https://www.luogu.org/problem/show?pid=2904 题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride

P2904 [USACO08MAR]跨河River Crossing

P2904 [USACO08MAR]跨河River Crossing 题目描述 Farmer John is herding his N cows (1 <= N <= 2,500) across the expanses of his farm when he finds himself blocked by a river. A single raft is available for transportation. FJ knows that he must ride on the ra

bzoj1650[Usaco2006 Dec]River Hopscotch 跳石子*

bzoj1650[Usaco2006 Dec]River Hopscotch 跳石子 题意: 数轴上有n个石子,第i个石头的坐标为Di,现在要从0跳到L,每次跳都从一个石子跳到相邻的下一个石子.现在问移走这M个石子后,相邻两个石子及0到最前一个石子及最后一个石子到L距离的最小值的最大值是多少.n≤50000 题解: 为什么有NOIP2015即视感~二分距离最小值,然后如果当前石子和上一个石子相差小于二分值就将这个石子移走,如果位置L与上一个石子相差小于二分值,此时若还没有移满M个且有没移走的石子

CodeForces The Endless River

The Endless River Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description standard input/outputStatements You've possibly heard about 'The Endless River'. However, if not, we are introducing it to you. The Endless Riv

River Hopscotch-[二分查找、贪心]

Description Every year the cows hold an event featuring a peculiar version of hopscotch that involves carefully jumping from rock to rock in a river. The excitement takes place on a long, straight river with a rock at the start and another rock at th

POJ 1700 cross river (数学模拟)

 Crossing River Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10311   Accepted: 3889 Description A group of N people wishes to go across a river with only one boat, which can at most carry two persons. Therefore some sort of shuttle