POJ 1849 Two (树形dp 树的直径 两种方法)


Two
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1232 Accepted: 619

Description

The city consists of intersections and streets that connect them.

Heavy snow covered the city so the mayor Milan gave to the winter-service a list of streets that have to be cleaned of snow. These streets are chosen such that the number of streets is as small as possible but still every two intersections to be connected i.e. between every two intersections there will be exactly one path. The winter service consists of two snow plovers and two drivers, Mirko and Slavko, and their starting position is on one of the intersections.

The snow plover burns one liter of fuel per meter (even if it is driving through a street that has already been cleared of snow) and it has to clean all streets from the list in such order so the total fuel spent is minimal. When all the streets are cleared of snow, the snow plovers are parked on the last intersection they visited. Mirko and Slavko don’t have to finish their plowing on the same intersection.

Write a program that calculates the total amount of fuel that the snow plovers will spend.

Input

The first line of the input contains two integers: N and S, 1 <= N <= 100000, 1 <= S <= N. N is the total number of intersections; S is ordinal number of the snow plovers starting intersection. Intersections are marked with numbers 1...N.

Each of the next N-1 lines contains three integers: A, B and C, meaning that intersections A and B are directly connected by a street and that street's length is C meters, 1 <= C <= 1000.

Output

Write to the output the minimal amount of fuel needed to clean all streets.

Sample Input

5 2
1 2 1
2 3 2
3 4 2
4 5 1

Sample Output

6

Source

Croatia OI 2002 national – second day, seniors

题目链接:http://poj.org/problem?id=1849

题目大意:有两个人从同一点出发,遍历一棵树的所有边,每个边有个权值,求遍历的最小代价,

题目分析:答案就是所有边权值乘2减去直径,这里不证了,证明的话,简单想就是两个人要走的公共路最长,除了公共路其他部分肯定都走了两次,因为是树,两点间只有唯一的路,所以最长的那条路我不走重复必然就是最小代价了,树上最长链就是树的直径
关于怎么求直径,这里也不证了记个结论,两种方法,一是两次dfs,第一次任意找一个点搜索从它开始能走到的最远的点,那个点必然是树的直径的一个端点,再从这个点开始dfs一下,就搜出了整棵树,二是树形dp的方法,对于任意一个点,搜索其两个能延伸最远和次远的儿子,把两个距离相加再加上两儿子到父亲的距离再取最大就是树的直径,这个很好证,两儿子肯定不在一条延伸路径上,画个图看的更清楚

两次DFS:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
int const MAX = 100005;
int n, s, cnt;
int dis[MAX], head[MAX];

struct EGDE
{
    int v, w, next; 
}e[MAX];

void Add(int u, int v, int w)
{
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt++;
}

void DFS(int u, int fa, int d)
{
    for(int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        int w = e[i].w;
        if(v != fa)
        {
            dis[v] = w + d;
            DFS(v, u, w + d);
        }
    }
    return;
}

int main()
{
    cnt = 0;
    int sum = 0;
    scanf("%d %d", &n, &s);
    memset(head, -1, sizeof(head));
    memset(dis, 0, sizeof(dis));
    for(int i = 0; i < n - 1; i++)
    {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        Add(u, v, w);
        Add(v, u, w);
        sum += 2 * w;
    }
    dis[s] = 0;
    DFS(s, -1, 0);
    int o, ma = -1;
    for(int i = 1; i <= n; i++)
    {
        if(dis[i] > ma)
        {
            ma = dis[i];
            o = i;
        }
    }
    dis[o] = 0;
    DFS(o, -1, 0);
    ma = -1;
    for(int i = 1; i <= n; i++)
        if(dis[i] > ma)
            ma = dis[i];
    printf("%d\n", sum - ma);
}



树形dp:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 100005;
int head[MAX], dp[MAX][2];
int n, s, cnt, ans;

struct EDGE
{
    int v, w, next;
}e[MAX];

void Add(int u, int v, int w)
{
    e[cnt].v = v;
    e[cnt].w = w;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void DFS(int u, int fa)
{
    dp[u][0] = dp[u][1] = 0;
    for(int i = head[u]; i != -1; i = e[i].next)
    {
        int v = e[i].v;
        int w = e[i].w;
        if(v != fa)
        {
            DFS(v, u);
            if(dp[u][0] < dp[v][0] + w)
            {
                int tmp = dp[u][0];
                dp[u][0] = dp[v][0] + w;
                dp[u][1] = tmp;
            }
            else if(dp[u][1] < dp[v][0] + w)
                dp[u][1] = dp[v][0] + w;
        }
    }
    ans = max(ans, dp[u][1] + dp[u][0]);
    return;
}

int main()
{
    cnt = 0;
    ans = 0;
    memset(head, -1, sizeof(head));
    scanf("%d %d", &n, &s);
    int sum = 0;
    for(int i = 0; i < n - 1; i++)
    {
        int u, v, w;
        scanf("%d %d %d", &u, &v, &w);
        Add(u, v, w);
        Add(v, u, w);
        sum += 2 * w;
    }
    DFS(s, -1);
    printf("%d\n", sum - ans);
}


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值