Description
小Q在电子工艺实习课上学习焊接电路板。一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号。电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅存在一条通路(通路指连接两个元件的导线序列)。在电路板上存在一个特殊的元件称为“激发器”。当激发器工作后,产生一个激励电流,通过导线传向每一个它所连接的节点。而中间节点接收到激励电流后,得到信息,并将该激励电流传向与它连接并且尚未接收到激励电流的节点。最终,激烈电流将到达一些“终止节点”——接收激励电流之后不再转发的节点。激励电流在导线上的传播是需要花费时间的,对于每条边e,激励电流通过它需要的时间为te,而节点接收到激励电流后的转发可以认为是在瞬间完成的。现在这块电路板要求每一个“终止节点”同时得到激励电路——即保持时态同步。由于当前的构造并不符合时态同步的要求,故需要通过改变连接线的构造。目前小Q有一个道具,使用一次该道具,可以使得激励电流通过某条连接导线的时间增加一个单位。请问小Q最少使用多少次道具才可使得所有的“终止节点”时态同步?
Input
第一行包含一个正整数N,表示电路板中节点的个数。 第二行包含一个整数S,为该电路板的激发器的编号。 接下来N-1行,每行三个整数a , b , t。表示该条导线连接节点a与节点b,且激励电流通过这条导线需要t个单位时间。
Output
仅包含一个整数V,为小Q最少使用的道具次数。
Sample Input
3
1
1 2 1
1 3 3
Sample Output
2
【数据规模】
对于40%的数据,N ≤ 1000
对于100%的数据,N ≤ 500000
对于所有的数据,te ≤ 1000000
其实题目是比较简单的,但是由于出题人标程打错了,爆了int,出题人除了ans开了long long,其他都是int,p党不好过呀,要么cheat,要么模拟C++爆int
把激发器做根,树dp
先算出每个节点往下延伸的最大深度,再把其他儿子调整成这个深度,这个可以证明是最优策略
要写BFS不然会爆栈的
原题本来是可以用这个过的
1 {$M 5000000} 2 const 3 maxn=500010; 4 var 5 f:array[0..maxn]of int64; 6 t:array[0..maxn*2]of int64; 7 first:array[0..maxn]of longint; 8 next,last:array[0..maxn*2]of longint; 9 flag:array[0..maxn]of boolean; 10 n,s,tot:longint; 11 ans:int64; 12 13 procedure insert(x,y,z:longint); 14 begin 15 inc(tot); 16 last[tot]:=y; 17 next[tot]:=first[x]; 18 first[x]:=tot; 19 t[tot]:=z; 20 end; 21 22 procedure init; 23 var 24 i,x,y,z:longint; 25 begin 26 read(n,s); 27 for i:=1 to n-1 do 28 begin 29 read(x,y,z); 30 insert(x,y,z); 31 insert(y,x,z); 32 end; 33 end; 34 35 procedure dfs(x:longint); 36 var 37 i:longint; 38 begin 39 i:=first[x]; 40 flag[x]:=true; 41 while i<>0 do 42 begin 43 if flag[last[i]]=false then 44 begin 45 dfs(last[i]); 46 if f[x]<f[last[i]]+t[i] then f[x]:=f[last[i]]+t[i]; 47 end; 48 i:=next[i]; 49 end; 50 i:=first[x]; 51 while i<>0 do 52 begin 53 if flag[last[i]]=false then inc(ans,f[x]-f[last[i]]-t[i]); 54 i:=next[i]; 55 end; 56 flag[x]:=false; 57 end; 58 59 begin 60 init; 61 dfs(s); 62 write(ans); 63 end.
但是没办法,用c++写了一个(爆int)
1 #include<cstdio> 2 using namespace std; 3 4 const int maxn=500010; 5 6 int f[maxn],first[maxn],t[maxn*2],next[maxn*2],last[maxn*2],fa[maxn]; 7 bool flag[maxn]; 8 int n,s,tot; 9 long long ans; 10 11 void insert(int x,int y,int z) 12 { 13 ++tot; 14 last[tot]=y; 15 next[tot]=first[x]; 16 first[x]=tot; 17 t[tot]=z; 18 } 19 20 void init() 21 { 22 int i,x,y,z; 23 scanf("%d%d",&n,&s); 24 for(i=1;i<n;++i) 25 { 26 scanf("%d%d%d",&x,&y,&z); 27 insert(x,y,z); 28 insert(y,x,z); 29 } 30 } 31 32 int q[maxn]; 33 int head,tail; 34 35 void bfs() 36 { 37 int i,j; 38 head=1,tail=1; 39 q[1]=s; 40 for(i=1;i<=n;++i)flag[i]=true; 41 while(head<=tail) 42 { 43 flag[q[head]]=false; 44 i=first[q[head]]; 45 while(i!=0) 46 { 47 if(flag[last[i]])q[++tail]=last[i]; 48 else fa[q[head]]=last[i]; 49 i=next[i]; 50 } 51 ++head; 52 } 53 for(i=tail;i>0;i--) 54 { 55 j=first[q[i]]; 56 while(j!=0) 57 { 58 if(last[j]!=fa[q[i]] & f[q[i]]<f[last[j]]+t[j])f[q[i]]=f[last[j]]+t[j]; 59 j=next[j]; 60 } 61 j=first[q[i]]; 62 while(j!=0) 63 { 64 if(last[j]!=fa[q[i]])ans+=f[q[i]]-f[last[j]]-t[j]; 65 j=next[j]; 66 } 67 } 68 } 69 70 int main() 71 { 72 init(); 73 bfs(); 74 printf("%lld",ans); 75 return 0; 76 }