第一开始想成了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. puppetSvn集成

  2. 一个跨域请求的XSS续

    之前讨论过,在解决post跨域请求时,采用iframe+本域代理页的形式,兼容性(当然是包括IE6啦)是最好的.上次提到,代理页面的作用是:执行本域下的回调函数.就是这个原因,给XSS带来了便利.详细 ...

  3. Codeforces554C:Kyoya and Colored Balls(组合数学计算+费马小定理)

    题意: 有k种颜色,每种颜色对应a[i]个球,球的总数不超过1000 要求第i种颜色的最后一个球,其后面接着的必须是第i+1种颜色的球 问一共有多少种排法 Sample test(s) input o ...

  4. Django之Cookie与Session

    一.cookie 1.cookie使用 def cookie(request): print(request.COOKIES) # 获取所有的COOKIES obj = render(request, ...

  5. 搭建完全分布式的hadoop[转]

    hadoop 创建用户及hdfs权限,hdfs操作等常用shell命令 sudo addgroup hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry# ...

  6. android sqlite数据库封装 实现crud

    android常用的数据保存方式有文件.sharepreferences.数据库.网络.contentprovider集中方式. 文件存储方式,经常使用在缓存整个页面数据,比如电子书内容.html数据 ...

  7. nignx日志格式

    web-master的nginx格式: log_format web_format '$remote_addr $remote_port $remote_user [$time_local] ' '& ...

  8. NFinal 视图—模板

    创建模板 1.新建Header.ascx用户控件,此控件就是模板,修改内容如下: <%@ Control Language="C#" AutoEventWireup=&quo ...

  9. Ajax:Cross-Origin Resource Sharing(转)

    实例:http://blog.csdn.net/hongweigg/article/details/39054403 通过XHR实现Ajax通信的一个主要限制,来源于跨域安全策略.默认情况下,XHR对 ...

  10. 自动化测试CTS命令

    #!/sbin/sh chmod +x /system/bin/input i=0 while [ "$i" != "10" ] do #am instrume ...