POJ 1985 Cow Maratho【樹的直徑】

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;
}



發佈了374 篇原創文章 · 獲贊 211 · 訪問量 70萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章