【洛谷2912】[USACO08OCT]牧场散步Pasture Walking
来源:互联网 发布:js window对象的方法 编辑:程序博客网 时间:2024/05/18 03:20
牧场散步Pasture Walking
题目描述
The N cows (2 <= N <= 1,000) conveniently numbered 1..Nare grazing among the N pastures also conveniently numbered 1..N. Mostconveniently of all, cow i is grazing in pasture i.
Some pairs of pastures are connected by one of N-1 bidirectionalwalkways that the cows can traverse. Walkway i connects pastures A_i and B_i (1<= A_i <= N; 1 <= B_i <= N) and has a length of L_i (1 <= L_i<= 10,000).
The walkways are set up in such a way that between any twodistinct pastures, there is exactly one path of walkways that travels betweenthem. Thus, the walkways form a tree.
The cows are very social and wish to visit each other often.Ever in a hurry, they want you to help them schedule their visits by computingthe lengths of the paths between 1 <= L_i <= 10,000 pairs of pastures(each pair given as a query p1,p2 (1 <= p1 <= N; 1 <= p2 <= N).
POINTS: 200
有N(2<=N<=1000)头奶牛,编号为1到W,它们正在同样编号为1到N的牧场上行走.为了方便,我们假设编号为i的牛恰好在第i号牧场上.
有一些牧场间每两个牧场用一条双向道路相连,道路总共有N - 1条,奶牛可以在这些道路上行走.第i条道路把第Ai个牧场和第Bi个牧场连了起来(1 <= A_i <= N; 1 <= B_i <= N),而它的长度是 1 <= L_i <= 10,000.在任意两个牧场间,有且仅有一条由若干道路组成的路径相连.也就是说,所有的道路构成了一棵树.
奶牛们十分希望经常互相见面.它们十分着急,所以希望你帮助它们计划它们的行程,你只需要计算出Q(1 < Q < 1000)对点之间的路径长度•每对点以一个询问p1,p2 (1 <= p1 <= N; 1 <= p2 <= N). 的形式给出.
输入输出格式
输入格式:
Line 1: Two space-separatedintegers: N and Q
Lines 2..N: Line i+1contains three space-separated integers: A_i, B_i, and L_i
Lines N+1..N+Q: Each line contains two space-separated integers representing two distinct pastures between which the cows wish to travel: p1 and p2
输出格式:
Lines 1..Q: Line i contains the length of the path between the two pastures in query i.
输入输出样例
输入样例#1:
4 2
2 1 2
4 3 2
1 4 3
1 2
3 2
输出样例#1:
2
7
dist保存的是到根节点的距离,两个点到根节点的距离再减去两次最近公共祖先到根节点的距离就是两点的距离。(稍微yy一下就能明白了=。=)
#include<iostream>#include<cstdio>#define N 50050using namespace std;int n,q,tot;int head[N],next[N],to[N],c[N],depth[N],dist[N],fa[N][32],du[N];void add_edge(int x,int y,int z){ to[++tot]=y; c[tot]=z; next[tot]=head[x]; head[x]=tot;}void dfs(int x,int yeye){ for (int i=head[x];i;i=next[i]) { int y=to[i]; if (y != yeye) { depth[y]=depth[x]+1; dist[y]=dist[x]+c[i]; fa[y][0]=x; for (int j=1;j<32;j++) fa[y][j]=fa[fa[y][j-1]][j-1]; dfs(y,x); } }}int lca(int x,int y){ if (depth[x]<depth[y]) swap(x,y); int d=depth[x]-depth[y]; for (int i=0;i<31;i++) if (d&(1<<i)) x=fa[x][i]; for (int i=31;i>=0;i--) if (fa[x][i] != fa[y][i]) x=fa[x][i],y=fa[y][i]; return (x == y?x:fa[x][0]);}int main(){ tot=0; cin>>n>>q; for (int i=1;i<n;i++) { int x,y,z; cin>>x>>y>>z; add_edge(x,y,z); add_edge(y,x,z); du[x]++;du[y]++; } for (int i=1;i<=n;i++) if (du[i] == 1) { depth[i]=1; dist[i]=0; dfs(i,0); break; } for (int i=0;i<q;i++) { int x,y; cin>>x>>y; cout<<(dist[x]+dist[y])-(2*dist[lca(x,y)])<<endl; } return 0;}
- 【洛谷2912】[USACO08OCT]牧场散步Pasture Walking
- 洛谷P2912 [USACO08OCT]牧场散步Pasture Walking
- [USACO08OCT]牧场散步Pasture Walking
- pasture三角形牧场 动态规划
- 【双重背包】三角形牧场 pasture
- 小P 的牧场(pasture)题解
- USACO08OCT
- 【Usaco Oct08 Gold】奶牛串门(Pasture Walking)(LCA)
- 【4连WA】【Usaco Oct08 Gold】奶牛串门(Pasture Walking)
- 【题】【倍增】NKOJ 1536 奶牛串门【Usaco Oct08 Gold】(Pasture Walking)
- 洛谷 P1550 [USACO08OCT]打井Watering Hole
- 散步
- 散步
- 洛谷1284 三角形牧场
- 洛谷 P1284 三角形牧场
- 洛谷 P1284 三角形牧场
- 洛谷 P1284 三角形牧场
- 洛谷1284 三角形牧场
- 洛谷P2925 [USACO08DEC]干草出售Hay For Sale
- Altium Designer 技巧备忘
- 虚继承初始化的底层实现
- 负载均衡集群LVS
- 随机性模型学习----- 蒙特卡洛模拟
- 【洛谷2912】[USACO08OCT]牧场散步Pasture Walking
- Java内省技术
- NOIP2017 考前汇总
- HDU 4403 A very hard Aoshu problem (DFS+状压)
- ImageLoader----苏苏的小弟子
- JAVA8高级新特性课程+Java Util Concurrency+Java NIO视频教程
- JZOJ5466. 【NOIP2017提高A组冲刺11.9】玩游戏
- Android全屏化
- 计算并输出1到n之间的所有数的平方和立方。