【JSOI2014】支线剧情2
来源:互联网 发布:8051单片机引脚映射 编辑:程序博客网 时间:2024/04/29 16:53
Description
Solution
这题明显是DP。
设f[i][0]和f[i][1]表示以i为根的子树打不打标记的最小值。
设shu[x]表示x为根的子树有多少个叶子节点。
f[x][0]+=f[last[i]]+chang[i]*shu[last[i]]
f[x][0]的转移很显然,因为子节点与x要走叶节点的个数次。
f[x][1]的转移就有好几种情况:
1:儿子打了标记,那么走完儿子的f[last[i]][1]之后,还要重新从根节点走下来
2:儿子没打标记,走完儿子的f[last[i]][0]之后,还要重新从x开始
3:还可以选一个儿子打标记,其他儿子不打标记,就先走不打标记的儿子,然后走打标记的儿子。
然后就没有了。
Code
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>#define fo(i,a,b) for(i=a;i<=b;i++)#define rep(i,a) for(i=first[a];i;i=next[i])using namespace std;typedef long long ll;const int maxn=1e6+7;int i,j,k,l,t,n,m,son[maxn];int first[maxn],next[maxn],last[maxn],num,chang[maxn],deep[maxn];ll f[maxn][2],ans;int fa[maxn],shu[maxn],shun[maxn];void add(int x,int y,int z){ last[++num]=y,next[num]=first[x],first[x]=num;chang[num]=z;}void dfs(int x,int y){ if(!first[x])shu[x]=1; ll i,p=0,q=0; rep(i,x){ dfs(last[i],y+chang[i]); shu[x]+=shu[last[i]]; f[x][0]+=f[last[i]][0]+shu[last[i]]*chang[i]; p=min(min(p+f[last[i]][1]+y+chang[i],p+f[last[i]][0]+chang[i]*shu[last[i]]),q+chang[i]+f[last[i]][1]); q=f[x][0]; } f[x][1]=min(p,f[x][0]);}int main(){// freopen("fan.in","r",stdin); scanf("%d",&n); fo(i,1,n){ scanf("%d",&son[i]); fo(j,1,son[i]){ scanf("%d%d",&k,&l);add(i,k,l); } } dfs(1,0); ans=min(f[1][0],f[1][1]); printf("%lld\n",ans);}
1 0
- 【JSOI2014】支线剧情2
- 【JZOJ3861】【JSOI2014】支线剧情2
- [JZOJ3861]【JSOI2014】支线剧情2
- 【JSOI2014】【JZOJ 3861】支线剧情2
- 【JZOJ 3861】【JSOI2014】支线剧情2
- 【JSOI2014】支线剧情
- BZOJ3876 [Ahoi2014&Jsoi2014]支线剧情
- 3876: [Ahoi2014&Jsoi2014]支线剧情
- bzoj3876 [Ahoi2014&Jsoi2014]支线剧情
- 【jzoj3861】【JSOI2014】【支线剧情2 】【树形动态规划】
- BZOJ3876: [Ahoi2014&Jsoi2014]支线剧情(上下界费用流)
- [上下界有源汇最小费用可行流] BZOJ 3876: [Ahoi2014&Jsoi2014]支线剧情
- bzoj3876 [Ahoi2014&Jsoi2014]支线剧情(有源汇有上下界最小费用流)
- bzoj-3876 支线剧情
- bzoj3876【Ahoi2014】支线剧情
- [bzoj3876]支线剧情
- [bzoj3876][AHOI2014]支线剧情
- BZOJ3876/AHOI2014支线剧情
- Aop面向切面编程
- Linux14.04+cuda7.5+anaconda2+opencv3.0+caffe
- 数组之链表操作(用数组超时)
- APNs(Apple Push Notification server)服务器代码C#编写
- Ubuntu 14.04 LTS + Qt 4.7.3 交叉编译
- 【JSOI2014】支线剧情2
- BZOJ 1006 神奇的国度 弦图 最大势算法
- Software developer interview questions: The soft part
- [51nod 1222]最小公倍数计数
- multimap容器案例_按照部门_增删改查员工信息
- 对于长期学习方式的一点看法
- C# 通过URL获取图片并显示在PictureBox上的方法
- “error LNK2019: 无法解析的外部符号”的几种可能原因
- C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别