zoukankan      html  css  js  c++  java
  • POJ 1985 Cow Marathon (求树的直径)

    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. 
     
    第一行输入n,m表示n个节点,m种关系,然后m行输入三个数a,b,c,表示a,b节点间的距离是c,求节点间最远的距离。S,N,W不影响结果,只在开始时输入就行。
    #include<cstdio>
    #include<queue>
    #include<string.h>
    #define M 100000
    using namespace std;
    int m,ans,flag[M],sum[M],n,a,b,c,i,head[M],num,node;
    struct stu
    {
        int from,to,val,next;
    }st[M];
    void init()
    {
        num=0;
        memset(head,-1,sizeof(head));
    }
    void add_edge(int u,int v,int w)
    {
        st[num].from=u;
        st[num].to=v;
        st[num].val=w;
        st[num].next=head[u];
        head[u]=num++;
    }
    void bfs(int fir)
    {    
        ans=0;
        int u;
        memset(sum,0,sizeof(sum));
        memset(flag,0,sizeof(flag));
        queue<int>que;
        que.push(fir);
        flag[fir]=1;
        while(!que.empty())
        {
        
            u=que.front();
            que.pop();
            for(i = head[u] ; i != -1 ; i=st[i].next)
            {
                if(!flag[st[i].to] && sum[st[i].to] < sum[u]+st[i].val)
                {
                    sum[st[i].to]=sum[u]+st[i].val;
                    if(ans < sum[st[i].to])
                    {
                        ans=sum[st[i].to];
                        node=st[i].to;
                    }
                    flag[st[i].to]=1;
                    que.push(st[i].to);
                }
            }
        }
    }
    int main()
    {
        char d;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            
            init();
            for(i = 0 ; i < m ; i++)
            {
                scanf("%d %d %d %c",&a,&b,&c,&d);
                add_edge(a,b,c);
                add_edge(b,a,c);
            }
            bfs(1);
            bfs(node);
            printf("%d
    ",ans);
        }
     } 
    ——将来的你会感谢现在努力的自己。
  • 相关阅读:
    Centos7 Crontab
    Centos7 php-fpm root 运行,执行 kill 等系统命令
    Centos7 安装系统服务、开机自启动
    CentOS7 安装Python3,开发SocketIO 客户端
    Centos7.6 安装DNS服务器
    exerunexplorer.exe
    Web GIS 离线地图
    DataGridView中添加CheckBox列用于选择行
    Android WebView Demo
    上海华魏光纤传感科技有限公司 招聘 《.NET研发工程师》
  • 原文地址:https://www.cnblogs.com/yexiaozi/p/5730083.html
Copyright © 2011-2022 走看看