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

 
【思路】
求树的直径。树上最远两点距离。两遍dfs。bfs也可。
W S E N 并没有什么卵用‘
【code】
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,x,y,z,head[],dad[],dis[],maxx,maxn,sumedge;
struct Edge
{
int x,y,z,nxt;
Edge(int x=,int y=,int z=,int nxt=):
x(x),y(y),z(z),nxt(nxt){}
}edge[];
void add(int x,int y,int z)
{
edge[++sumedge]=Edge(x,y,z,head[x]);
head[x]=sumedge;
}
void dfs(int x)
{
for(int i=head[x];i;i=edge[i].nxt)
{
int v=edge[i].y;
if(dad[x]!=v)
{
dad[v]=x;
dis[v]=dis[x]+edge[i].z;
dfs(v);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
char s[];
cin>>x>>y>>z>>s[];
add(x,y,z);
add(y,x,z);
}
dfs();
maxx=-0x7fffff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
maxn=i;
}
}
memset(dis,,sizeof(dis));
memset(dad,,sizeof(dad));
dfs(maxn);
maxx=-0x7fff;
for(int i=;i<=n;i++)
{
if(dis[i]>maxx)
{
maxx=dis[i];
}
}
printf("%d\n",maxx);
return ;
}

最新文章

  1. h5移动端常见问题
  2. Java使用velocity导出word
  3. fir.im Weekly - Mobile developer 利器分享
  4. Eclipse中使用tomcat 8服务器初级教程
  5. 傅里叶变换:MP3、JPEG和Siri背后的数学
  6. 首次push本地代码到github上出现的问题及解决方案
  7. PAT-乙级-1009. 说反话 (20)
  8. 文件过滤驱动实现目录重定向(一)good
  9. 日期函数(SqlServer)
  10. Chrome 小工具: 启动本地应用 (Native messaging)
  11. mvc Controller类介绍
  12. Objective-c 多线程操作 自定义NSOperation 模拟下载
  13. RxJava系列6(从微观角度解读RxJava源码)
  14. LeetCode编程训练 - 拓扑排序(Topological Sort)
  15. 微信小程序+java后台
  16. Linux 环境下安装RabbitMQ的步骤
  17. 使用netty HashedWheelTimer构建简单延迟队列
  18. 原型设计工具—Axure
  19. nginx基础知识总结
  20. HDU 2043 密码

热门文章

  1. codechef Tree and Queries Solved
  2. 模板题 Problem I Link Cut Tree
  3. sql中Cast()函数的用法
  4. 【postman】安装使用说明
  5. 基于MNIST数据的卷积神经网络CNN
  6. aSmack连接server异常smack.SmackException$ ConnectionException thrown by XMPPConnection.connect();
  7. 【机器学习算法-python实现】协同过滤(cf)的三种方法实现
  8. Android用户界面设计:基本button
  9. bzoj 1088 简单dfs
  10. appium第一个安卓自动化工程