POJ2631 Roads in the North

题解:

树的最长路径,根据树的最长路径的性质,从任一一点bfs得到最远的点的位置,然后再从该点重新bfs一次就可以得到树的直径了

数学证明http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html

代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
#define pb push_back
#define mp make_pair
#define se second
#define fs first
#define ll long long
#define CLR(x) memset(x,0,sizeof x)
#define MC(x,y) memcpy(x,y,sizeof(x))
#define SZ(x) ((int)(x).size())
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define INF 2097152
typedef pair<int,int> P;
const double eps=1e-9;
const int maxn=10100;
const int mod=1e9+7;

struct Node{
    int v,w,nxt;
};

Node edges[maxn*2];
int head[maxn],dis[maxn],vis[maxn];
int goal,cnt,max_length;

void Addedge(int u,int v,int w){
    edges[cnt].v=v;
    edges[cnt].w=w;
    edges[cnt].nxt=head[u];
    head[u]=cnt++;
}

int bfs(int u){
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    queue<int> q;
    q.push(u);
    vis[u]=1;
    while(!q.empty()){
        int now=q.front();
        q.pop();
        for(int i=head[now];i!=-1;i=edges[i].nxt){
            int v=edges[i].v;
            if(!vis[v]){
                dis[v]=dis[now]+edges[i].w;
                q.push(v);
                vis[v]=1;
            }
            if(dis[v]>max_length){
                max_length=dis[v];
                goal=v;
            }
        }
    }
    return max_length;
}

int main(){
    memset(head,-1,sizeof(head));
    cnt=0;
    max_length=0;
    int u,v,w;
    while(~scanf("%d%d%d",&u,&v,&w)){
        Addedge(u,v,w);Addedge(v,u,w);
    }
    bfs(1);
    printf("%d\n",bfs(goal));
}
时间: 2024-10-18 10:26:23

POJ2631 Roads in the North的相关文章

poj2631 ?Roads in the North(求树的直径)

Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2941   Accepted: 1447 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such

UVa 10308 Roads in the North 树的直径

题目来源:UVa 10308 Roads in the North 题意:求距离最远的2点之间的距离 思路:裸的树的直径 或者树形DP #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn = 100010; struct node { int to, w; node(){} node(int to, int w): to(to), w(w) {

poj 2631 Roads in the North

题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to

POJ 2631 Roads in the North (求树的直径)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

题解报告:poj 2631 Roads in the North(最长链)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

Roads in the North (树的直径)

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. Give

UVA10308 Roads in the North 树的最长路径

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

UVA 10308 Roads in the North

一颗全连通且只有一条路从一个顶点到达另一个顶点,直接深搜,返回时返回最远的支路,且最远的支路加上第二远的支路和总路途最远比较,更新总路途最大,因为以一个点为中心走很多条路,最远的肯定是最大两条路的和,做法类似dp 输入有点坑,输完最后一组数据直接EOF,处理输入搞了好久,gets返回的是指针,遇到EOF返回NULL,遇空白行字符串第一个字符为NULL结束符,返回指针不为NULL 1 #include <iostream> 2 #include <stdio.h> 3 #includ

UVA - 10308 - Roads in the North (DFS)

题目传送:UVA - 10308 思路:就是树的遍历,DFS即可,注意输入 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <cmath> #include <queue> #include <s