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]表示向上走的最遠距離。

TIM截圖20170811083559

對於這棵樹上的節點2來說,2經過子樹的最遠距離是dp[2][0](紅顏色的),利用dfs每次向上更新節點的就能記錄。

對於節點2經過父親節點的最遠距離,要更麻煩點。首先已經跑完了第一個dfs知道了每個點到它子樹下的最遠距離。通過觀察可以發現2過他父親節點的最遠距離就是2到1的距離加上1到它子樹的葉子的最遠距離。但是這樣會出現一個問題,如果最遠距離是(1-2-4)就會得到最遠的距離是(2-1-2-4)很明顯是錯誤的。因為如此,要用一個dp[2][1]記錄次遠的距離,當第一個dfs更新到1節點的時候,記錄下次遠的距離。1的節點是2和3,要是2被選中為最遠的距離,那么3就是次遠的,這樣當2向上訪問的判斷他是不是最長的那個(dp[v][0] + edge[i].val == dp[u][0]) ,要是最長的就換用次長的邊

 

#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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM