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. main函数是必须的吗

    研究实验4 研究过程: 问题引出:C语言编程非得用主函数main吗,不用是否可以? 对此问题进行研究,用tc.exe书写代码如下: 图1  没有main函数的c程序 对其进行编译,链接发现,编译阶段可 ...

  2. Stanford Word Segmenter的特定领域训练

    有没有人自己训练过Stanford Word Segmenter分词器,因为我想做特定领域的分词,但在使用Stanford Word Segmenter分词的时候发现对于我想做的领域的一些词分词效果并 ...

  3. Linux内核设计与实现 第四章

    1. 什么是调度 现在的操作系统都是多任务的,为了能让更多的任务能同时在系统上更好的运行,需要一个管理程序来管理计算机上同时运行的各个任务(也就是进程). 这个管理程序就是调度程序,功能: 决定哪些进 ...

  4. a simple example for spring AOP

    /** * Created by Administrator on 2015/11/25. * a interface */public interface ArithmeticCalculator{ ...

  5. 【软件工程Ⅱ】作业二 |分布式版本控制系统Git的安装与使用

    本次作业的要去来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2097 远程库地址:https://github.com/Mol ...

  6. boost::asio之(一)简单客户端服务器回显功能

    客户端: // BoostDev.cpp: 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #inc ...

  7. Java设计模式之工厂模式(Factory模式)介绍(转载)

    原文见:http://www.jb51.net/article/62068.htm 这篇文章主要介绍了Java设计模式之工厂模式(Factory模式)介绍,本文讲解了为何使用工厂模式.工厂方法.抽象工 ...

  8. Oracle ORDS的简单SQL配置模板

    1. 先加上简单的SQL配置模板. DECLARE PRAGMA AUTONOMOUS_TRANSACTION; BEGIN ORDS.ENABLE_SCHEMA(p_enabled => TR ...

  9. Node http请求信息

    //1:加载http模块 httpconst http = require("http");//2:创建http 服务器var server = http.createServer ...

  10. vue-cli webpack项目npm run dev启动过程

    前言 通过vue init webpack和npm install命令初始化项目后,执行npm run dev就打开了网站http://localhost:8080.初学者不知道index.html. ...