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 ...
随机推荐
- bootstrap-datetimepicker 时间表箭头不能显示
我使用的是bootstrap-datetimepicker+bootstrap v3,但这个插件使用的时候,并没有和V3相匹配,仍然调用的是bootstrap V2的图标,代码是: <i cla ...
- 训练趣题:黑与白 有A、B、C、D、E五人,每人额头上都帖了一张黑或白的纸。(此处用javascript实现)
今天的题目原题是这样的: “ 黑与白:有A.B.C.D.E五人,每人额头上都帖了一张黑或白的纸.五人对坐,每人都可以看到其它人额头上的纸的颜色.五人相互观察后,A说:“我看见有三人额头上帖的是白纸,一 ...
- jmeter中webdriver插件,进行自动化压测
1.下载JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache-jmeter ...
- 通过C#去调用C++编写的DLL
这个问题缠了我2个小时才弄出来,其实很简单.当对方提供一个dll给你使用时,你需要去了解这个dll 是由什么语言写的,怎么编译的,看它的编译类型.这样即使在没有头绪时,你可以先尝使用一些比较热门的编译 ...
- C语言数据输入与输出
1 概论 C语言提供了跨平台的数据输入输出函数scanf()和printf()函数,它们可以按照指定的格式来解析常见的数据类型,例如整数,浮点数,字符和字符串等等.数据输入的来源可以是文件,控制台以及 ...
- hdu 1396 Counting Triangles(递推)
Counting Triangles Problem Description Given an equilateral triangle with n thelength of its side, p ...
- Eclipse debug调试
Eclipse debug调试: F5:跳入方法F6:向下逐行调试F7:跳出方法F8:直接跳转到下一个断点
- LINUX 下mysql数据库导出
mysqldump -u root -p dbname > db.sql
- win8.1磁盘使用率100解决方法
关闭家庭组,因为这功能会导致硬盘和CPU处于高负荷状态.关闭方法:Win+C – 设置 – 更改电脑设置 – 家庭组 – 离开如果用不到家庭组可以直接把家庭组服务也给关闭了:控制面板 – 管理工具 – ...
- 关于反射Assembly.Load("程序集").CreateInstance("命名空间.类")
关于反射Assembly.Load("程序集").CreateInstance("命名空间.类") 而不管在哪一层写这段代码其中的("程序集" ...