[bzoj 1060][luogu p1131]时态同步
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[bzoj 1060][luogu p1131]时态同步相关的知识,希望对你有一定的参考价值。
(似乎是)第一次做树形DP吔,好鸡冻~
题目大意:
一棵有根边权树,修改边权值代价为边权值变化量,目标状态是每个叶子到根的路径权值相等.求达到目标状态耗费的最小代价.
要把每条路径的权值加到最大权值,可以证明它最优然而我不会,递归处理子树即可.
代码如下:
1 #include <cstdio> 2 #include <cstring> 3 #define max(a,b) (((a) > (b)) ? (a) : (b)) 4 #define mcl(a) memset(a,0,sizeof(a)) 5 const int MAXS = 60 * 1024 * 1024,N = 500005; 6 inline char __getchar() 7 { 8 static char buf[MAXS];static int len = 0,pos = 0; 9 if(pos == len)pos = 0,len = fread(buf,1,MAXS,stdin);if(pos == len)return ‘\\0‘;return buf[pos++]; 10 } 11 inline int getint() 12 { 13 int register num = 0;char register ch = __getchar();for(;ch < ‘0‘ || ch > ‘9‘;ch = __getchar()); 14 for(;ch >= ‘0‘ && ch <= ‘9‘;ch = __getchar())num = (num << 3) + (num << 1) + (ch ^ ‘0‘);return num; 15 } 16 int n,s,cnt = 0,head[N];long long f[N],ans; 17 struct edge{int next,to,val;}e[N << 1]; 18 void addedge(int u,int v,int w){e[++cnt].to = v;e[cnt].next = head[u];head[u] = cnt;e[cnt].val = w;} 19 void dfs(int u,int fa) 20 { 21 for(int register i = head[u];i;i = e[i].next) 22 { 23 int register v = e[i].to;if(v == fa)continue; 24 dfs(v,u); 25 f[u] = max(f[u],f[v] + e[i].val); 26 } 27 for(int register i = head[u];i;i = e[i].next) if(fa != e[i].to) ans += f[u] - f[e[i].to] - e[i].val; 28 } 29 int main() 30 { 31 mcl(head);mcl(e);mcl(f); 32 n = getint();s = getint(); 33 for(int register i = 1;i < n;i++){int register a,b,t;a = getint();b = getint();t = getint();addedge(a,b,t);addedge(b,a,t);} 34 dfs(s,0);printf("%lld\\n",ans); 35 return 0; 36 }
用了读优之后神清气爽,
BZOJ上432ms Rk15,luogu上48ms Rk 3.
(辣鸡B(婊)Z(子)O(口)J(丁))
以上是关于[bzoj 1060][luogu p1131]时态同步的主要内容,如果未能解决你的问题,请参考以下文章