BZOJ1602: [Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 1084 Solved: 556
[Submit][Status]
Description
N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草。 这n块土地被n-1条边连接。 奶牛可以在边上行走,第i条边连接第Ai,Bi块牧场,第i条边的长度是Li(1<=Li<=10000)。 这些边被安排成任意两头奶牛都可以通过这些边到达的情况,所以说这是一棵树。 这些奶牛是非常喜欢交际的,经常会去互相访问,他们想让你去帮助他们计算Q(1<=q<=1000)对奶牛之间的距离。
Input
*第一行:两个被空格隔开的整数:N和Q
*第二行到第n行:第i+1行有两个被空格隔开的整数:AI,BI,LI
*第n+1行到n+Q行:每一行有两个空格隔开的整数:P1,P2,表示两头奶牛的编号。
Output
*第1行到第Q行:每行输出一个数,表示那两头奶牛之间的距离。
Sample Input
2 1 2
4 3 2
1 4 3
1 2
3 2
Sample Output
7
HINT
Source
const maxn=+;
type node1=record
go,next,z:longint;
end;
node2=record
l,r,mid,sum:longint;
end; var e:array[..*maxn] of node1;
t:array[..*maxn] of node2;
p,a,v,fa,s,head,dep,son,top:array[..maxn] of longint;
i,n,m,x,y,z,sz,ans,tot:longint;
ch:char;
procedure swap(var x,y:longint);
var t:longint;
begin
t:=x;x:=y;y:=t;
end;
procedure insert(x,y,z:longint);
begin
inc(tot);
e[tot].go:=y;e[tot].z:=z;e[tot].next:=head[x];head[x]:=tot;
end;
function min(x,y:longint):longint;
begin
if x<y then exit(x) else exit(y);
end;
function max(x,y:longint):longint;
begin
if x>y then exit(x) else exit(y);
end; procedure dfs1(x:longint);
var i,j,y:longint;
begin
j:=;s[x]:=;
i:=head[x];
while i<> do
begin
y:=e[i].go;
if dep[y]= then
begin
dep[y]:=dep[x]+;
fa[y]:=x;v[y]:=e[i].z;
dfs1(y);
inc(s[x],s[y]);
if s[y]>s[j] then j:=y;
end;
i:=e[i].next;
end;
son[x]:=j;
end;
procedure dfs2(x,chain:longint);
var i,y:longint;
begin
inc(sz);p[x]:=sz;top[x]:=chain;
if son[x]<> then dfs2(son[x],chain);
i:=head[x];
while i<> do
begin
y:=e[i].go;
if (p[y]=) and (y<>son[x]) then dfs2(y,y);
i:=e[i].next;
end;
end;
procedure pushup(k:longint);
begin
t[k].sum:=t[k<<].sum+t[k<<+].sum;
end; procedure build(k,x,y:longint);
begin
with t[k] do
begin
l:=x;r:=y;mid:=(l+r)>>;
if l=r then begin sum:=a[l];exit;end;
build(k<<,l,mid);build(k<<+,mid+,r);
pushup(k);
end;
end;
function getsum(k,x,y:longint):longint;
begin
with t[k] do
begin
if (l=x) and (r=y) then exit(sum);
if y<=mid then exit(getsum(k<<,x,y))
else if x>mid then exit(getsum(k<<+,x,y))
else exit(getsum(k<<,x,mid)+getsum(k<<+,mid+,y));
end;
end;
procedure init;
begin
readln(n,m);
for i:= to n- do begin readln(x,y,z);insert(x,y,z);insert(y,x,z);end;
dep[]:=;
dfs1();
dfs2(,);
for i:= to n do a[p[i]]:=v[i];
build(,,n);
end;
procedure solvesum;
begin
ans:=;
readln(x,y);
while top[x]<>top[y] do
begin
if dep[top[x]]<dep[top[y]] then swap(x,y);
inc(ans,getsum(,p[top[x]],p[x]));
x:=fa[top[x]];
end;
if dep[x]>dep[y] then swap(x,y);
inc(ans,getsum(,p[x],p[y]));
if p[x]<> then dec(ans,v[x]);
writeln(ans);
end; procedure main;
begin
for i:= to m do solvesum;
end; begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
main;
close(input);close(output);
end.
BZOJ1602: [Usaco2008 Oct]牧场行走的更多相关文章
- [BZOJ1602] [Usaco2008 Oct] 牧场行走 (LCA)
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...
- 【bzoj1602】[Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1793 Solved: 935[Submit][St ...
- bzoj 1602 [Usaco2008 Oct]牧场行走(LCA模板)
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 379 Solved: 216[Submit][Sta ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走( 最短路 )
一棵树..或许用LCA比较好吧...但是我懒...写了个dijkstra也过了.. ---------------------------------------------------------- ...
- 1602: [Usaco2008 Oct]牧场行走
1602: [Usaco2008 Oct]牧场行走 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1211 Solved: 616 [Submit][ ...
- 【BZOJ】1602: [Usaco2008 Oct]牧场行走(lca)
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 一开始以为直接暴力最短路,但是n<=1000, q<=1000可能会tle. 显然 ...
- BZOJ 1602: [Usaco2008 Oct]牧场行走 倍增裸题
Description N头牛(2<=n<=1000)别人被标记为1到n,在同样被标记1到n的n块土地上吃草,第i头牛在第i块牧场吃草. 这n块土地被n-1条边连接. 奶牛可以在边上行走, ...
- BZOJ——1602: [Usaco2008 Oct]牧场行走 || 洛谷—— P2912 [USACO08OCT]牧场散步Pasture Walking
http://www.lydsy.com/JudgeOnline/problem.php?id=1602 || https://www.luogu.org/problem/show?pid=2912 ...
- LCA || BZOJ 1602: [Usaco2008 Oct]牧场行走 || Luogu P2912 [USACO08OCT]牧场散步Pasture Walking
题面:[USACO08OCT]牧场散步Pasture Walking 题解:LCA模版题 代码: #include<cstdio> #include<cstring> #inc ...
随机推荐
- 28、Jquery 页面效果
Jquery动画函数分为三类 基本动画函数 基本动画函数包括show(显示).hide(隐藏).toggle(切换)这三个函数. 例子所需html <input type="butto ...
- 自动显示git分支--安装oh-my-zsh(Ubuntu环境)
1,安装zsh sudo apt-get install zsh 2,克隆项目 git clone git://github.com/robbyrussell/oh-my-zsh.git ~/.oh- ...
- TransactionScope简单用法
记录TransactionScope简单用法,示例如下: void Test() { using (TransactionScope scope = new TransactionScope()) { ...
- chrome跨域配置
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security --use ...
- Handler 原理分析和使用(一)
我为什么写Handler,原因主要还在于它在整个 Android 应用层面非常之关键,他是线程间相互通信的主要手段.最为常用的是其他线程通过Handler向主线程发送消息,更新主线程UI. 下面是一个 ...
- java - import *
以前看过很多视频,现在发觉很多讲师讲的有些地方是错的,在这里就说一下 import *,例如: import java.util.*的时候,表示的是将文件中使用到的类(而不是全部类)导入,例如在imp ...
- jquery 关于event.target使用的几点说明介绍
event.target说明:引发事件的DOM元素. this和event.target的区别js中事件是会冒泡的,所以this是可以变化的,但event.target不会变化,它永远是直接接受事件的 ...
- JavaScript H5 Canvas
Canvas 由于浏览器对HTML5标准支持不一致,所以,通常在<canvas>内部添加一些说明性HTML代码,如果浏览器支持Canvas,它将忽略<canvas>内部的HTM ...
- Bootstrap_排版_表格
一.基础表格 <table class="table"> <thead> <tr> <th>表格标题</th> < ...
- 《React-Native系列》38、 ReactNative混合组件封装
在我们做ReactNative项目的过程中,我们会发现由ReactNative提供给我们的组件或API往往满足不了我们的需求,常常需要我们自己去封装Native组件. 今天我们介绍下如果封装一个简单的 ...