POJ 2631 Roads in the North(木の直径を求めて、2回or木DPを遍歴します)

15996 ワード

タイトルリンク:http://poj.org/problem?id=2631
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.
 
あなたに1本の木をあげて、木の上で最も远い2点の距离を求めますいくらです.(タイトルの説明は奇抜で、点数は辺数+1に等しく、すべての演算は2^31-1を超えないと考えられます)
考え方:任意の点から、この点から最も遠い点xを見つけます.さらにxから単源最短路を作り、xから最も遠い点とxがこの木の最も遠い点のペアです.(もちろん木による分治も可能です)
証明書(DISCUSS参照):
     MN->  [1]

  A  DFS      AB->  [2]

  [1] MN AB    .  MN<AM+AN<=AM+AB=BM    [1]  

  [2] B         .     [1]  K AB  B    MN     MN=MK+KN=MK+AN-AK<=MK+AB-AK=MK+BK=BM      MB MN       [2]      [1]    [     A  NK . A NK          MN            ]

  [3]  B  DFS             .   [2].B       

    

 
コード(0 MS):

 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdio>

 4 #include <algorithm>

 5 #include <queue>

 6 using namespace std;

 7 

 8 const int MAXN = 10010;

 9 const int MAXE = 20010;

10 

11 int dis[MAXN], head[MAXN];

12 int to[MAXE], next[MAXE], cost[MAXE];

13 int n, ecnt;

14 

15 void init() {

16     memset(head, -1, sizeof(head));

17     ecnt = 0;

18 }

19 

20 void add_edge(int u, int v, int c) {

21     to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;

22     to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;

23 }

24 

25 int bfs(int st) {

26     memset(dis, 0x3f, sizeof(dis));

27     queue<int> que; que.push(st);

28     dis[st] = 0;

29     while(!que.empty()) {

30         int u = que.front(); que.pop();

31         for(int p = head[u]; ~p; p = next[p]) {

32             int &v = to[p];

33             if(dis[v] > dis[u] + cost[p]) {

34                 dis[v] = dis[u] + cost[p];

35                 que.push(v);

36             }

37         }

38     }

39     int ed = st;

40     for(int i = 1; i <= n; ++i)

41         if(dis[i] > dis[ed]) ed = i;

42     return ed;

43 }

44 

45 int main() {

46     init();

47     n = 1;

48     int u, v, c;

49     while(scanf("%d%d%d", &u, &v, &c) != EOF) {

50         add_edge(u, v, c);

51         ++n;

52     }

53     u = bfs(1);

54     v = bfs(u);

55     printf("%d
", dis[v]); 56 }

View Code
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
後記(2014-7-26):
もちろんこの問題はDPにもなりますが、前は面倒だと思って上記の解法を使いました.今では比較的大きな書き方がありますが、辺権が負数の場合にも当てはまります(上記のやり方では辺権に負数があれば役に立たないそうですが、検証するのがおっくうで興味があるので検証してみましょう......).
考え方は簡単で、木の直径は必然的に木の上のある点から下への最長鎖と次長鎖の和である.実現するときは最長チェーンの大きさを残せばいいので、簡単にコードを見ないといいです.
 
コード(16 MS):

 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdio>

 4 #include <algorithm>

 5 using namespace std;

 6 

 7 const int MAXV = 10010;

 8 const int MAXE = 20010;

 9 

10 int head[MAXV];

11 int to[MAXE], next[MAXE], cost[MAXE];

12 int n, ecnt;

13 

14 void init() {

15     memset(head, -1, sizeof(head));

16     ecnt = 0;

17 }

18 

19 void add_edge(int u, int v, int c) {

20     to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;

21     to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;

22 }

23 

24 int dfs(int u, int f, int &ans) {

25     int maxdep = 0;

26     for(int p = head[u]; ~p; p = next[p]) {

27         int &v = to[p];

28         if(v == f) continue;

29         int tmp = dfs(v, u, ans) + cost[p];

30         ans = max(ans, maxdep + tmp);

31         maxdep = max(tmp, maxdep);

32     }

33     return maxdep;

34 }

35 

36 int main() {

37     init();

38     n = 1;

39     int u, v, c;

40     while(scanf("%d%d%d", &u, &v, &c) != EOF) {

41         add_edge(u, v, c);

42         ++n;

43     }

44     int ans = 0;

45     dfs(1, 0, ans);

46     printf("%d
", ans); 47 }

View Code