ACM: 树的直径(两点最长距离) 图论…

来源:互联网 发布:厦门淘宝代运营 编辑:程序博客网 时间:2024/05/21 18:42
Cow Marathon
Description
After hearing aboutthe epidemic of obesity in the USA, Farmer John wants his cows toget more exercise, so he has committed to create a bovine marathonfor his cows to run. The marathon route will include a pair offarms and a path comprised of a sequence of roads between them.Since FJ wants the cows to get as much exercise as possible hewants to find the two farms on his map that are the farthest apartfrom each other (distance being measured in terms of total lengthof road on the path between the two farms). Help him determine thedistances between this farthest pair of farms.

Input

* Lines 1.....: Sameinput format as "Navigation Nightmare".

Output

* Line 1: An integergiving the distance between the farthest pair of farms.

Sample Input

7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S

Sample Output

52

Hint

The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 tofarm 5 and is of length 20+3+13+9+7=52.

题意: 在这棵树的结构中找出两点间的最长距离.

解题思路:
              1. 树上面求最长路简单路(无环). 就是树的直径问题.
              2. 树的直径问题经典: 两遍BFS即可.
              问题分析:
                        (1). 一开始任取一个点u进行搜索查找出距离点u最远距离的点v和长度.
                        (2). 第二次BFS则从第一次中的点v找出距离点v最远距离的点的路径长度.
              3. 问题正确性.
               证明:
                        (1). 情况1: u在最长路上, 那么v一定是最长路的一端.
                              反证法: v不是在最长路的一端, 即有一个v1使得(u->v1)是最长路的一部分,
                                           就有: dist(u->v1) >dist(u->v); 矛盾.
                               即: v在最长路的一端. (第二次BFS就可以找出距离v最远的点的长度.)
                        (2). 情况2: u不再最长路上, 则有点u到点v的路与最长路一定有一个交点c.
                                         并且(c->v)与最长路的后半段是重合的. 即v一定是最长路的一端.

代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define MAX 80005

struct node
{
    int v,w;
    intnext;
}edges[MAX];

int n, m;
int first[MAX], num;
int dist[MAX], ans;
bool vis[MAX];
int point_u;

inline void add(int u,int v,int w)
{
    edges[num].v= v;
    edges[num].w= w;
   edges[num].next = first[u];
    first[u] =num++;
}

void read_graph()
{
   memset(first,-1,sizeof(first));
    int u, v,w;
    charch;
    nodet;
    ans =0;
    num =0;
    for(int i =1; i <= m; ++i)
    {
       scanf("%d %d%d%c",&u,&v,&w,&ch);
      add(u,v,w);
      add(v,u,w);
    }
}

int bfs(int start)
{
   memset(vis,false,sizeof(vis));
    int front =0, rear = 0;
    int qu[MAX],t;
    dist[rear] =0;
    vis[start] =true;
    qu[rear++] =start;
   
    while(front< rear)
    {
       for(int e =first[qu[front]]; e != -1; e = edges[e].next)
       {
          if(!vis[edges[e].v] )
          {
             dist[rear] =dist[front]+edges[e].w;
             qu[rear] =edges[e].v;
            vis[qu[rear]] = true;
            if(dist[rear] > ans)
             {
                ans =dist[rear];
                point_u =qu[rear];
             }
            rear++;
          }
       }
      front++;
    }
    returnans;
}

int main()
{
//   freopen("input.txt","r",stdin);
   while(scanf("%d %d",&n,&m) !=EOF)
    {
      read_graph();
      bfs(1);
      printf("%d\n",bfs(point_u));
    }
   
    return0;
}
0 0
原创粉丝点击