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 ...
随机推荐
- 【锋利的jQuery】学习笔记02
第二章 jQuery选择器 一.jQuery选择器的优势 写法简洁 $("div") 支持css2和css3选择器(对于css3选择器支持这一项,我认为应该是jQuery首先创造并 ...
- 什么时候用using (SPSite site = new SPSite(SPContext.Current.Web.Url))
并不是所有时候都适合用using(){},只有当需要提升用户的权限的时候才会用到using,其他时候都可以直接使用SPContext.Current.Web; using ...
- Centos6.5 安装 MariaDB-10.0.20-linux-x86_64.tar.gz
下载mariadb :https://downloads.mariadb.org/ 我选择mariadb-10.0.20-linux-x86_64.tar.gz这个版本 复制安装文件 /opt 目录 ...
- 多个互相有联系的checkbox的单选逻辑
checkbox单选的状态逻辑,状态好的时候一下就写好了,状态不好的时候要调试比较久,当然主要是对其中的事件不太清楚. 先给出效果图吧. 然后给出代码, selectZhiFuBaoPay.setOn ...
- Mvc网站开发知识
网站开发基本本步骤 mvc是一个设计模式 网站开发前台页面技术 页面设计:Html css+Div 页面特效:JavaScript,jQuery oop编程核心公共技能 c#基础语法与程序逻辑 面向对 ...
- Angularjs总结(一)表单验证
常用的表单验证1.必须字段html5特性 增加required <input type="text" required /> 2.最小长度和最大长度 <input ...
- Windows phone 之样式的关联
建议大家做界面要用Blend. 做过web的都知道DIV+CSS,给页面元素关联样式有三种方式: 1.内联样式表:就是在每个元素里面写style.优点就是灵活,给指定的元素添加样式.缺点是重用性很差, ...
- Windowsphone 之xml序列化和反序列化的应用(WebService解析返回的数据DataSet )
关于Xml的序列化和反序列化: 可以看这篇文章,http://www.cnblogs.com/Windows-phone/p/3243575.html WebService解析返回的数据DataSet ...
- SecureCRT、FileZilla使用Public证书登录Linux
本文作者:Vinkn ,转载请注明出处http://www.cnblogs.com/Vinkn/ 一.简介 服 务器部署之后一般都配置了防火墙,一般也都开启了ssh,可以直接登录远程服务器,为了安全等 ...
- 在本地Apache服务器配置虚拟主机站点
Apache 配置localhost虚拟主机步骤1,打开apache目录下httpd.conf文件,找到如下模块 # Virtual hosts #Include conf ...