HDU 2169 Computer[树形dp]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 2169 Computer[树形dp]相关的知识,希望对你有一定的参考价值。

Computer

时限:1000ms

Problem Description

A school bought the first computer some time ago(so this computer‘s id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 

技术分享



Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.

 
Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
 
Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
Sample Input
5 1 1 2 1 3 1 1 1
Sample Output
3 2 3 4 4

题意:

给你一个树状的电脑网络,求出每个点到其他点的最短距离。

思路:

我做搜索的时候做过这道题,当时用三个bfs过掉了,树的直径过掉的,今天练习数位dp,所以用数位dp写。思路很清晰,从u点出发有两个状态,一个是向下的最短,一个是过父亲节点,向上的最短距离。对于向下的很好解决,先从根节点一层一层的推上去。但是对于过父亲节点的情况,当u为其父亲节点向下的最远路径上的一点的时候,可能是u->u的父亲节点->u->u的子节点。所以要记录u的子树的次远距离,这样就当上面的情况发生的时候我们选择次远的距离作为从该点出发的最远距离。

dp[u][0]表示u向下走的最远距离,dp[u][1]表示u向下走的次远距离,dp[u][2]表示向上走的最远距离。

#include "stdio.h"
#include "vector"
#include "string.h"
#include "algorithm"
using namespace std;
const int maxn = 10000 + 10;
int head[maxn];
struct node {
    int to, next, val;
} edge[maxn];
int tot = 0;
int dp[maxn][3];
void add_edge(int u,int v,int w) {
    edge[tot].to = v;  edge[tot].val = w;  
    edge[tot].next = head[u];  
    head[u] = tot++;  
} 
void dfs1(int u) {
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        dfs1(v);
        int sval = dp[v][0] + edge[i].val;
        if (sval >= dp[u][0]) {
            dp[u][1] = dp[u][0]; 
            dp[u][0] = sval;
        }
        else if (sval > dp[u][1]) dp[u][1] = sval;
    } 
}
void dfs2(int u) {
    for (int i = head[u]; i != -1; i = edge[i].next) {
        int v = edge[i].to;
        if (dp[v][0] + edge[i].val == dp[u][0]) 
            dp[v][2] = max(dp[u][2], dp[u][1]) + edge[i].val;
        else 
            dp[v][2] = max(dp[u][2], dp[u][0]) + edge[i].val;
        dfs2(v);
    }
}
void init() {
    memset(head, -1, sizeof(head));
    memset(dp, 0, sizeof(dp));
    tot = 0;
}
int main(int argc, char const *argv[])
{
    int n;
    while (scanf("%d", &n) != EOF) {
        init();
        for (int i = 2; i <= n; i++) {
            int u, w;
            scanf("%d%d", &u, &w);
            add_edge(u, i, w);
        }
        dfs1(1);  dfs2(1);
        for (int i = 1; i <= n; i++) {
            printf("%d\n", max(dp[i][0], dp[i][2]));
        }
    }
    return 0;
}

再附上曾经用树的直径A掉的代码。

技术分享
#include <bits/stdc++.h>
#define MAXN 10010
using namespace std;
struct node{
    int from, to, val, next;
} edge[MAXN*2];
int dist1[MAXN], head[MAXN], edgenum, s, dist2[MAXN];
int ans;
bool vis[MAXN];
void init() {
    memset(head, -1, sizeof(head));
    edgenum = 0;
}
void addEdge(int x, int y, int z) {
    edge[edgenum].from = x;
    edge[edgenum].to = y;
    edge[edgenum].val = z;
    edge[edgenum].next = head[x];
    head[x] = edgenum++;
}
void bfs1(int x) {
    queue<int> que; ans = 0;
    memset(vis, false, sizeof(vis));
    memset(dist1, 0, sizeof(dist1));
    while (!que.empty()) que.pop();
    que.push(x); vis[x] = true;
    while (que.size()) {
        int a = que.front(); que.pop();
        for (int i = head[a]; i != -1; i = edge[i].next) {
            int b = edge[i].to;
            if (!vis[b] && dist1[b] < dist1[a] + edge[i].val) {
                dist1[b] = dist1[a] + edge[i].val;
                if(ans < dist1[b]) {
                    ans = dist1[b]; s = b;
                }
                vis[b] = true; que.push(b);
            }
        }
    }
}void bfs2(int x) {
    queue<int> que; ans = 0;
    memset(vis, false, sizeof(vis));
    memset(dist2, 0, sizeof(dist2));
    while (!que.empty()) que.pop();
    que.push(x); vis[x] = true;
    while (que.size()) {
        int a = que.front(); que.pop();
        for (int i = head[a]; i != -1; i = edge[i].next) {
            int b = edge[i].to;
            if (!vis[b] && dist2[b] < dist2[a] + edge[i].val) {
                dist2[b] = dist2[a] + edge[i].val;
                if(ans < dist2[b]) {
                    ans = dist2[b]; s = b;
                }
                vis[b] = true; que.push(b);
            }
        }
    }
}
int main() {
    int a, b, c, n, m;
    while (scanf("%d", &n) != EOF) {
        init();
        for (int i = 2; i <= n; i++) {
            scanf("%d%d", &a, &b);
            addEdge(i, a, b); addEdge(a, i, b);
        }
        bfs1(1); bfs1(s); bfs2(s);
        for (int i = 1; i <= n; i++) {
            printf("%d\n", max(dist1[i], dist2[i]));
        }
    }
    return 0;
}
View Code

 



以上是关于HDU 2169 Computer[树形dp]的主要内容,如果未能解决你的问题,请参考以下文章

HDU2196 Computer(树形DP)

hdu 2196 Computer 树形DP

题解报告:hdu 2196 Computer(树形dp)

HDU2196 Computer(树形DP)

hdu2196 Computer[树形dp]

HDU 2196.Computer 树形dp