第一开始想成了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. Git本地版本控制备忘

    首先git是一个版本控制工具,类似于SVN 笔记包括两部分,git本地版本控制和git远程协助 一.Git本地版本控制 以git windows版本msysgit为例,下载地址http://msysg ...

  2. win7系统如何恢复administrator用户

    默认情况下,administrator用户是禁用的. 要恢复的话,右键单击我的电脑 管理-->本地用户和组-->用户-->右键属性 把"账户已禁用"前的选择符号去 ...

  3. Maven--多模块依赖实例解析(五)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> <Maven—生命周期和插件(四)&g ...

  4. UBI(unsorted block image )块管理

    一.介绍 ubi是unsorted block images的缩写,是由IBM开发设计的,它与ubifs有不同的含义,ubifs是一种文件系统(nokia开发的):而ubi是一种块管理工具,工作在mt ...

  5. Bubble Sort 冒泡排序

    //Bubble Sort ( O(n²)) public class TestBubbleSort { public int[] bubbleSortArray(int[] arr){ ; i &l ...

  6. WHY IE AGAIN? - string.charAt(x) or string[x]?

    近期今天在写一个"删除字符串中反复字符串"的函数,代码例如以下: 开门见山,重点 string.charAt(index) 取代 string[index] function re ...

  7. HDU 4760 Good FireWall 完好Trie题解

    本题乍看像是线段树之类的区间操作,只是由于仅仅是须要查找ip的前缀,故此事实上是使用Trie来做. 挺高难度的Trie应用,做完这道题之后说明Trie功力有一定火候了. 这里的Trie使用到了Dele ...

  8. 如何解决DE0-Nano的EPCS16 无法下载的问题:NO EPCS LAYOUT DATA --- LOOKING FOR SECTION [EPCS-XXXXXX]

    在用NIOS的flashprogram对EPCSx进行烧录程序时,出现了No EPCS layout data --- looking for section [EPCS-XXXXXX]的错误,在网上 ...

  9. 让qq图标在自己的网站上显示方法

    代码如下: <div id="xixi" onmouseover="toBig()" style="top: 260px; left: 5px; ...

  10. [转]Sql Server 2005中的架构(Schema)、用户(User)、登录(Login)和角色(Role)

    每一个概念的产生必然是因为碰到了无法解决的问题.换句话说,如果没有它,必然会导致某些问题难以解决.所以我想从这个角度切入,希望能把这几个复杂而暧昧的多角关系从最实用的角度来阐述清楚. 在问题的最初,我 ...