zoukankan      html  css  js  c++  java
  • 树的直径模板

    转载自:树的直径

    题目:

    Cow Marathon

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

    n个点,m条边,每条边给出两个端点和距离,和方向(即东西南北,本题中该信息无用) 求图中两点之间的最大距离

    挑战2代码:

    #include <stdio.h>
    #include <vector>
    #include <queue>
    using namespace std;
    const int maxn=1e5+20;
    const int inf=0x3f3f3f3f;
     
    struct edge{
        int t,w;
        edge(int tt=0,int ww=0):t(tt),w(ww){}
    };
     
    vector<edge> G[maxn];
    int n,m,d[maxn];
    //bool vis[maxn];
    //int cnt;
     
    void bfs(int s){
        for(int i=0;i<=n;++i) d[i]=inf;
        queue<int> q;
        q.push(s);
        d[s]=0;
        int u;
        while(!q.empty()){
            u=q.front(); q.pop();
            for(int i=0;i<G[u].size();++i){
                edge e=G[u][i];
                if(d[e.t]==inf){// or d[e.t]>d[u]+e.w 一样的 因为从树上一点出发不可能通过两个不同的点到达一个点 即无论如何只会更新一次,入队一次
                    d[e.t]=d[u]+e.w;
                    q.push(e.t);
                }
            }
        }
    }
     
    int solve(){
        bfs(1);//任选一个节点出发 如果下标是从1开始的不能bfs(0).....!!!
        int maxv=0;
        int maxi=0;
        for(int i=0;i<=n;++i){
            if(d[i]==inf) continue;
            if(maxv<d[i]){
                maxv=d[i];
                maxi=i;
            }
        }
        bfs(maxi);
        maxv=0;
        for(int i=0;i<=n;++i){
            if(d[i]==inf) continue;
            maxv=max(maxv,d[i]);
        }
        return maxv;
    }
     
    int main(){//3928K	1141MS
        int a,b,c;
        char s[3];
        while(scanf("%d%d",&n,&m)==2){
            for(int i=0;i<=n;++i) G[i].clear();
            while(m--){
                scanf("%d%d%d%s",&a,&b,&c,s);
                //cout<<a<<" "<<b<<" "<<c<<" "<<s<<endl;
                G[a].push_back(edge(b,c));
                G[b].push_back(edge(a,c));
            }
            printf("%d
    ",solve());
        }
        return 0;
    }
  • 相关阅读:
    bzoj 1025: [SCOI2009]游戏【数学+dp】
    bzoj 1195: [HNOI2006]最短母串【状压dp】
    洛谷 P1083 借教室【二分+差分/线段树】
    bzoj 2151: 种树【贪心+堆】
    bzoj 1055: [HAOI2008]玩具取名【区间dp】
    bzoj 2152: 聪聪可可【点分治】
    bzoj 4552: [Tjoi2016&Heoi2016]排序【二分+线段树】
    bzoj 1103: [POI2007]大都市meg【dfs序+树状数组】
    bzoj 3751: [NOIP2014]解方程【数学】
    bzoj 3612: [Heoi2014]平衡【整数划分dp】
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/10016637.html
Copyright © 2011-2022 走看看