Gym - 101808K Another Shortest Path Problem (LCA+思维)

K. Another Shortest Path Problem

time limit per test

3.0 s

memory limit per test

256 MB

input

standard input

output

standard output

Shortest path problems have always been a part of competitive programming, appearing in many contests all around the world. In order to keep that tradition, we created another one:

You are given an undirected connected weighted graph with N nodes and N edges, and you have to answer Q queries. Each query consists of two nodes X and Y, and you have to find the length of the shortest path between nodes X and Y.

Input

The first line contains a single integer T, the number of test cases.

Each test case starts with a line containing two integers N and Q, the number of nodes (and edges) and the number of queries. (3 ≤ N ≤ 105) (1 ≤ Q ≤ 105)

Each of the following N lines contain the description of the edges. The ith line contains 3 space-separated integers uivi, and wi. This means that there is an undirected edge between nodes ui and vi, with a weight of wi. (1 ≤ ui, vi ≤ N) (1 ≤ wi ≤ 105)

Then Q lines follow, the ith line contains two integers X and Y. This means that you need to find the shortest path between nodes X and Y. (1 ≤ X, Y ≤ N)

It is guaranteed that the graph contains no self loops or multiple edges.

Output

For each test case, and for each query, print one line containing one integer, the shortest path between X and Y.

Example

input

Copy

1
6 3
1 2 2
1 3 4
2 6 3
3 4 1
3 5 10
3 6 6
1 4
2 5
3 2

output

Copy

5
16
6

题意:给你一个含n条边的带权无向连通图,q次查询,每次查询两点间的最短距离。

思路:LCA+思维。

设a,b两点间的距离为f(a,b) 则f(a,b)=dis[a]+dis[b]-2*dis[lca(a,b)];

由于n条边,因此我们先任取一条边,设这条边为X,Y,权值为Z,设查询的点为x,y,则答案为

min(f(a,b),f(a,X)+f(b,X),f(a,Y)+f(b,Y),f(a,X)+f(b,Y)+Z,f(a,Y)+f(b,X)+Z);

注意long long。这道题是在做完hdu5296后突然想起来的,其实这道题就是简单的lca应用,之前居然没做出来。。。

代码:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=400010;
int n,m,q,tot,tot2,cnt,tmp,ans;
int f[maxn][20];
int head[maxn];
ll dis[maxn];
int dfn[maxn],d[maxn];
int dep[maxn],e[maxn],pos[maxn];
bool vis[maxn];
struct node
{
    int to,nex;
    ll w;
}a[maxn];
void add(int u,int v,ll w)
{
    a[cnt].to=v;
    a[cnt].w=w;
    a[cnt].nex=head[u];
    head[u]=cnt++;
}
void init()
{
    cnt=tot=tot2=0;
    memset(head,-1,sizeof(head));
    memset(pos,-1,sizeof(pos));
    memset(vis,0,sizeof(vis));
    dis[1]=0;
}
void dfs(int u,int deep)//1
{
    if(pos[u]!=-1)return;
    dfn[tot2]=u;d[u]=tot2++;
    pos[u]=tot;e[tot]=u;dep[tot++]=deep;
    for(int i=head[u];i!=-1;i=a[i].nex)
    {
        int v=a[i].to;
        if(pos[v]==-1)
        {
            dis[v]=dis[u]+a[i].w;
            dfs(v,deep+1);
            e[tot]=u;dep[tot++]=deep;
        }
    }
}
void rmq(int n)//2
{
    for(int i=1;i<=n;i++)f[i][0]=i;
    for(int j=1;(1<<j)<=n;j++)
    for(int i=1;i+(1<<j)-1<=n;i++)
    {
        if(dep[f[i][j-1]]<dep[f[i+(1<<(j-1))][j-1]]) f[i][j]=f[i][j-1];
        else f[i][j]=f[i+(1<<(j-1))][j-1];
    }
}
int RMQ(int l,int r)
{
    int k=(int)(log((double)(r-l+1))/log(2.0));
    if(dep[f[l][k]]<dep[f[r-(1<<k)+1][k]]) return f[l][k];
    else return f[r-(1<<k)+1][k];
}
int lca(int x,int y)//3
{
    if(pos[x]<pos[y]) return e[RMQ(pos[x],pos[y])];
    else return e[RMQ(pos[y],pos[x])];
}
ll cal(int x,int y)
{
    return dis[x]+dis[y]-2*dis[lca(x,y)];
}
int main()
{
    int T,cas=1;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n,&q);
        for(int i=0;i<n-1;i++)
        {
            int x,y;ll z;
            scanf("%d%d%lld",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        int X,Y;ll Z;
        scanf("%d%d%lld",&X,&Y,&Z);
        //printf("Case #%d:\n",cas++);
        dfs(1,0);
        rmq(2*n-1);//4
        ll ans=0;
        while(q--)
        {
            int u,v;
            scanf("%d %d",&u,&v);
            ans=cal(u,v);
            ans=min(ans,cal(u,X)+cal(v,X));
            ans=min(ans,cal(u,Y)+cal(v,Y));
            ans=min(ans,cal(u,X)+cal(v,Y)+Z);
            ans=min(ans,cal(u,Y)+cal(v,X)+Z);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

 

 

编程竞赛和算法训练平台如 Codeforces Gym 提供了大量高质量的竞赛题目,供选手进行训练和提升。关于 Gym 103409K 问题的解决方案或讨论,通常可以通过以下几种方式获取相关信息: 1. **Codeforces Gym 页面**:Gym 103409K 问题属于某个特定的比赛或训练集,可以在 Codeforces 的 Gym 页面中查找该问题编号的对应比赛,并查看题目描述、提交记录以及可能存在的公开讨论[^1]。 2. **提交记录与代码查看**:在 Codeforces 平台上,用户可以提交代码后查看其他人的提交记录,包括通过的代码。通过阅读其他选手的代码,可以学习不同的解题思路和优化技巧。通常在问题页面中点击“Standings”可以查看排名,并通过“Hacks”或“Submissions”查看具体代码[^1]。 3. **官方题解与讨论区**:某些比赛会提供官方题解(Editorial),通常在比赛结束后发布。如果该比赛有官方题解,可以在 Codeforces 的博客(Blog)部分查找相关文章。此外,问题页面下方的评论区也可能包含选手之间的讨论,有时会涉及解题思路或技巧[^1]。 4. **社区资源**:一些编程竞赛社区如 Codeforces、AtCoder、Topcoder 的论坛,以及 Stack Overflow、Reddit 的 r/learnprogramming 或 r/programming 等板块,也可能存在对该问题的讨论。 5. **GitHub 仓库**:许多参赛者会将自己的解题代码整理到 GitHub 上,搜索关键词 "Gym 103409K solution" 或类似内容,可能会找到相关代码或题解。 ### 示例代码结构 以下是一个用于处理典型算法竞赛问题的 Python 模板示例,适用于读取输入并输出结果: ```python def main(): import sys input = sys.stdin.read data = input().split() # 示例处理逻辑 n = int(data[0]) a = list(map(int, data[1:n+1])) # 示例输出 print(sum(a)) if __name__ == "__main__": main() ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值