POJ 1985--Cow Marathon【树的直径 && 模板】

Cow Marathon

Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4182   Accepted: 2116
Case Time Limit: 1000MS

Description

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence
of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms).
Help him determine the distances between this farthest pair of farms.

Input

* Lines 1.....: Same input format as "Navigation Nightmare".

Output

* Line 1: An integer giving 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 to farm 5 and is of length 20+3+13+9+7=52.

第一道树的直径题

#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#define maxn 40000+10
#define maxm 80000+10
#define INF 0x3f3f3f3f
using namespace std;

struct node{
    int u, v, w, next;
};

node edge[maxm];
int head[maxn], cnt;
int dist[maxn], vis[maxn];
int n, m;
int sum, ed ;

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, w, head[v]};
    head[v] = cnt++;
}

void getmap(){
    char str[5];
    while(m--){
        int a, b, c;
        scanf("%d%d%d%s", &a, &b, &c, str);
        add(a, b, c);
    }
}

void bfs(int sx){
    queue<int>q;
    for(int i = 1; i <= n; ++i){
        vis[i] = 0;
        dist[i] = 0;
    }
    vis[sx] = 1;
    sum = 0;
    ed = sx;
    q.push(sx);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1 ;i = edge[i].next){
            int v = edge[i].v;
            if(!vis[v]){
                dist[v] = dist[u] + edge[i].w;
                if(sum < dist[v]){
                    ed = v;
                    sum = dist[v];
                }
                vis[v] = 1;
                q.push(v);
            }
        }
    }
}

int main (){
    while(scanf("%d%d", &n, &m) != EOF){
        init();
        getmap();
        bfs(1);
        bfs(ed);
        printf("%d\n", sum);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-28 01:04:17

POJ 1985--Cow Marathon【树的直径 && 模板】的相关文章

POJ 1985 Cow Marathon(树的直径)

http://poj.org/problem?id=1985 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径) 分析: 对于树的直径问题, <<算法导论>>(22 2-7)例题有说明. 详细解法: 首先从树上随意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b点最远的点c. 那么bc间的距离就是树的直径. 证明: 1.    a

BZOJ 3363 POJ 1985 Cow Marathon 树的直径

题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 80010 using namespace std; int

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

poj1985 Cow Marathon --- 树的直径

树的直径即树中最长的路径的长度. 用两次dfs,第一次从任意点出发求得一个最远点p, 第二次从p出发求得最远点,这条路径就是最长路,即所求. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include &l

poj 1985 Cow Marathon

题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon ro

poj:1985:Cow Marathon(求树的直径)

Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case Time Limit: 1000MS Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has com

POJ 1985 Cow Marathon (树形DP,树的直径)

题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: #include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; const int max

POJ 1985 Cow Marathon【树的直径】

题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实就是把后面的字母无视即可 2.这题数据量没给,所以把数组开得很大才敢交TUT #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <

算法笔记--树的直径模板

思路: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c之间的距离就是树的直径. 模板: const int N=1e6+5; int head[N]; int dis[N]; bool vis[N]; int cnt=0,b,mxn=0; struct edge { int to,w,next; }edge[N]; void add_edge(int u,