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;
    }
  • 相关阅读:
    关于生成二维码的相关参考资料
    C#生成二维码的方法
    .NET 二维码生成(ThoughtWorks.QRCode)
    微信扫描二维码登录网站技术原理
    C# ArrayList的用法
    C#多线程编程
    c#使用多线程的几种方式示例详解
    解决Winform应用程序中窗体背景闪烁的问题
    C# 线程调用主线程中的控件
    30、网络编程
  • 原文地址:https://www.cnblogs.com/lu1nacy/p/10016637.html
Copyright © 2011-2022 走看看