WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
title: WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS
date: 2020-03-20 10:43:00
categories: acm
tags: [acm,woj,图论]
用到了树的 直径
1 描述
Tom is an explorer and now he is in a mysterious cave. He finds that there are lots of rooms in it. You are ensured that these rooms are connected together by some roads and there is only one way between any two rooms. That is, all the rooms are connected as a tree.
Now, Tom wants to travel all the rooms in this mysterious cave. The roads and rooms can be passed more than once. But there are too many rooms here and he doesn?t want to waste too much time and wants to find a walking sequence which can minimum the distance he walks. Note, Tom can select any room in the cave as the start point in his exploration.
Given the distance of roads in the cave, please write a program to calculate the minimum distance Tom has to go in his exploration.
输入格式
Standard input will contain multiple test cases. The first line of the input if a single integer T (1 <= T <= 50) which is the number of test cases.
Each test case starts with an integer N (2 <= N <= 50000), which is the number of the rooms. The following N - 1 lines contains three integers each, u (1 <= u <= N), v (1 <= v <= N) and d (1 <= d <= 2000). u and v is the serial number of the rooms and d is the distance between Room u and Room v .Note that, the serial numbers of the rooms start at 1.
输出格式
Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last test case.
For each test case, print a line containing the minimum distance Tom should go.
样例输入
2
2
1 2 3
3
1 2 3
1 3 4
样例输出
Case 1:
3
Case 2:
7
2 分析
给一棵树(节点间只有一条路(可能无边)),节点间边有正权。求从任一节点开始遍历这棵树所有点的最小权(节点可重复经过)
笔记加一条 数据结构-树的性质。树性质是向上只有一个节点,向下有没有限制个节点。(好像没什么用),
画图知道无论从哪个点开始沿一条边出发,把经过这条边可以到达的点看成集合A,整个树为集合T,要想去T-S,必然要原路返回到起始点
(就是说把这个点砍了就是二分图)。所以这题看成两个过程,先是从起始点s遍历A,然后从最后到的点返回到起始点s,再遍历T-S
然后再画图可知从哪个点开始都是一样的(大雾,不一样!)
然后我感觉做过这种题,搜了一下想起来了,只需要找到树的直径然后直径上其他的树枝走两遍就行了,直径只走一遍
然后转化为无根树求直径的问题.
两次DFS/BFS都可以。任选一点做DFS,找到的最长点再做DFS,再次找到的最长点就是直径
//当然最短路Bellmanford 等算法也可以
类似的题POJ1985+POJ2631
3 code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct Node{
int v,d;
};
int n,dis[50001];
vector<Node> tree[50001];
Node tmp1,tmp2;
int T,casee=0;
int u,v,d,i,ans,faresta,tpans;
int BFS(int x)
{
for(i=0;i<=n;i++) //n.输入点的index到了n,一开始顺手写成n-1了
dis[i]=0;
int vis[n+5];
memset(vis,0,sizeof(vis));
queue<int> q;
q.push(x);
vis[x]=1;
int point = 1;
while(!q.empty())
{
int f=q.front();
q.pop();
if(dis[f]>tpans)
{
tpans = dis[f];
point = f;
}
for(i=0;i<tree[f].size();i++)
{
Node tmp = tree[f][i];
if(vis[tmp.v]==0)
{
vis[tmp.v]=1;
dis[tmp.v] = dis[f] + tmp.d;
q.push(tmp.v);
}
}
}
return point;
}
void build(){
for(i=1;i<=n;i++)
tree[i].clear();
ans=0;
for(i=0;i<n-1;i++){
cin>>u>>v>>d;
tmp1.v=v;tmp1.d=d;
tmp2.v=u;tmp2.d=d;
tree[u].push_back(tmp1); //相连的边,点
tree[v].push_back(tmp2);
ans=ans+2*d;
}
return;
}
int main(){
cin>>T;
while(T--){
ans=0;
if(casee!=0)
cout<<endl;
casee++;
cin>>n;
build();
tpans=0;
faresta=BFS(1);
tpans=0;
BFS(faresta);
printf("Case %d:\n",casee);
cout<<ans-tpans<<endl;
}
system("pause");
return 0;
}
WOJ1024 (POJ1985+POJ2631) Exploration 树/BFS的更多相关文章
- poj1985 / poj2631(树的直径)
poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- 1021. Deepest Root (25) -并查集判树 -BFS求深度
题目如下: A graph which is connected and acyclic can be considered a tree. The height of the tree depend ...
- BZOJ3073 PA2011Journeys(线段树+bfs)
线段树优化建图裸题.建两棵线段树,一棵表示入一棵表示出.对题中所给的边新建一个虚拟点,将两段区间拆成线段树上对应区间,出线段树中对应区间所表示的点向虚拟点连边权0的边,虚拟点向入线段树中对应区间所表示 ...
- CF1076D Edge Deletion 最短路径树+bfs
题目描述 You are given an undirected connected weighted graph consisting of n n n vertices and m m m edg ...
- LeetCode Top100 Liked Questions
1. TwoSum https://www.cnblogs.com/zhacai/p/10429120.html easy 2. Add Two Numbers https://www.cnblog ...
- 【spoj8222-Substrings】sam求子串出现次数
http://acm.hust.edu.cn/vjudge/problem/28005 题意:给一个字符串S,令F(x)表示S的所有长度为x的子串中,出现次数的最大值.求F(1)..F(Length( ...
- 关于SAM和广义SAM
关于SAM和广义SAM 不是教程 某些思考先记下来 SAM 终于学会了这个东西诶...... 一部分重要性质 确定一个重要事情,S构造出的SAM的一个重要性质是当且仅当对于S的任意一个后缀,可以从1号 ...
- ACM模板_axiomofchoice
目录 语法 c++ java 动态规划 多重背包 最长不下降子序列 计算几何 向量(结构体) 平面集合基本操作 二维凸包 旋转卡壳 最大空矩形 | 扫描法 平面最近点对 | 分治 最小圆覆盖 | 随机 ...
随机推荐
- 误删数据库怎么办?mysql 回滚,撤销操作,恢复数据
刚刚不小心把数据库删掉了,于是想着上网上找找有没有可以恢复数据库的方法,没想到还真有,除了备份以外,还有以下方法. 在mysql有时执行了错误的update或者delete时导致大量数据错误恢复的办法 ...
- Python批量 png转ico
Python 批量 png 转 ico 一.前言: 首先说一下ico文件的作用:ico是windows的图标文件格式,可以用于浏览器首段图标显示,也可以用于Windows软件.我的话一般用来美化文件夹 ...
- 2V升3.3V芯片,输出500MA,低功耗10uA解决方案
2V的输入电压其实非常少,一般都是镍氢电池1.2V,干电池1.5V,来给玩具,MCU单片机,模块啊,等等供电.不过2V的供电电源或者设备确实是不常见的. 一般2V升3.3V,需要升压芯片PW5100即 ...
- uni-app开发经验分享二十: 微信小程序 授权登录 获取详细信息 获取手机号
授权页面 因为微信小程序提供的 权限弹窗 只能通用户确认授权 所以可以 写一个授权页面,让用户点击 来获取用户相关信息 然后再配合后台就可以完成登录 <button class="bt ...
- ././include/linux/kconfig.h:4:32: fatal error: generated/autoconf.h: No such file or directory 解决办法
我在编写内核驱动模块的时候报了一个非常奇怪的错误,如下图: 在目录下看了一下确实没有发现这个文件,感觉很奇怪,因为我记得之前编译模块是没有错误的,所以不可能是我代码写的有问题. 查阅了资料很多说要清除 ...
- pycharm2021永久激活
Pycharm破解版地址: 链接: https://pan.baidu.com/s/1dEkzKRFMaeNjWF4h7y2TdQ 提取码: eqr3 Anaconda地址:版本是python3.6 ...
- TekRADIUS5.5安装教程
1.下载地址:https://www.kaplansoft.com/TekRADIUS/release/tekradius.zip 2.解压安装,双击一步默认安装下来就是了 3.配置连接数据库: 4. ...
- C指针的这些使用技巧,掌握后立刻提升一个Level
这是道哥的第016篇原创 关注+星标公众号,不错过最新文章 目录 一.前言 二.八个示例 1. 开胃菜:修改主调函数中的数据 2. 在被调用函数中,分配系统资源 2.1 错误用法 2.2 正确用法 3 ...
- 阿姆达尔定律 Amdahl's law
Amdahl's law - Wikipedia https://en.wikipedia.org/wiki/Amdahl%27s_law 阿姆达尔定律(英语:Amdahl's law,Amdahl' ...
- 慕课网金职位 Java工程师2020 百度网盘下载
百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...