POJ __1986 Distance Queries (lca+求距离的骚操作)

Distance Queries
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 15728   Accepted: 5536
Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare" 

* Line 2+M: A single integer, K. 1 <= K <= 10,000 

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6
1 4
2 6

Sample Output

13
3
36

Hint

Farms 2 and 6 are 20+3+13=36 apart. 

Source

USACO 2004 February

               有n个点m个边~a距离b为c(距离root为线性)~后面那个字符没用~询问x个~求dis[i]+dis[j]-2*dis[lca(i,j)];

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=230100;
const int DEG=20;
struct ***
{
    int to,ne,len;
}ed[maxn*2];
int head[maxn],cnt;
int dis[maxn];//计算距离;
void add(int u,int v,int w)
{
    ed[cnt].to=v;
    ed[cnt].len=w;
    ed[cnt].ne=head[u];
    head[u]=cnt++;
}
void init()
{
    cnt=1;
    memset(head,-1,sizeof(head));
    memset(dis,0,sizeof(dis));
}
int fa[maxn][DEG];
int deg[maxn];//深度数组
void BFS(int root)
{
    queue<int>q;
    deg[root]=0;
    fa[root][0]=root;
    q.push(root);
    dis[root]=0;//
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        for(int s=1;s<DEG;s++)
        {
            fa[t][s]=fa[fa[t][s-1]][s-1];
        }
        for(int s=head[t];s!=-1;s=ed[s].ne)
        {
            int v=ed[s].to;
            if(v==fa[t][0])
            {
                continue;
            }
            deg[v]=deg[t]+1;
            dis[v]=dis[t]+ed[s].len;//
            fa[v][0]=t;
            q.push(v);
        }
    }
}
int LCA(int u,int v)
{
    if(deg[u]>deg[v])
    {
        swap(u,v);
    }
    int hu=deg[u],hv=deg[v];
    int tu=u,tv=v;
    for(int det=hv-hu,s=0;det;det>>=1,s++)
    {
        if(det&1)
        {
            tv=fa[tv][s];
        }
    }
    if(tu==tv)
    {
        return tu;
    }
    for(int s=DEG-1;s>=0;s--)
    {
        if(fa[tu][s]==fa[tv][s])
        {
            continue;
        }
        tu=fa[tu][s];
        tv=fa[tv][s];
    }
    return fa[tu][0];
}
bool in[maxn];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    init();
    memset(in,0,sizeof(in));
    for(int s=1;s<=m;s++)
    {
        int a,b,c;
        char w;
        scanf("%d%d%d %c",&a,&b,&c,&w);
        add(a,b,c);
        add(b,a,c);
        in[b]=1;
    }
    int root;
    for(int s=1;s<=n;s++)
    {
        if(!in[s])
        {
            root=s;
            break;
        }
    }
    BFS(root);
    int te;
    cin>>te;
    while(te--)
    {
        int u,v;
        scanf("%d%d",&u,&v);
      //  cout<<LCA(u,v)<<endl;
        cout<<dis[u]+dis[v]-2*dis[LCA(u,v)]<<endl;
    }
    return 0;
}





















全部评论

相关推荐

01-28 16:12
中南大学 Java
几年前还没有chatgpt的时候,刷题真的是很痛苦。刷不出来只能看题解,题解有几个问题:第一个是每次看的写题解的人都不一样,很难有一个统一的思路;第二个也是最重要的是,题解只提供了作者自己的思路,但是没有办法告诉你你的思路哪里错了。其实很少有错误的思路,我只是需要被引导到正确的思路上面去。所以传统题解学习起来非常困难,每次做不出来难受,找题解更难受。但是现在chatgpt能做很多!它可以这样帮助你&nbsp;-1.&nbsp;可以直接按照你喜欢的语言生成各种解法的题解和分析复杂度。2.&nbsp;把题和你写的代码都发给它,它可以告诉你&nbsp;你的思路到底哪里有问题。有时候我发现我和题解非常接近,只是有一点点🤏想错了。只要改这一点点就是最优解。信心倍增。3.&nbsp;如果遇到不懂的题解可以一行一行询问为什么要这样写,chatgpt不会嫌你烦。有时候我觉得自己的range写错了,其实那样写也没错,只是chat老师的题解有一点优化,这个它都会讲清楚。4.&nbsp;它可以帮你找可以用同类型解法来做的题。然后它可以保持解法思路不变,用一个思路爽刷一个类型的题。如果题目之间思路又有变化,它会告诉你只有哪里变了,其他的地方还是老思路。5.&nbsp;它也可以直接帮你总结模板,易错点。经过chat老师的指导,我最大的改变是敢刷题了。之前刷题需要先找某一个人写的算法题repo,然后跟着某一个人他的思路刷他给的几个题。如果想写别的题,套用思路失败了,没有他的题解,也不知道到底哪里错了;看别人的题解,思路又乱了。这个问题在二分查找和dp类型的题里面特别常见。但是现在有chat老师,他会针对我的代码告诉我我哪里想错了,应该怎么做;还按照我写代码的习惯帮我总结了一套属于我的刷题模板。每天写题全是正反馈!
明天不下雨了:那我建议可以用 chatgpt atlas 或者 dia 去刷,也可以用 chrome 加个 ai 插件去刷 左边刷题右边 chat 效果很好
AI时代的工作 VS 传...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务