B - Roads in the North POJ-2631

C++剑指offer:[POJ]2631 Roads in the North - 用树形DP的方式求出一棵树的直径 前言 此题是道很简单的题(做法不单一,不仅只有树形DP的方法) 做完了这道题才发现此题原来是一道求树的直径的题,也就是求树上两个节点的最大距离。 题目 问题 N(2692): [POJ2631]北极地区的路 时间限制:1 Sec内存限制:128 MB 题目描述 寒冷、人烟稀少的北极地区修建与维护道路时非常昂贵的。因此,修路时只会在任意两个村子之间形成一条唯一的路径,并且中途... 阅读详情

B - Roads in the North

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit

Status
Description
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
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
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22


#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int MAXN=1e4+10;
int head[MAXN],dis[MAXN];
bool vis[MAXN];
int edgenum,ans,Tnode,n;

struct Edge{
    int from;
    int to;
    int val;
    int next;
}edge[MAXN];

void init()
{
    memset(head,-1,sizeof(head));edgenum=0;
}

void addedge(int u,int v,int w)
{
    edge[edgenum].from=u;
    edge[edgenum].to=v;
    edge[edgenum].val=w;
    edge[edgenum].next=head[u];
    head[u]=edgenum++;
}

void bfs(int s)
{
    memset(dis,0,sizeof(dis));memset(vis,false,sizeof(vis));
    ans=0;
    queue<int> que; que.push(s); vis[s]=true; dis[s]=0,Tnode=s;

    while(!que.empty())
    {
        int now=que.front(); que.pop();
        for(int i=head[now];i!=-1;i=edge[i].next)
        {
            int go=edge[i].to;
            if(!vis[go])
            {
                if(dis[go]<dis[now]+edge[i].val)
                dis[go]=dis[now]+edge[i].val;
                vis[go]=true;
                que.push(go);
                if(ans<dis[go])//灵活运用  此题中无节点数目  
                {
                   ans=dis[go];
                   Tnode=go;
                }
            }
        }
    }

}

int main()
{
    int u,v,w;
    init();//忘了你 连结果都不给一个 输入也输不进去 
   //   n=0;
    while(~scanf("%d%d%d",&u,&v,&w))
    {
    //  if(!u) break;  没有结束条件啊 
        addedge(u,v,w);
        addedge(v,u,w);
    //  ++n; 
    }
    bfs(1);//节点从0,1开始模板不用变 
    bfs(Tnode);
    printf("%d\n",ans);
    return 0;
}
POJ 2631Roads in the North【树的直径】【模板题】 POJ 2631:Roads in the North   总时间限制:  1000ms   内存限制:  65536kB 描述 Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are bu... 阅读详情

相关推荐

Fat-Tree Topo Architecture(胖树拓扑结构)

背景 最近在搜索关于Fat-Tree的相关资料时,发现网上内容比较少,很多博客是相互转载的,拓扑结构图也加载不出来,使得对Fat-Tree的理解过于抽象。 实践 传统数据中心采用多层级的树形结构,这种结构针对客户端/服务器(C/S)模式能有较好的效果。树形结构包括单根树和多根树。多根数的根节点往往作为备份节点存在(我们以方格代表交换机)。 传统单根/多根拓扑结构有以下缺点:成本高,根部交换机必须要有足够大的带宽来满足下层服务器之间的通信;性能瓶颈,无法满足数据中心内部大规模的MapReduce和数据拷贝。

金色牛神的专栏 3万+

poj2631(树的直径)

解题思路:具体做法就是先随便选一个点,进行bfs或dfs,然后距离这个点最远的点,一定是树直径中的一个端点。然后再从这个端点bfs或dfs求得直径。 证明详见https://www.cnblogs.com/a-clown/p/6131346.html #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;vector&gt; #...

qq_39861441的博客 311

Autosar Os降低CPU负载的方案总结

由于各个项目的实际场景不尽相同,可能需要有针对性的对症优化。如果你还有别的一些优化手段欢迎留言交流。

3679

poj 2631 (求树上的最大距离 基础题)

题目链接:http://poj.org/problem?id=2631 求树上的最大距离 方法

乐观之奋斗者 798

【深度学习新浪潮】李飞飞团队VAGEN框架深度解析:让VLM智能体学会“三思而后行“

VAGEN框架的真正价值,不仅在于性能指标的突破,更在于重新定义了VLM智能体的发展方向——人工智能的进步不应只追求"做得对",更要实现"想得清"。李飞飞团队通过强制显式思考、动态表示适配和精细化奖励三大创新,让智能体第一次具备了可解释的世界建模能力。正如人类通过"观察-预测-行动"认识世界,VAGEN为AI注入的这种思考范式,或许正是通往通用人工智能的关键一步。对于开发者而言,开源的VAGEN框架不仅提供了强大的工具,更揭示了一个重要理念:让AI学会思考,比让它学会执行更重要。

专注:太空算力、AI infra、异构调度、大模型成本工程 281

斗鱼直播间弹幕爬虫实战:WebSocket连接与实时数据采集

在数据驱动的时代,直播平台的弹幕数据蕴含着巨大的价值——用户行为分析、舆情监控、弹幕情感分析、直播热度评估等应用场景层出不穷。斗鱼作为国内领先的直播平台,其弹幕系统基于WebSocket协议实现实时双向通信。本文将带您从零开始,使用Python构建一个完整的斗鱼直播间弹幕爬虫系统。

2201_76125261的博客 138

[树上DP] POJ2631 树的最长路径(最远点对)

题目 对于一棵n个结点的无根树,找到一条最长路径。换句话说,要找到两个点,使得它们的距离最远。 POJ2631 思路 和树的重心问题一样,先把无根树转成有根树。对于任意结点i,经过i的最长路就连接i的两棵不同子树u和v的最深叶子的路径。 基本的求法是,先随便找一个点作为根结点转换为无根树后,遍历每一个点,找出当i为根结点时的子树到叶子的最大距离d(j),在根据d(j)求出结点i作...

OI - IceCab 3242

Aurix TC3xx系列MCU ResourceM模块配置(多核资源分配)

为减轻主核的负载率或者平衡各个核的资源分配,通常需要把一些MCU内部资源分配到从核上,在EB tresos工具中,通过ResourceM模块实现多核资源分配。

m0_38045338的博客 1388

poj 2631: Roads in the North (树的直径)

题目来源:http://poj.org/problem?id=2631Roads in the NorthTime Limit: 1000MS Memory Limit: 65536K Total Submissions: 3351 Accepted: 1655 DescriptionBuilding and maintaining roads among communitiesin...

da_kao_la的博客 365

【软件系统架构】系列五:TCP/IP 协议栈

TCP/IP协议栈是现代网络通信的核心框架,采用分层架构设计,包含应用层、传输层、网络层和网络接口层四层结构。应用层提供HTTP、FTP等用户服务接口;传输层通过TCP/UDP实现端到端通信;网络层负责IP寻址和路由选择;网络接口层处理物理数据传输。数据在传输过程中经历层层封装,从应用层报文最终转换为物理层的比特流。该协议栈通过三次握手建立连接、数据传输、四次挥手断开连接的流程,确保可靠通信。作为互联网的基础,TCP/IP协议栈具有跨平台、标准化和可扩展的特点,广泛应用于各类网络设备和嵌入式系统中。

moton2017的博客 2164

FastAPI基础项目:仿头条新闻的web项目,实现基本的新闻列表页和详情页查看功能

该项目是一个基于FastAPI和MySQL的新闻应用入门项目。主要包含新闻列表页和详情页两个功能页面。项目结构清晰,包含主应用文件、数据库脚本和HTML模板。使用SQLAlchemy进行数据库操作,通过脚本可自动生成50条示例新闻数据。环境配置简单,仅需安装FastAPI、MySQL驱动等基础依赖,通过.env文件管理数据库连接。该项目适合作为FastAPI+MySQL的入门学习案例,展示了基本的CRUD操作、分页查询和模板渲染功能。

数据知道的博客 4429

poj 2631 Roads in the North(裸树的直径)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a

小张的专栏 794

Lammps模拟剪切作用下的位错滑移

Lammps模拟位错与障碍物作用。在lammps中实现周期性位错阵列模型模拟剪切作用下1/2刃位错与障碍物的交互作用

Rockyoung 3461

POJ-2631.Roads in the North(求树的直径)

2631.Roads in the North Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route...

紫藤&code的博客 224

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

POJ 2631 Roads in the North 【树的直径】

题意:输入是一系列行,每行包含三个正整数:一个村的数量,一个不同的村庄的数量,以及连接村庄的路段长度,以公里为单位。 所有路段都是双向的。 您要输出一个整数:该地区最偏远的两个村庄之间的道路距离。 现在可以直接裸树的直径的模板了。

AC_Dreameng 1025

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Roads in the North POJ - 2631

Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route fro...

weixin_30301449的博客 115

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

poj 2631 Roads in the North

原题: Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5309 Accepted: 2508 Description Building and maintaining roads among communities in the far North is an expensive bu...

@fei 279

【POJ 2631】Roads in the North

1.题目链接。求树的直径裸题,树的直径定义为树上距离最远的两个点。至于求法,bfs,dfs,树DP都可以。简单说一下如何bfs,因为我觉得这个最好实现。(但是据说没办法处理负边权)。其实bfs很简答,就是先随机找一个点,然后找出距离这个点最远的点,再从这个点bfs找出距离这个点最远的点,就是直径。为什么??我举个例子,在一张图像中,是一个人,你想知道这个人的身高,那么你可以现在图上任选一个点,找到...

YG爱木木 290

【POJ】2631 - Roads in the North(树的直径)

点击打开题目 Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2526   Accepted: 1241 Description Building and maintaining roads among com

wyg1997 826

【POJ 2631】 Roads in the North

Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5802 Accepted: 2731 Description Building and maintaining roads among communities in t...

weixin_30734435的博客 120

树的直径学习小记 Poj 1985 Cow Marathon+Poj 2631 Roads in the North

1. 树上面求最长路简单路(无环). 就是树的直径问题. 2. 树的直径问题经典解法:两遍BFS                          (1). 一开始任取一个点u进行搜索查找出距离点u最远距离的点v和长度.                          (2). 第二次BFS则从第一次中的点v找出距离点v最远距离的点的路径长度.                3. 问题正

whyorwhnt的专栏 1167

UVA10308 POJ2631 Roads in the North【DFS】

Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7827 Accepted: 3603 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that the

海岛Blog 403

POJ 2631 -- Roads in the North【树的直径 && 裸题】

Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2358   Accepted: 1156 Description Building and maintaining roads among communities in th

_阿阿阿阿欢 1733

POJ 2631 Roads in the North(树的直径)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a v...

邵光亮的博客 651

POJ2631 Roads in the North

题面:http://poj.org/problem?id=2631 本题是求树的直径裸题。 Code: #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<cstdlib> #include<algorithm&gt...

weixin_30505043的博客 137
上一篇: E - Farthest Nodes in a Tree
下一篇: A - Cow Marathon POJ-1985
DTL66
博客等级 码龄10年 32粉丝 267原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值