【dp】【树的直径】Roads in the North

Roads in the North
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4694 Accepted: 2238
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
Source
The UofA Local 1999.10.16

【分析】
一道求树的直径的裸题。
树的直径的定义就是树上最远两点的距离。

关于树的直径有两种普遍的做法。
一:两遍bfs或dfs。
先从任意一点走一遍bfs或dfs,找到最远距离的点,记为p,随后,从p开始bfs或dfs,再次找到最远的点记为q,则pq为一条直径。
关于证明自行百度,反证法比较好证。

二、树形dp
令对于任意一个点i,我们令Fi为从i出发向儿子节点能走到的最长路,i的儿子节点为j。那么经过这个点i的最长路应该是这个点的所有Fj中最大长度加上次大长度(自行脑补理解)。于是可以一边dp一边找最大值了。

还有一个问题,这题目给的是一棵无根树,可以做成无向图任意找一点dfs转成有根树再做,对于这种操作相对简单的题也可以边dp边做。只需记录走过的节点让它不要往回走就顺利地走出一个树结构了。

【代码】

#include<cstdio>
#include<cstring>
#define maxn 6039
using namespace std;
int n, v[maxn], ans, k, a, b, c, f[maxn];  //v数组标记访问,f为dp数组
int max(int a, int b){return a>b?a:b;}
struct NODE{
    int to, nex, w;
}edge[maxn<<1];
int head[maxn];
void add(int from, int to, int weight){
    edge[k].to = to;
    edge[k].w = weight;
    edge[k].nex = head[from];
    head[from] = k++;
}
int dfs(int node){
       v[node] = 1;
    int max1 = 0, max2 = 0;
       int tmp;
        for(int i = head[node]; i!=-1; i = edge[i].nex){
               if(!v[edge[i].to]){
                       tmp = dfs(edge[i].to)+edge[i].w;
                       if(tmp>=max1){
                               max2 = max1;
                               max1 = tmp;
                               }
                               else if(tmp>=max2){
                                       max2 = tmp;
                               }
                       }
        }
        ans = max(ans, max1+max2);  //找最长路
        return max1;  //返回fi的值
}

int main(){
    freopen("1.in", "r", stdin);
    memset(head, -1, sizeof(head));
    while(scanf("%d%d%d", &a, &b, &c)!=EOF){
        add(a, b, c);
        add(b, a, c);
        n = max(a, max(b, n));
    }
    dfs(1);
    printf("%d", ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值