6690: Transit Tree Path

时间限制: 1 Sec  内存限制: 128 MB
提交: 472  解决: 132
[提交] [状态] [讨论版] [命题人:admin]

题目描述

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

 /*
题意 :
求结点x经过结点k到达结点y的最短距离
转化为以k结点为起始点,求k分别到结点x和结点y的最短距离
用到SPFA算法
*/
//赛后补题,一开始将dist[]的值赋值为INF,wa了
//后来想了一下,INF值为1061109567
//虽然大于1e9,但时如果两条边都是1e9,则加和就>INF,此时就会出错
//所以需要用LINF来个dist[]中的数组初始化为无穷大
//一定要判断清除用哪个
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
const int maxn=1e5+;
typedef long long type_weight;
struct Edge
{
//根据题意定义weight的类型
//可能为long long 型,也可能为int型
//用typedef定义type_weight,方便修改类型
int vex;
type_weight weight;
Edge(int v=,type_weight w=):vex(v),weight(w){}
};
vector<Edge>E[maxn]; //向E[u]中加入(v,weight) : 边u与边v相连,其权值为weight
void addedge(int u,int v,type_weight weight)
{
E[u].push_back(Edge(v,weight));
}
//visited[i] : 判断结点i是否在队列中
//dist[] : 存储最短距离,最好定义成long long 型
//cnt[] : 判断是否存在负环
bool visited[maxn];
long long dist[maxn];
int cnt[maxn];
//求从start结点到其他结点的最短距离
//共n个结点
bool SPFA(int start,int n)
{
memset(visited,,sizeof(visited));
//初始化dist[]为LINF,即长整型的最大值(无穷大)
for(int i=;i<=n;i++)
dist[i]=LINF;
dist[start]=;
visited[start]=true; queue<int >que;
while(!que.empty())
que.pop();
que.push(start); while(!que.empty())
{
int u=que.front();
que.pop(); //遍历以u为弧尾的所有结点
//E[u][i].v : 以u为弧尾的结点
//E[u][i].weight : 结点u与结点E[u][i].v之间的权重
for(int i=;i<E[u].size();i++)
{
int v=E[u][i].vex; //判断边u是否能松弛边v
if(dist[v] > dist[u]+E[u][i].weight)
{
dist[v]=dist[u]+E[u][i].weight;
if(!visited[v])
{
que.push(v);
visited[v]=true;
//如果某个结点进入队列的此数超过n-1次,则此图存在负环
if(++cnt[v] > n)
return false;
}
}
}
}
return true;
} int main()
{
int N;
scanf("%d",&N);
for(int i=;i<N;i++)
{
int a,b;
type_weight weight;
scanf("%d%d%lld",&a,&b,&weight);
addedge(a,b,weight);
addedge(b,a,weight);
}
int Q,K;
scanf("%d%d",&Q,&K);
SPFA(K,N);
for(int i=;i<=Q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
printf("%lld\n",dist[x]+dist[y]);
}
return ;
}

2018.7中石油个人赛第4场(D-Transit Tree Path)-最短路算法的更多相关文章

  1. Stones【中石油个人赛第十七场I】

    Stones 题目链接(传送门) 来源:upc12899 题目描述 There are N stones arranged in a row. Every stone is painted white ...

  2. 2018.9.9 nowcoder 普及组第一场

    2018.9.9 nowcoder 普及组第一场 C-括号 题目大意:一个只包含左右括号的字符串\(S\),希望删掉S中若干个字符,使得剩下的字符串是一个合法的括号串,有多少不同的方案. Soluti ...

  3. Civil 3D 2017本地化中VBA程序移植到2018版中

    中国本地化包简直就是一块鸡肋, 但对于某些朋友来说还真离不了: 可惜中国本地化包的推出一直滞后, 在最新版软件出来后1年多, 本地化还不一定能够出来, 即使出来了, 也只能是购买了速博服务的用户才能得 ...

  4. 2018牛客网暑假ACM多校训练赛(第二场)E tree 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round2-E.html 题目传送门 - 2018牛客多校赛第二场 E ...

  5. 爬取CVPR 2018过程中遇到的坑

    爬取 CVPR 2018 过程中遇到的坑 使用语言及模块 语言: Python 3.6.6 模块: re requests lxml bs4 过程 一开始都挺顺利的,先获取到所有文章的链接再逐个爬取获 ...

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

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

  7. MVC4 EF linq从客户端中检测到有潜在的危险的Request.Path值

    今天做项目的时候遇到了这样的问题贴出来给大家分享下啦, 使用MVC4 EF linq跳转视图的时候出现,从客户端中检测到有潜在的危险的Request.Path值错误,如下图所示: 解决办法如下:  r ...

  8. Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix)

    Leetcode之深度优先搜索(DFS)专题-329. 矩阵中的最长递增路径(Longest Increasing Path in a Matrix) 深度优先搜索的解题详细介绍,点击 给定一个整数矩 ...

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

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

随机推荐

  1. .apply()用法和call()的区别

    Js apply方法详解我在一开始看到javascript的函数apply和call时,非常的模糊,看也看不懂,最近在网上看到一些文章对apply方法和call的一些示例,总算是看的有点眉目了,在这里 ...

  2. 个人作业-Week1(新增详细说明)

    快速看完整部教材,列出你仍然不懂的5到10个问题,发布在你的个人博客上. 如何提出有价值的问题? 请看这个文章:http://www.cnblogs.com/rocedu/p/5167941.html ...

  3. Daily Scrum- 12/23

    Meeting Minutes 与Travis和Zhongqiu讨论了Beta的Feature以及更长期的计划: 讨论了一些使用及设计上的Bug (Feature); 开始了新的开发周期: Burnd ...

  4. SQLserver 进程被死锁问题解决

    事务(进程ID xx)与另一个进程被死锁在 锁|通信缓冲区 资源上,并且已被选座死锁牺牲品.请重新运行该事务.Sqlserver 当出现这个错误时,如下图: 解决办法:更改数据库事务隔离级别 alte ...

  5. atcoder A - Frog 1(DP)

    A - Frog 1 Time Limit: 2 sec / Memory Limit: 1024 MB Score : 100100 points Problem Statement There a ...

  6. Java控制台常用命令

    http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html javaw是java的带console版本,其他一致. h ...

  7. FreeMarker has_content等价于StringUtils.isNotNullOrEmpty

    has_content It is true if the variable exists (and isn't Java null) and is not "empty", ot ...

  8. Java 线程内 递归 Bug 一例

    一个线程的run方法里使用递归方法,出了Bug. private boolean ispass(String creationId){ List<Map> maps =creationSe ...

  9. Jquery 组 tbale表格隔行变色

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...

  10. GIL全局解释器锁+GIL全局解释器锁vs互斥锁+定时器+线程queue+进程池与线程池(同步与异步)

    以多线程为例写个互斥锁 from threading import Thread ,Lockimport timemutex = Lock() n = 100 def task(): global n ...