Cow Marathon
Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 4216   Accepted: 2137
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

本体的大概意思就是求一个树上任意两个点之间的最大距离,本树是一个无向图树,方法并不唯一,
可以用dp解决,也可以用dfs解决,
存储边的时候一定要切记应该是无向图,两边都应该存进去
 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点。这样就可以首先任取一个点,bfs求出离其最远的点,
在用同样的方法求出离这个叶子节点最远的点,此时两点间的距离就是树的直径。
 
    #include<iostream>  
    #include<cstring>  
    #include<cstdio>  
    #include<algorithm>        
    using namespace std;  
    #define MAXN  500000        
    struct edge{  
        int to,dis,next;  
    }e[9999999];        
    int head[MAXN],en;  
    int dis[MAXN];        
    void add(int u,int v ,int w){  
        e[en].to=v;  
        e[en].dis=w;  
        e[en].next=head[u];  
        head[u]=en++;  
    }  
    void dfs(int f,int u,int d){  
        dis[u]=d;  
        for(int i=head[u];i!=-1;i=e[i].next)  
            if(e[i].to!=f)  
                dfs(u,e[i].to,d+e[i].dis);  
    }
    int u,v,c;  
    int n,m;  
    char s[20];  
    int main(){  
        while(~scanf("%d%d",&n,&m)){  
            memset(head,-1,sizeof(head));  
            en=0;  
            for(int i=0;i<m;i++){  
                scanf("%d%d%d%s",&u,&v,&c,s);  
                add(u,v,c);  
                add(v,u,c);  
            }  
            dfs(0,1,0);  
            int maxs,tag;  
            maxs=-1;tag=0;  
            for(int i=1;i<=n;i++){  
                if(maxs<dis[i])  
                {  
                    maxs=dis[i];  
                    tag=i;  
                }  
            }  
            dfs(0,tag,0);  
            maxs=-1;  
            for(int i=1;i<=n;i++)  
                maxs=max(maxs,dis[i]);  
            printf("%d\n",maxs);  
        }  
        return 0;  
    }  
下面的这个题是CCF认证的第四题,
该问题仍然求得是任意两点之间的最大距离,
但是该题并没有给出边的长度,
所以在套用模版的时候可以把边存储为1
另外该题有n+m,边,在进行循环的时候一定要注意该题的界限是m+n
 
姓名:
IJ����  
问题描述
试题编号: 201503-4
试题名称: 网络延时
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
   给定一个公司的网络,由n台交换机和m台终端电脑组成,交换机与交换机、交换机与电脑之间使用网络连接。交换机按层级设置,编号为1的交换机为根交换 机,层级为1。其他的交换机都连接到一台比自己上一层的交换机上,其层级为对应交换机的层级加1。所有的终端电脑都直接连接到交换机上。
  当信息在电脑、交换机之间传递时,每一步只能通过自己传递到自己所连接的另一台电脑或交换机。请问,电脑与电脑之间传递消息、或者电脑与交换机之间传递消息、或者交换机与交换机之间传递消息最多需要多少步。
输入格式
  输入的第一行包含两个整数n, m,分别表示交换机的台数和终端电脑的台数。
  第二行包含n - 1个整数,分别表示第2、3、……、n台交换机所连接的比自己上一层的交换机的编号。第i台交换机所连接的上一层的交换机编号一定比自己的编号小。
  第三行包含m个整数,分别表示第1、2、……、m台终端电脑所连接的交换机的编号。
输出格式
  输出一个整数,表示消息传递最多需要的步数。
样例输入
4 2
1 1 3
2 1
样例输出
4
样例说明
  样例的网络连接模式如下,其中圆圈表示交换机,方框表示电脑:

  其中电脑1与交换机4之间的消息传递花费的时间最长,为4个单位时间。
样例输入
4 4
1 2 2
3 4 4 4
样例输出
4
样例说明
  样例的网络连接模式如下:

  其中电脑1与电脑4之间的消息传递花费的时间最长,为4个单位时间。
评测用例规模与约定
  前30%的评测用例满足:n ≤ 5, m ≤ 5。
  前50%的评测用例满足:n ≤ 20, m ≤ 20。
  前70%的评测用例满足:n ≤ 100, m ≤ 100。
  所有评测用例都满足:1 ≤ n ≤ 10000,1 ≤ m ≤ 10000。
答题栏
试题编号: 201503-4
试题名称: 网络延时
 

最新文章

  1. [Hadoop in Action] 第4章 编写MapReduce基础程序
  2. web工具网站等
  3. 在SpringMVC中获取request对象
  4. mac OS X操作 -- 常用
  5. 初识python中的类与对象
  6. VC++ ADO相关
  7. 可选择Email和用户名登录的代码
  8. Unity给力插件之Final IK
  9. POJ 3614 Sunscreen 优先队列 贪心
  10. bigdata_批量机器执行通用脚本
  11. ie7,IE8不支持document.getElmentsByClassName的问题
  12. mongodb 面试题总结
  13. 【GIT】git 删除本地分支和远程分支、本地代码回滚和远程代码库回滚
  14. scala用ssh2连接Linux
  15. momentjs的使用
  16. asp.net EF框架执行原生SQL语句
  17. 彻底卸载tv
  18. HTTP请求/响应报文结构
  19. 1.3.2、CDH 搭建Hadoop在安装之前(端口---Cloudera Navigator加密使用的端口)
  20. 【C#】解析C#程序集的加载和反射

热门文章

  1. UWP开发:自动生成迷宫&amp;自动寻路算法(2)
  2. SAP Cloud for Customer的Account Team里的role如何配置
  3. halt, reboot, poweroff - 中止系统运行
  4. JS实现全排列
  5. 厌食?暴食?试试这个 VR 新疗法
  6. js判断是否为app
  7. Java(面试题):字符串截取
  8. NIOP 膜你题
  9. 用C#(ASP.Net)在Exchange Server环境下发送邮件
  10. TreeMap 底层是红黑树 排序是根据key值进行的 添加元素时异常 Comparable异常 Comparator比较自定义对象放在键的位置