poj2631 Roads in the North(求树的直径)
Roads in the North
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 2941 | Accepted: 1447 |
Description
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Output
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22
code
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring> using namespace std; const int MAXN = ; struct Edge{
int to,w,nxt;
Edge(){}
Edge(int x,int y,int z){to = x,w = y,nxt = z;}
}e[]; int head[MAXN<<],dis[MAXN];
int tot;
queue<int>q; int bfs(int x)
{
memset(dis,-,sizeof(dis));
q.push(x);
dis[x] = ;
int id,mx = ; while (!q.empty())
{
int u = q.front();
q.pop();
for (int i=head[u]; i; i=e[i].nxt)
{
int v = e[i].to, w = e[i].w;
if (dis[v]==-) //双向边,只算一次
{
dis[v] = dis[u]+w;
if (dis[v]>mx) mx = dis[v],id = v;
q.push(v);
}
}
}
return id;
}
int main()
{
int u,v,w;
while (scanf("%d%d%d",&u,&v,&w)!=EOF)
{
e[++tot] = Edge(v,w,head[u]);
head[u] = tot;
e[++tot] = Edge(u,w,head[v]);
head[v] = tot;
}
printf("%d",dis[bfs(bfs())]);
return ;
}
poj2631 Roads in the North(求树的直径)的更多相关文章
- poj2631 求树的直径裸题
题目链接:http://poj.org/problem?id=2631 题意:给出一棵树的两边结点以及权重,就这条路上的最长路. 思路:求实求树的直径. 这里给出树的直径的证明: 主要是利用了反证法: ...
- poj1985 Cow Marathon (求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 3195 Accepted: 1596 Case ...
- hdu 4607 Park Visit 求树的直径
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...
- [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)
http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...
- 4612 warm up tarjan+bfs求树的直径(重边的强连通通分量)忘了写了,今天总结想起来了。
问加一条边,最少可以剩下几个桥. 先双连通分量缩点,形成一颗树,然后求树的直径,就是减少的桥. 本题要处理重边的情况. 如果本来就两条重边,不能算是桥. 还会爆栈,只能C++交,手动加栈了 别人都是用 ...
- HDU4612+Tarjan缩点+BFS求树的直径
tarjan+缩点+树的直径题意:给出n个点和m条边的图,存在重边,问加一条边以后,剩下的桥的数量最少为多少.先tarjan缩点,再在这棵树上求直径.加的边即是连接这条直径的两端. /* tarjan ...
- hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Su ...
- 求树的直径+并查集(bfs,dfs都可以)hdu4514
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 这题主要是叫我们求出树的直径,在求树的直径之前要先判断一下有没有环 树的直径指的就是一棵树上面距 ...
- HDU 4514 - 湫湫系列故事——设计风景线 - [并查集判无向图环][树形DP求树的直径]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4514 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- (求树的直径)Warm up -- HDU -- 4612
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 给一个无向图, 加上一条边后,求桥至少有几个: 那我们加的那条边的两个顶点u,v:一定是u,v之 ...
随机推荐
- hibernate课程 初探单表映射1-3 hibernate简介
1 hibernate定义: Java领域一项开源的orm框架技术: hibernate对jdbc进行轻量级的封装. hibernate 作为持久层存在.就是通过对象关系映射把项目中的对象持久化到数据 ...
- React:关于在delegate中的confirm或者alert在多次弹出
如何试验出是delegate的过程就不解说了 直接上解决方案:(当然我的解决方案只适合一定情况) 取比较简单的代码来解说,例如 我一开始使用delegate的原因是由于我有部分html页面是ajax中 ...
- Angular ui-route介绍
参考博客: https://www.cnblogs.com/haogj/p/4885928.html 原文地址:http://www.ng-newsletter.com/posts/angular-u ...
- linux 命令——17 whereis(转)
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和 find相比,whereis查找的速度 ...
- 使用bouncycastle进行DESede/DESeee/AES128/AES192/AES256的加解密
前言 默认的jdk不支持DESeee的算法,本地化的JDK中配置有拦截规则,可以通过使用bouncycastle的jar包中的DESEngine类来进行DESeee算法的运算. DES的8字节加解密 ...
- 【BZOJ2427】[HAOI2010] 软件安装(缩点+树形DP)
点此看题面 大致题意: 有\(N\)个软件,每个软件有至多一个依赖以及一个所占空间大小\(W_i\),只有当一个软件的直接依赖和所有的间接依赖都安装了,它才能正常工作并造成\(V_i\)的价值.求在容 ...
- SpringMVC+Hibernate框架快速搭建
1. 新建Maven项目springmvc 2. pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- angular实现全屏显示效果
- C++的新特性for-each
C++实验课要求用for each 循环来实现关联容器 map 的输出,一开始完全萌比.查了好久的资料才整理出下面的: C++11新特性之一就是类似java的for each循环: map<in ...
- JS大小转化B KB MB GB的转化方法
function conver(limit){ var size = ""; ){ //如果小于0.1KB转化成B size = limit.toFixed() + "B ...