1. 程式人生 > >HDU 2169 Computer[樹形dp]

HDU 2169 Computer[樹形dp]

res file max split 最短 電腦網絡 父親節 vid problem

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]