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. 

Source

题意:求树上最长路。
容易发现:求一个连通块中的最长路径,首先,从该连通块中任意一个结点出发,求最长路径的端点是S,然后再从S出发求最长路径L,路径L就是要求的路径。
代码:
 #include"bits/stdc++.h"

 #define db double
#define ll long long
#define vl vector<ll>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define rep(i, a, n) for (int i=a;i<n;i++)
#define per(i, a, n) for (int i=n-1;i>=a;i--)
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
const int N = 1e6 + ;
const int mod = 1e9 + ;
const int MOD = ;
const db PI = acos(-1.0);
const db eps = 1e-;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3fffffffffffffff;
int t, n, m;
bool vis[N];
char ss[];
vector<pii> e[N];
queue<int> q;
void add(int u,int v,int w){
e[u].push_back(pii(v,w));
}
int ma;
int f[N];
int s;
void BFS(int x)//BFS求最长路
{
memset(f,, sizeof(f));
memset(vis,, sizeof(vis));
while(!q.empty()) q.pop();
ma=;
q.push(x);
vis[x]=;
s=x;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=;i<e[u].size();i++){
int v=e[u][i].fi;
int w=e[u][i].se;
if(!vis[v]){
if(f[v]<f[u]+w) f[v]=f[u]+w;
vis[v]=;
q.push(v);
}
if(ma<f[v]) ma=f[v],s=v;
}
}
}
int main() { while (scanf("%d%d", &n, &m) == ) {
for(int i=;i<=n;i++) e[i].clear();
for (int i = ; i < m; i++) {
int x, y, z;
scanf("%d %d %d %s", &x, &y, &z, ss);
add(x,y,z),add(y,x,z);
}
BFS();
BFS(s);
pi(ma);
}
return ;
}

POJ1985 树的直径(BFS的更多相关文章

  1. poj2631 树的直径 + bfs

    //Accepted 492 KB 0 ms //树的直径 bfs #include <cstdio> #include <cstring> #include <iost ...

  2. hdu2196 树的直径 + bfs

    //Accepted 740 KB 15 ms //树的直径 //距离一个顶点最远的点一定是树的直径的一个端点 #include <cstdio> #include <cstring ...

  3. 树上选两点(使最短)树的直径+bfs

    题意: 给你一颗树,让你放两个点,放在哪里的时候任意点到某个最近的消防站最远值最小. 思路: 树的直径类题目. 首先我们想两个点会把整棵树分成两个团,所以肯定会在树的某个链上切开. 而且要切一定切在树 ...

  4. ZOJ 3820 Building Fire Stations 求中点+树的直径+BFS

    题意:给一棵树,要求找出两个点,使得所有点到这两个点中距离与自己较近的一个点的距离的最大值(所有点的结果取最大的值,即最远距离)最小. 意思应该都能明白. 解法:考虑将这棵树摆直如下: 那么我们可以把 ...

  5. luogu P3761 [TJOI2017]城市 树的直径 bfs

    LINK:城市 谢邀,学弟说的一道毒瘤题. 没有真正的省选题目毒瘤 或者说 写O(n)的做法确实毒瘤. 这里给一个花20min就写完的非常好写的暴力. 容易想到枚举哪条边删掉 删掉之后考虑在哪两个点上 ...

  6. 树的直径(BFS)

    ][];];];];,,;vis[i]=; ; j <= n ; j++){ ){;//标记 res[j]=res[root]+; ; i <= n- ; i++){; data[b][a ...

  7. [USACO2004][poj1985]Cow Marathon(2次bfs求树的直径)

    http://poj.org/problem?id=1985 题意:就是给你一颗树,求树的直径(即问哪两点之间的距离最长) 分析: 1.树形dp:只要考虑根节点和子节点的关系就可以了 2.两次bfs: ...

  8. poj1985 / poj2631(树的直径)

    poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...

  9. poj1985 Cow Marathon (求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3195   Accepted: 1596 Case ...

随机推荐

  1. HCNA多区域OSPF配置

    1.拓扑图 2.各路由器配置角本 ospf 多区域配置 #R5配置 sys sysname AR5 interface s2// ip add 10.0.35.5 255.255.255.0 inte ...

  2. startup ORA-00845: MEMORY_TARGET not supported on this system

    一台虚拟机跑多个实例时,由于/dev/shm空间不够导致如下报错> startupORA-00845: MEMORY_TARGET not supported on this system解决方 ...

  3. A full JDK must be specified

    当你开发就了,你就发现,你遇到了各种奇葩的问题,结果,自己奇葩了. 背景:由于项目需要做安全恢复测试,然后,就搭一个新的库环境去测试: 配置jboss的运行jdk时,结果,蹦出个:A full JDK ...

  4. Orchard Core 使用工作流处理审批和创建内容项

    译自:http://www.ideliverable.com/blog/orchard-core-workflows-walkthrough-content-approval 转载请注明出处, 原文地 ...

  5. Docker镜像提交命令commit的工作原理和使用方法

    在本地创建一个容器后,可以依据这个容器创建本地镜像,并可把这个镜像推送到Docker hub中,以便在网络上下载使用. 下面我们来动手实践. docker pull nginx:1.15.3 用命令行 ...

  6. Uva 12298 超级扑克2

    题目链接:https://vjudge.net/problem/UVA-12298 题意: 1.超级扑克,每种花色有无数张牌,但是,这些牌都是合数:比如黑桃:4,6,8,9,10,,,, 2.现在拿走 ...

  7. idea 一次性自动导包

    当复制粘贴一段纯文本代码时,许多类需要导包. 如图 使用optimize imports了,发还是没有办法导入未导入的包,只能删除未使用导包,这跟eclipse不一样,让人很不习惯,查了好多资料都没有 ...

  8. while、dowhile、switchcase 循环嵌套、穷举、迭代

    for(var i=0;i<5;i++)//假如规定到5 { document.write("123456<br/>") } 如果变成这样 var i=0: fo ...

  9. GOPL第三章练习题3.3 3.4代码

    练习3.3是peak展示为红色,valley展示为蓝色. 练习3.4是将svg图像打印到浏览器中. // Copyright © 2016 Alan A. A. Donovan & Brian ...

  10. js中的AJAX

    AJAX:Asynchronous JavaScript and XML.意思就是用JavaScript执行异步网络请求. 如果仔细观察一个Form的提交,你就会发现,一旦用户点击Submit按钮,表 ...