HUSTOJ:Transit Tree Path
问题 D: Transit Tree Path
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)
输入
N
a1 b1 c1
:
aN−1 bN−1 cN−1
Q K
x1 y1
:
xQ yQ
输出
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的更多相关文章
- AtCoder ABC 070D - Transit Tree Path
传送门:http://abc070.contest.atcoder.jp/tasks/abc070_d 本题是一个图论问题——树(Tree). 有一棵结点数目为n的无向树.第i条边连接结点ai与bi, ...
- Atcoder Beginner Contest 070 D - Transit Tree Path
题意:n个点,n-1条边,组成一个无向的联通图,然后给出q和k,q次询问,每次给出两个点,问这两个点之间的最短距离但必须经过k点. 思路:我当时是用优化的Dijkstra写的(当天刚学的),求出k点到 ...
- 2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法
6690: Transit Tree Path 时间限制: 1 Sec 内存限制: 128 MB提交: 472 解决: 132[提交] [状态] [讨论版] [命题人:admin] 题目描述 Yo ...
- 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 ...
- maven项目启动报错:SLF4J: Class path contains multiple SLF4J bindings.
SringBoot的Application启动报错: SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding ...
- 【启发式搜索】Codechef March Cook-Off 2018. Maximum Tree Path
有点像计蒜之道里的 京东的物流路径 题目描述 给定一棵 N 个节点的树,每个节点有一个正整数权值.记节点 i 的权值为 Ai.考虑节点 u 和 v 之间的一条简单路径,记 dist(u, v) 为其长 ...
- Codechef March Cook-Off 2018. Maximum Tree Path
目录 题意 解析 AC_code @(Codechef March Cook-Off 2018. Maximum Tree Path) 题意 给你一颗\(n(1e5)\)个点有边权有点权的树,\(Mi ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
随机推荐
- django framawork
中文文档: https://q1mi.github.io/Django-REST-framework-documentation/ 神奇的generics from snippets.models i ...
- Tag Helpers 的使用介绍
什么是 Tag Helpers ? 在 Razor 文件中,Tag Helpers 能够让服务端代码参与创建和渲染 HTML 元素.例如,内置的ImageTagHelper能够在图像名称后面追加版本号 ...
- bs4库学习
# -*- coding:utf-8 -*- import bs4 import requests def tags_val(tag, key='', index=0): ''' tag指HTML元素 ...
- 数据分析 大数据之路 三 numpy
import numpy as np a = np.arange(9) b = a.reshape(3,3) print(b) print(b.max(axis=0)) # axis=0 示为 Y 轴 ...
- [BZOJ1047][HAOI2007]理想的正方形(RMQ+DP)
题意 有一个a*b的整数组成的矩阵,现请你从中找出一个n*n的正方形区域,使得该区域所有数中的最大值和最小值的差最小. 思路 RMQ求 再DP 代码 #include<cstdio> #i ...
- 解决Idea GitLab Clone failed: Authentication failed for的问题
刚使用GitLab做项目管理,在idea-check versionControl中使用git clone工程,一直报Clone failed: Authentication failed for ‘ ...
- ORACLE启动报错ORA-03113: end-of-file on communication channel
使用过程中发现oracle运行很慢(其实应该先关注空间问题),就准备关机重启一下,关不掉就强制关闭,然后启动就报错了. 1.SQL> startup ORACLE instance starte ...
- WPF实现只打开一个窗口,并且重复打开时已经打开的窗口置顶
内容来自:https://codereview.stackexchange.com/questions/20871/single-instance-wpf-application 第一步:添加Syst ...
- 使用getline输入一行字符串
给定10个国家名,按字母顺序输出,国家名中可以包含空格,国家名用换行隔开 #include<algorithm> #include<iostream> #include< ...
- 【react】---17新增的生命周期
一.废除的生命周期 官网文档指出使用这些生命周期的代码会在未来版本的react中更容易产生bug,尤其是对于异步渲染的版本 由于未来采用异步渲染机制,所以即将在17版本中去掉的生命周期钩子函数 com ...