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. 1023 C. Bracket Subsequence

    传送门 [http://codeforces.com/contest/1023/problem/C] 题意 n字符串长度,k要求的字符串的长度,字符串只包含'('和')',而且这两种的数量相等,要求的 ...

  2. Microsoft Visual Studio 2013安装及试用

    我是在网上下载的vs2013版的安装包,下载的是压缩文件,解压后是2.86GB.安装包下载完成后我们就可以进入安装了. 同时建议最好在互联网连接的情况下安装. 打开下载好的文件,我们要选择.exe可执 ...

  3. 《Linux内核设计与实现》读书笔记 5

    第五章系统调用 5.1与内核通信 系统调用在用户空间进程和硬件设备间添加了一个中间层, 作用:为用户空间提供了一种硬件的抽象接口:保证了系统的稳定和安全,避免应用程序不正确使用硬件,窃取其他进程的资源 ...

  4. 读<架构漫谈>系列有感

    读了这一系列博文,我对架构也有了大致的了解.在简单的阅读之后,我解决了几个问题. 第一个问题,什么是架构? 要学习架构,首先要知道架构.那么,什么是架构呢?引用<架构漫谈(一)>里的话就是 ...

  5. zookeeper安装和使用 windows环境(转)

    原文地址: http://blog.csdn.net/tlk20071/article/details/52028945 简介 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是G ...

  6. 8-Python3从入门到实战—基础之数据类型(集合-Sets)

    Python从入门到实战系列--目录 集合的定义 集合(set)和字典类似,也是一组key的集合,但不存储value:由于key不能重复,所以,在set中,没有重复的key. 创建一个set,需要提供 ...

  7. slf4j的java包冲突问题

  8. Smarty 变量修饰器

    为了更方便的实现 功能与显示分离,通常会用 smarty 将功能代码中的数据 assign 到页面中,在页面中合理使用 smarty 的修饰方法,会使页面显示更美观! 一个数据可同时使用多个修饰函数, ...

  9. pandas shift

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/5/24 15:03 # @Author : zhang chao # @Fi ...

  10. 玩弄 python 正则表达式

    这里记录一个我常用的模型,每次久了不使用正则就会忘记. 记得最好玩的一句关于正则表达式的话就是 当你想到一件事情可以用正则表达式解决的时候 现在你就面临了两个问题了. python里面使用了re模块对 ...