poj1985 Cow Marathon(树的直径#入门)

poj1985 Cow Marathon(树的直径)


Time Limit: 2000MS		Memory Limit: 30000K
Total Submissions: 9110		Accepted: 4171
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

USACO 2004 February

思路

先搞清样例输入:
from,to,val,direction(from到to有一条长val的边,to在from的direction方向)
这里不用考虑方向,直接建双向边。
题意:求解两个牧场之间最长的距离。
即求树的直径即可。
树的直径求解步骤:

  1. 任意找一点作为起始点,找到离他最远的一点pos1,根据树直径的性质,此时pos1一定是树直径的一个端点。
  2. 把pos1当做起点,找离pos1最远的点pos2;
    pos1------pos2即为树的直径。

方法:

  1. 两遍dfs或者两遍bfs。
  2. 树形dp

AC代码:

  • 法1:两遍dfs(bfs略):
  • 时间复杂度O(n)
  • 优点:好记录路径
  • 缺点:写起来相对复杂一点点
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 1e5+5;
struct Edge
{
    int from;
    int to;
    int val;
    int nxt;
}edge[N];
int head[N<<1],idx;
void init()
{
    memset(head,-1,sizeof(head));
    idx = 0;
}
inline void add_edge(int from,int to,int val)
{
    edge[idx].from = from;
    edge[idx].to = to;
    edge[idx].val = val;
    edge[idx].nxt = head[from];
    head[from] = idx++;
}
bool vis[N];
int pos1,pos2,maxx;
void dfs(int s,int dis)
{
    if(dis > maxx)
    {
        pos1 = s;
        maxx = dis;
        //cout<<pos1<<endl;
    }
    for(int i = head[s]; ~i;i = edge[i].nxt)
    {
        int to = edge[i].to;
        int val = edge[i].val;
        if(!vis[to])
        {
            vis[to] = true;
            dfs(to,dis+val);
        }
    }
    return;
}
int main()
{

    int n,m;
    cin>>n>>m;
    int x,y,v;
    string d;
    init();
    while(m--)
    {
        cin>>x>>y>>v>>d;
        add_edge(x,y,v);
        add_edge(y,x,v);
    }
    memset(vis,false,sizeof(vis));
    maxx = 0;
    dfs(1,0);
    //cout<<pos1<<endl;
    pos2 = pos1;
    maxx = 0;
    memset(vis,false,sizeof(vis));
    dfs(pos1,0);
    //cout<<pos1<<endl;
    cout<<maxx<<endl;
    return 0;
}

简化代码:

#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 1e5+5;
struct Edge
{
    int from;
    int to;
    int val;
    int nxt;
} edge[N];
int head[N<<1],idx;
void init()
{
    memset(head,-1,sizeof(head));
    idx = 0;
}
inline void add_edge(int from,int to,int val)
{
    edge[idx].from = from;
    edge[idx].to = to;
    edge[idx].val = val;
    edge[idx].nxt = head[from];
    head[from] = idx++;
}
int pos1,pos2,maxx;
void dfs(int u,int fa,int dis)
{
    if(dis > maxx)
    {
        pos1 = u;
        maxx = dis;
    }
    for(int i = head[u]; ~i; i = edge[i].nxt)
    {
        int to = edge[i].to;
        int val = edge[i].val;
        if(to != fa)
        {
            dfs(to,u,dis+val);
        }
    }
}
int main()
{

    int n,m;
    cin>>n>>m;
    int x,y,v;
    string d;
    init();
    while(m--)
    {
        cin>>x>>y>>v>>d;
        add_edge(x,y,v);
        add_edge(y,x,v);
    }
    dfs(1,0,0);
    pos2 = pos1;
    dfs(pos1,0,0);
    cout<<maxx<<endl;
    return 0;
}
  • 法2:dp
  • 时间复杂度O(n)
  • 优点:写起来简单
  • 缺点:不好记录路径
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
const int N = 1e5+5;
struct Edge
{
    int from;
    int to;
    int val;
    int nxt;
} edge[N];
int head[N<<1],idx;
void init()
{
    memset(head,-1,sizeof(head));
    idx = 0;
}
inline void add_edge(int from,int to,int val)
{
    edge[idx].from = from;
    edge[idx].to = to;
    edge[idx].val = val;
    edge[idx].nxt = head[from];
    head[from] = idx++;
}
int maxx;
int dp[N];
void DP(int u,int fa)
{
     for(int i = head[u]; ~i; i = edge[i].nxt)
    {
        int to = edge[i].to;
        int val = edge[i].val;
        if(to != fa)
        {
            DP(to,u);
            maxx = max(maxx,dp[u]+dp[to]+val);
            dp[u] = max(dp[u],dp[to]+val);
        }
    }
    return;
}
int main()
{

    int n,m;
    cin>>n>>m;
    int x,y,v;
    string d;
    init();
    while(m--)
    {
        cin>>x>>y>>v>>d;
        add_edge(x,y,v);
        add_edge(y,x,v);
    }
    DP(1,0);
    cout<<maxx<<endl;
    return 0;
}
发布了301 篇原创文章 · 获赞 38 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tb_youth/article/details/104383957