问题 D: Transit Tree Path

 
You are given a tree with N vertices.
Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N−1 edges, where N is the number of its vertices.
The i-th edge (1≤i≤N−1) connects Vertices ai and bi, and has a length of ci.

You are also given Q queries and an integer K. In the j-th query (1≤j≤Q):

find the length of the shortest path from Vertex xj and Vertex yj via Vertex K.
Constraints
3≤N≤105
1≤ai,bi≤N(1≤i≤N−1)
1≤ci≤109(1≤i≤N−1)
The given graph is a tree.
1≤Q≤105
1≤K≤N
1≤xj,yj≤N(1≤j≤Q)
xj≠yj(1≤j≤Q)
xj≠K,yj≠K(1≤j≤Q)

输入

Input is given from Standard Input in the following format:
N  
a1 b1 c1  
:  
aN−1 bN−1 cN−1
Q K
x1 y1
:  
xQ yQ

 

输出

Print the responses to the queries in Q lines.
In the j-th line j(1≤j≤Q), print the response to the j-th query.

样例输入

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

样例输出

3
2
4

提示

The shortest paths for the three queries are as follows:
Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4


  题意:求两个点经过k点之后的最短距离

  HUST得训练赛上遇到得题目,直接dfs求出k点到各个点的最短距离就可以,输出的时候输出到两点距离的和就ok

  附上代码(已AC):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define MAX 100005
#define ll long long struct Edge{
ll to;
ll next;
ll cost;
}; Edge edge[MAX*];
ll head[MAX];
ll dis[MAX];
void DFS(ll u,ll v,ll w)
{
dis[u]=w;
for(ll i=head[u];i!=-;i=edge[i].next)
{
ll to=edge[i].to;
if(to==v)
continue;
DFS(to,u,w+edge[i].cost);
}
return ;
}
int main()
{
ll n,u,v,w,q,k;
scanf("%lld",&n);
ll cnt=;
memset(head,-,sizeof(head));
for(ll i=;i<=n-;i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
edge[cnt].to=v;
edge[cnt].cost=w;
edge[cnt].next=head[u];
head[u]=cnt++;
edge[cnt].to=u;
edge[cnt].cost=w;
edge[cnt].next=head[v];
head[v]=cnt++;
}
scanf("%lld%lld",&q,&k);
DFS(k,-,);
while(q--)
{
scanf("%lld%lld",&u,&v);
printf("%lld\n",dis[u]+dis[v]);
}
return ;
}

HUSTOJ:Transit Tree Path的更多相关文章

  1. AtCoder ABC 070D - Transit Tree Path

    传送门:http://abc070.contest.atcoder.jp/tasks/abc070_d 本题是一个图论问题——树(Tree). 有一棵结点数目为n的无向树.第i条边连接结点ai与bi, ...

  2. Atcoder Beginner Contest 070 D - Transit Tree Path

    题意:n个点,n-1条边,组成一个无向的联通图,然后给出q和k,q次询问,每次给出两个点,问这两个点之间的最短距离但必须经过k点. 思路:我当时是用优化的Dijkstra写的(当天刚学的),求出k点到 ...

  3. 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法

    6690: Transit Tree Path 时间限制: 1 Sec  内存限制: 128 MB提交: 472  解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...

  4. Lintcode376-Binary Tree Path Sum-Easy

    376. Binary Tree Path Sum Given a binary tree, find all paths that sum of the nodes in the path equa ...

  5. maven项目启动报错:SLF4J: Class path contains multiple SLF4J bindings.

    SringBoot的Application启动报错: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding ...

  6. 【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path

    有点像计蒜之道里的 京东的物流路径 题目描述 给定一棵 N 个节点的树,每个节点有一个正整数权值.记节点 i 的权值为 Ai.考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长 ...

  7. Codechef March Cook-Off 2018. Maximum Tree Path

    目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...

  8. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

  9. LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium

    题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...

随机推荐

  1. Git 通过ssh 配置基于Host的差异配置

    Host gitlab.xxx.com HostName gitlab.xxx.com User user IdentityFile xxx\.ssh\id_rsa Host github.com H ...

  2. NOIP2013 D1T3 货车运输 zz耻辱记

    目录 先来证明下lemma: 图上2点间最小边权最大的路径一定在MST上 感性理解下: 每次kruskal algo都连接最大的不成环边 此时有2个未联通的联通块被连起来. 那么考虑u, v两点的联通 ...

  3. pycharm下虚拟环境建立,django项目建立等情况说明

  4. spring创建bean的三种方式

    spring创建bean的三种方式: 1通过构造方法创建bean(最常用) 1.1 spring默认会通过无参构造方法来创建bean,如果xml文件是这样配置,则实体类中必须要有无参构造方法,无参构造 ...

  5. jquery获取一组文本框的值

    $("#btn5").click(function()  {    var str="";$("[name='checkbox'][checked]& ...

  6. 按模板批量修改Excel文件内容

    Sub 按模板修改Excel文件() Dim MoBanWorkBook As Workbook Set MoBanWorkBook = Application.ActiveWorkbook Dim ...

  7. Android 应用内悬浮控件实践总结

    在工作中遇到一个需求,需要在整个应用的上层悬浮显示控件,目标效果如下图: 首先想到的是申请悬浮窗权限,OK~ 打开搜索引擎,映入眼帘的并不是如何申请,而是“Android 悬浮窗权限各机型各系统适配大 ...

  8. 用Java写hello world

    public class HelloWorld{ public static void main(String[] args){ System.out.println("hello worl ...

  9. koa 写简单服务

    这两天用koa写了点服务,这里面和express还是有部分区别的 1.静态服务:  koa 中,是有中间件, koa-static, const static_f = require('koa-sta ...

  10. 《JavaScript DOM编程艺术》学习笔记(三)

    终于要完成这最后一部分了,距离第二部分已经过去五天了,一直想早点写的,但还是拖到今天了………… 34.position属性的和法制:static是position属性的默认值,意思是有关元素将按照它们 ...