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. Python-dict-12

    字典 Why:咱们目前已经学习到的容器型数据类型只有list,那么list够用?他有什么缺点呢? 1. 列表可以存储大量的数据类型,但是如果数据量大的话,他的查询速度比较慢. 2. 列表只能按照顺序存 ...

  2. C. Vasya and Multisets

    传送门 [http://codeforces.com/contest/1051/problem/C] 题意 给你一堆数,问是否可以分为两堆使得两堆里只出现一下的数字的种类相等,可以输出任意一种分的方式 ...

  3. ELF分析 实践

    分析ELF文件: 首先编写一个.c文件,并使用gcc -c xx.c -o xx.o 对其进行编译. 我在这个实践中是ELF_1.o文件. 接下来输入ls -l ELF_1.o 查看重定位文件的信息. ...

  4. SVN解决冲突

    SVN冲突出现场景 如今是一个团结协作的时代,开发一个系统,往往会多人协作共同完成.版本管理是必不可少的,常用的软件有Git,SVN等.今天说一下,SVN管理版本时,如果出现冲突后,如何快速解决冲突. ...

  5. GIthub地址

    https://github.com/cuibaoxue/Text1

  6. h5定位geolaction无法调试解决方法

    昨天接到一个在h5获取经纬度的需求,看了文档后,代码其实很简单,但在浏览器上调试就比较蛋疼了... 代码: function successfulCallback(position) { consol ...

  7. eclipse集成tomcat日志文件输出配置

    eclipse集成tomcat日志文件输入配置 2015-07-21 00:13 1072人阅读 评论(0) 收藏 举报  分类: tomcat(1)  eclipse Where can I vie ...

  8. activiti 选人的实现

    activiti默认是不带有选人的功能的,它默认的是在调用complete 方法的时候自动根据下一个节点的 assignee属性或者candidate属性 设置下一节点的候选人或者 assginee. ...

  9. [转帖] cnblog新闻区 “40岁以上的员工,请自觉离开”

    “40岁以上的员工,请自觉离开” 投递人 itwriter 发布于 2018-04-29 22:36 评论(9) 有2733人阅读 原文链接 [收藏] « » “准确地说,华为目前要裁掉的.清退的,是 ...

  10. linux 终端快捷操作

    终端一些常用快捷键 按键 作用 Ctrl+d 键盘输入结束或退出终端 Ctrl+s 暂停当前程序,暂停后按下任意键恢复运行 Ctrl+z 将当前程序放到后台运行,jobs命令查看后台工作,命令&quo ...