POJ 1985 Cow Maratho【树的直径】

55 篇文章 1 订阅
12 篇文章 0 订阅

Cow Marathon
Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 5349 Accepted: 2627
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. 

Source


原题链接: http://poj.org/problem?id=1985

题目大意:有n个农场,这n个农场有一些边连着,然后要你找出两个点,使得这一对点的路径长度最大,输出这个最大的长度.输入的最后一个参数没用....


直接裸树的直径就可以了.之前都用DFS解决,现在是一下BFS.并且用了pair,建图方便多了,不用结构体,不用写构造函数,配合vector,简直完美...

AC代码1.DFS

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  *
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100000;
int n,m;
struct Edge
{
    int v,w;
    Edge(){}
    Edge(int v,int w):v(v),w(w){}
};
vector<Edge>G[maxn];

int dis[2][maxn];
bool vis[maxn];
int DIS;
int p;
void DFS(int root,int flag)
{
    vis[root]=true;
    if(dis[flag][root]>DIS)
    {
        DIS=dis[flag][root];
        p=root;
    }
    for(int i=0;i<G[root].size();i++)
    {
        int v=G[root][i].v;
        int w=G[root][i].w;
        if(!vis[v])
        {
            vis[v]=true;
            dis[flag][v]=dis[flag][root]+w;
            DFS(v,flag);
        }
    }
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n>>m)
    {
        int u,v,w;
        char ch[2];
        for(int i=0;i<=n;i++)
        {
            G[i].clear();
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%s",&u,&v,&w,ch);
            G[u].push_back(Edge(v,w));
            G[v].push_back(Edge(u,w));
        }

        memset(dis,0,sizeof(dis));
        memset(vis,false,sizeof(vis));
        DIS=0;
        DFS(1,0);
        int d1=p;
        memset(vis,false,sizeof(vis));
        DIS=0;
        DFS(d1,1);
        cout<<dis[1][p]<<endl;

    }
    return 0;
}

AC代码2:BFS.

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  *
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100000;
int n,m;
struct Edge
{
    int v,w;
    Edge(){}
    Edge(int v,int w):v(v),w(w){}
};
vector<Edge>G[maxn];

int dis[2][maxn];
bool vis[maxn];
int DIS;
int p;
int BFS(int root,int flag)
{
    vis[root]=true;
    DIS=0;
    int r=0;
    queue<int>q;
    q.push(root);
    while(!q.empty())
    {
        int p=q.front();
        q.pop();
        for(int i=0;i<G[p].size();i++)
        {
            int v=G[p][i].v;
            int w=G[p][i].w;
            if(!vis[v])
            {
                vis[v]=true;
                dis[flag][v]=dis[flag][p]+w;
                if(dis[flag][v]>DIS)
                {
                    DIS=dis[flag][v];
                    r=v;
                }
                q.push(v);
            }
        }
    }
    return r;
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n>>m)
    {
        int u,v,w;
        char ch[2];
        for(int i=0;i<=n;i++)
        {
            G[i].clear();
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d%s",&u,&v,&w,ch);
            G[u].push_back(Edge(v,w));
            G[v].push_back(Edge(u,w));
        }

        memset(dis,0,sizeof(dis));
        memset(vis,false,sizeof(vis));
        int r1=BFS(1,0);
        memset(vis,false,sizeof(vis));
        int r2=BFS(r1,1);
        cout<<dis[1][r2]<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值