第一开始想成了DP。尼玛后来才发现只有N条边,那就简单了。。

从起点S遍历整棵树,从某点跳出来回到终点T,问最短路长度。然而从某点跳出时走过的路径是一个定值。。。。

长度为整棵树的边长和sum*2-d1[i]。。然后求这个值加上回学校的路长的最小值就好了

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#define INF 2000100000
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ; ll d2[maxn];
ll d1[maxn];
int vis[maxn]; struct Edge
{
int to, len;
} edge[maxn]; struct node
{
int id, d;
};
vector<Edge> g[maxn]; void bfs(int s)
{
memset(vis, , sizeof(vis));
memset(d1, , sizeof(d1));
d1[s] = ;
vis[s] = ;
queue<node> que;
que.push((node) {
,
});
while(!que.empty())
{
node x = que.front();
que.pop();
int p = x.id;
for(int i = ; i < g[p].size(); ++i)
{
Edge &e = g[p][i];
if(!vis[e.to]) {
d1[e.to] = d1[p] + e.len;
vis[e.to] = ;
que.push((node) {
e.to, d1[e.to]
});
}
}
}
}
int main()
{
//freopen("in", "r", stdin);
int n;
while(~scanf("%d", &n)) {
for(int i = ; i <= n; ++i) scanf("%I64d", d2 + i);
int from, to, len;
ll sum = ;
for(int i = ; i < n; ++i)
{
scanf("%d%d%d", &from, &to, &len);
g[from].push_back((Edge) {
to, len
});
g[to].push_back((Edge) {
from, len
});
sum += len;
}
sum *= ;
bfs();
//for(int i = 0; i <= n; ++i) printf("%d ",d1[i]);
//printf("\n");
ll mx = INF;
for(int i = ; i <= n; ++i)
{
mx = min(mx, sum - d1[i] + d2[i]);
}
cout << mx << endl;
for(int i = ; i <= n; ++i) g[i].clear();
}
}

HDU4171--bfs+树的更多相关文章

  1. 【Tsinsen A1039】【bzoj2638】黑白染色 (BFS树)

    Descroption 原题链接 你有一个\(n*m\)的矩形,一开始所有格子都是白色,然后给出一个目标状态的矩形,有的地方是白色,有的地方是黑色,你每次可以选择一个连通块(四连通块,且不要求颜色一样 ...

  2. poj 1383 Labyrinth【迷宫bfs+树的直径】

    Labyrinth Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 4004   Accepted: 1504 Descrip ...

  3. 1501 二叉树最大宽度和高度 (BFS+树的遍历)

    题目:http://www.wikioi.com/problem/1501/ 给你一颗二叉树,求该数的宽和高, 首先求出树的高,直接进行二叉树遍历,能够得到二叉树的高 然后是得到宽,本人采用的是一层一 ...

  4. codeforce 337D Book of Evil ----树形DP&bfs&树的直径

    比较经典的老题 题目意思:给你一颗节点数为n的树,然后其中m个特殊点,再给你一个值d,问你在树中有多少个点到这m个点的距离都不大于d. 这题的写法有点像树的直径求法,先随便选择一个点(姑且设为点1)来 ...

  5. cf605D. Board Game(BFS 树状数组 set)

    题意 题目链接 有\(n\)张牌,每张牌有四个属性\((a, b, c, d)\),主人公有两个属性\((x, y)\)(初始时为(0, 0)) 一张牌能够被使用当且仅当\(a < x, b & ...

  6. codeforces 690C2 C2. Brain Network (medium)(bfs+树的直径)

    题目链接: C2. Brain Network (medium) time limit per test 2 seconds memory limit per test 256 megabytes i ...

  7. POJ 1383 Labyrinth (bfs 树的直径)

    Labyrinth 题目链接: http://acm.hust.edu.cn/vjudge/contest/130510#problem/E Description The northern part ...

  8. 快速切题 sgu116. Index of super-prime bfs+树思想

    116. Index of super-prime time limit per test: 0.25 sec. memory limit per test: 4096 KB Let P1, P2, ...

  9. HDU 2653 (记忆化BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2653 题目大意:迷宫中有普通点和陷阱.其中普通点可以走可以飞,但是陷阱只能飞.走耗时1,飞耗时2.但 ...

  10. HDU 5025 (BFS+记忆化状压搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5025 题目大意: 迷宫中孙悟空救唐僧,可以走回头路.必须收集完钥匙,且必须按顺序收集.迷宫中还有蛇, ...

随机推荐

  1. 算法导论(第三版)Exercises4.2(第四章二节)

    4.2-1(计算结果) 18  14 62  66 4.2-2(Strassen算法计算矩阵乘法) void multiplyMatrix(int a[], int b[], int n, int r ...

  2. window.open和window.close的使用详解

    有时候,我们想通过JS实现一个<a>的新开标签的效果,此时我们想到了window.open方法实现.那么window.open到底应该怎么使用呢?   我们知道window.open可以新 ...

  3. C# 图结构操作

    仿造<<Java常用算法手册>>里面对的算法,使用C#实现了一遍. 理论知识我就不讲解了,在这本书里面已经写的非常完美! 代码如何下: using System; using ...

  4. 在ubuntu14.04上安装maven

    1.首先到Maven官网下载安装文件,目前最新版本为3.3.1,下载文件为apache-maven-3.0.3-bin.tar.gz,下载可以使用wget命令: 2.进入下载文件夹,找到下载的文件,运 ...

  5. NetAnalyzer笔记 之 一. 开篇语

    [创建时间:2015-08-26 22:00:12] NetAnalyzer下载地址 第一次写技术相关的博客,不足之处还请担待并告知. 在开始之前,先简单介绍一下NetAnalyzer, NetAna ...

  6. Leetcode:linked_list_cycle

    一.     题目 给定一个链表.确定它是否有一个环.不使用额外的空间? 二.     分析 1. 空链表不成环 2. 一个节点自环 3. 一条链表完整成环 思路:使用两个指针,一个每次往前走2步,一 ...

  7. CocoaPods的一些理解

    在这片博客中,我将分享我从cocopods中学到的东西. 如果你使用Cocoapods,你的.gitignore文件中会有什么. 这个问题在debate on SO中被提及,但是我建议只追踪Podfi ...

  8. ASP.net获取当前页面的文件名,参数,域名等方法

    ASP.net后台获取当前页面的文件名 System.IO.Path.GetFileName(Request.Path).ToString(); 获取当前页面文件名,参数,域名等方法 假设当前页完整地 ...

  9. MySQL的一些语法总结

    初学MySQL,今天遇到了一个问题,然后汇总了一下MySQL的一些语法 1. date和datetime类型是不同的 date只记录日期(包括年月日),datetime记录日期和时间(包括年月日时分秒 ...

  10. T4文本模板

    <#...#> 可以包含语句 <#=...#>  用于表达式,提供“输出”操作 <#+ ...> 使用类功能控制块向文本模板添加方法.属性.字段,必须作为文件中最后 ...