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=;
memset(head,-,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=;
int u;
memset(sum,,sizeof(sum));
memset(flag,,sizeof(flag));
queue<int>que;
que.push(fir);
flag[fir]=;
while(!que.empty())
{ u=que.front();
que.pop();
for(i = head[u] ; i != - ; 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]=;
que.push(st[i].to);
}
}
}
}
int main()
{
char d;
while(scanf("%d %d",&n,&m)!=EOF)
{ init();
for(i = ; i < m ; i++)
{
scanf("%d %d %d %c",&a,&b,&c,&d);
add_edge(a,b,c);
add_edge(b,a,c);
}
bfs();
bfs(node);
printf("%d\n",ans);
}
}

POJ 1985 Cow Marathon (求树的直径)的更多相关文章

  1. POJ 1985 Cow Marathon【树的直径】

    题目大意:给你一棵树,要你求树的直径的长度 思路:随便找个点bfs出最长的点,那个点一定是一条直径的起点,再从那个点BFS出最长点即可 以下研究了半天才敢交,1.这题的输入格式遵照poj1984,其实 ...

  2. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  3. POJ 1985 Cow Marathon(树的直径模板)

    http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

  4. poj:1985:Cow Marathon(求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case ...

  5. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  6. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

  7. Cow Marathon(树的直径)

    传送门 Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5362   Accepted: 2634 ...

  8. [POJ1985] Cow Marathon 「树的直径」

    >传送门< 题意:求树的直径 思路:就是道模板题,两遍dfs就求出来了 Code #include <cstdio> #include <iostream> #in ...

  9. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  10. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

随机推荐

  1. 关于long long int和__int64用%I64d和%lld输出在不同编译语言下的正确性

    http://blog.csdn.net/febr2/article/details/52068357 这个网址最下面

  2. AtCoder Regular Contest 074 F - Lotus Leaves

    题目传送门:https://arc074.contest.atcoder.jp/tasks/arc074_d 题目大意: 给定一个\(H×W\)的网格图,o是可以踩踏的点,.是不可踩踏的点. 现有一人 ...

  3. April Fools Contest 2017 D

    Description Input The only line of the input contains a string of digits. The length of the string i ...

  4. PHP使用curl函数实现多种请求(post,get)

    PHP使用curl函数实现get,post请求 一.CURL介绍 CURL是一个非常强大的开源库,支持很多协议,包括HTTP.FTP.TELNET等,我们使用它来发送HTTP请求.它给我 们带来的好处 ...

  5. 复习Java和前端、后端框架等。

    以下便是我开始复习时做的笔记.

  6. npm install error: MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”

    When I tried to run angular 4 material2 demo on my windows server 2012, got a error message: node-pr ...

  7. 根据 目录号 案卷号 用户名 查询 page 中 的条数

    select count(*) from am_b_page a join am_b_entry b on a.entry_id=b.entry_id where b.catalogue_code=' ...

  8. 深入理解spark streaming

    spark streaming是建立在spark core之上的,也就说spark streaming任务最终执行还是依赖于RDD模型.在转化成最终的RDD模型执行前,spark streaming主 ...

  9. ping localhost 返回 ::1的导致不能打开http://localhost的原因及解决

    虽然可以在浏览器中正常访问http://localhost但用file,file_get_contents等函数打开http://localhost异常.用127.0.0.1也可以打开,本地hosts ...

  10. .net 音频转换 .amr 转 .mp3 (ffmpeg转换法)

    最近看来是跟声音干上了啊! 音频转换的第二种方法,这种方法相对第一种来说,要简单的多! 首先,你得下载个“ffmpeg.exe” 插件,然后把它放到你的项目中,如下图: 程序中会调用该文件,以助于转换 ...