[Ioi2005]River
2015-10-26 20:50
267 查看
![](http://images2015.cnblogs.com/blog/697201/201510/697201-20151026203347138-1573812222.jpg)
![](http://images2015.cnblogs.com/blog/697201/201510/697201-20151026203354325-1959131871.jpg)
设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; }
相关文章推荐
- 两个字符窜,在母窜中查找子窜的位置
- ubuntu12.04TLS安装CodeBlocks
- TDA线程分析初步
- linux下安装python后无法执行错误解决
- android实现超链接的3中方法
- OC第一天:类和对象
- ANDROID OKHTTP MVP
- 对称矩阵的压缩存储及基本运算(1)
- 黑马程序员****C语言基础****C语言的基本概念
- System.DllNotFoundException Unable to load DLL SQLite.Interop.dll解决办法
- 顶层const与底层const
- C#中登录界面常用方法,回车键直接登录
- 文件系统fsd hook (一)原理
- 团“模子”政策模式乱舞
- 使用D3 Geo模块画澳大利亚地图
- Codeforces Round #326
- 关于viewpager中嵌套的fragment中的listview的横向滑动的滑动冲突的问题。
- ural 1289. One Way Ticket
- C++:调整基类成员在派生类中的访问属性的其他方法(同名成员和访问声明)
- ScrollView定时器复用