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. BZOJ3782 上学路线

    设障碍个数为,\(obs\)则一般的容斥复杂度为\(O(2^{obs})\).但因为这个题是网格图,我们可以用DP解.设\(f[i]\)表示不经过任何障碍到达第\(i\)个障碍的方案数,转移时枚举可以 ...

  2. 过滤器Filter的使用(以登录为例子)

    使用过滤器步骤: (1)在web.xml文件中添加过滤器(以下例子是过滤多个请求) <!-- 用户登录过滤 --> <filter> <filter-name>lo ...

  3. json.dumps()和json.loads()

    json.dumps()用于将字典形式的数据转化为字符串,json.loads()用于将字符串形式的数据转化为字典,代码: import json data = { 'name' : 'Connor' ...

  4. Quartz应用与集群原理分析

    Quartz在CRM中的应用场景: https://tech.meituan.com/mt-crm-quartz.html https://www.mtyun.com/library/mt-crm-q ...

  5. FTP Download File By Some Order List

    @Echo Off REM -- Define File Filter, i.e. files with extension .RBSet FindStrArgs=/E /C:".asp&q ...

  6. ntpdate[31915]: the NTP socket is in use, exiting

    [root@master local]# ntpdate cn.pool.ntp.org 10 Oct 13:24:36 ntpdate[31915]: the NTP socket is in us ...

  7. doc.update

    db.collection('todos').doc('todo-identifiant-aleatoire').update({ // data 传入需要局部更新的数据 data: { // 表示将 ...

  8. tenda u1 usb wireless device install driver for kali linux kernal Debian 4.3.3-7kali2 (2016-01-27) x86_64 GNU/Linux

    因为内核比较新的关系,tenda官方网站上面提供给u1无线网卡的驱动并不能正常编译使用,编译的时候报一个类似错误 /usr/src/linux-headers--kali1-common/Makefi ...

  9. sort和uniq的应用实例

    sort 排序 uniq 1.语法:sort [option]... [file]... 2.选项:-k key,关键子,指定以那个列来排序.如果不指定,默认将正行作为关键字排序-n 对数值排序.默认 ...

  10. HDU4296-ChengduOnling-贪心

    某人要搬砖盖一栋楼,有m块砖,每块有两个值w和s,表示重量和强度.楼盖好后每块砖有一个危险值,表示为此砖之上的砖的重量和减此砖的强度,即Σ0(j-1)w - sj. 整体危险值是每块砖危险值的最大值. ...