zoukankan      html  css  js  c++  java
  • BZOJ-1602: [Usaco2008 Oct]牧场行走 (LCA炒鸡大裸题)

    1602: [Usaco2008 Oct]牧场行走

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 2127  Solved: 1125
    [Submit][Status][Discuss]

    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

    4 2
    2 1 2
    4 3 2
    1 4 3
    1 2
    3 2

    Sample Output

    2
    7

    HINT

     

    Source

    貌似这题是当年初识LCA的时候给的样板题……laj第一次交的时候dfs忘记给deep[adj[i]]赋值了 _(:зゝ∠)_ 真鸡儿丢人退群吧……
     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1005;
     5 int n,m;
     6 int tot,head[MAX],adj[MAX<<1],wei[MAX<<1],next[MAX<<1];
     7 int dis[MAX],fa[MAX][21],deep[MAX];
     8 inline int read(){
     9     int an=0,x=1;char c=getchar();
    10     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    11     while (c>='0' && c<='9') {an=(an<<3)+(an<<1)+c-'0';c=getchar();}
    12     return an*x;
    13 }
    14 void addedge(int u,int v,int w){
    15     tot++;adj[tot]=v,wei[tot]=w,next[tot]=head[u],head[u]=tot;
    16 }
    17 void dfs(int x,int ff){
    18     int i,j;
    19     for (i=1;i<=20;i++){
    20         if (deep[x]<(1<<i)) break;
    21         fa[x][i]=fa[ fa[x][i-1] ][i-1];
    22     }
    23     for (i=head[x];i;i=next[i]){
    24         if (adj[i]==ff) continue;
    25         fa[adj[i]][0]=x;dis[adj[i]]=dis[x]+wei[i];deep[adj[i]]=deep[x]+1;
    26         dfs(adj[i],x);
    27     }
    28 }
    29 int lca(int x,int y){
    30     if (deep[x]<deep[y]) swap(x,y);
    31     int i,j,dd=deep[x]-deep[y];
    32     for (i=20;i>=0;i--)
    33         if (dd&(1<<i))
    34             x=fa[x][i];
    35     for (i=20;i>=0;i--)
    36         if (fa[x][i]!=fa[y][i])
    37             x=fa[x][i],y=fa[y][i];
    38     return x==y?x:fa[x][0];
    39 }
    40 int main(){
    41     freopen ("walk.in","r",stdin);freopen ("walk.out","w",stdout);
    42     int i,j,u,v,w;
    43     n=read(),m=read();
    44     for (i=1;i<n;i++){
    45         u=read(),v=read(),w=read();
    46         addedge(u,v,w),addedge(v,u,w);
    47     }
    48     dfs(1,0);
    49     for (i=1;i<=m;i++){
    50         u=read(),v=read();
    51         w=lca(u,v);
    52         printf("%d
    ",dis[u]+dis[v]-2*dis[w]);
    53     }
    54     return 0;
    55 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    jQuery之五:CheckBox控制
    WinServer2003 设置之:xp风格
    ASP.net: cookie
    ASP.NET之:URL重写(转载)
    jQuery 之二:Ajax加载Json数据
    jQuery 之一:对象插件
    Asp.net:Form
    jQuery之四:Table过滤
    jQuery之三:Tab控制
    Opera 9.01 Build 8543
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7747679.html
Copyright © 2011-2022 走看看