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. 
有n个农田和m条路,以及每条路的方向(方向在这道题中没有用),求最长的一条路,也就是两点间的最大距离,即树的直径.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..M+1: Each line contains four space-separated entities, F1,

F2, L, and D that describe a road. F1 and F2 are numbers of

two farms connected by a road, L is its length, and D is a

character that is either 'N', 'E', 'S', or 'W' giving the

direction of the road from F1 to F2.

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

题目大意:从点A到点B的距离是C,有N个点,M条线,问两点之间最远的距离;
思路:
首先按要存图,一开开始是用的数组,一直RE后来才发现数的范围是1E5,二维数组直接就蹦了,所以要用Vector存图,输入的时候也要用c语言的常规输入输出,不然会TLE
#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
int n,m;//m个农场,6条路径
struct stu{
int a,b;//保存点2和点1 到点2点距离
}e; vector<stu >arr[];
int mark[];//标记数组
int ans=;
int xx; void dfs(int x,int step){
if(step>ans){
ans=step;
xx=x;
}
for(int i=;i<arr[x].size();i++){//arr[x]中的点肯定是与x相连接的点
if(mark[arr[x][i].a]==){
mark[arr[x][i].a]=;
dfs(arr[x][i].a,step+arr[x][i].b);
mark[arr[x][i].a]=;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
int x,y,z;
char s;
memset(arr,,sizeof(arr));
for(int i=;i<m;i++){
// scanf("%d%d%d %c\n",&x,&y,&z);
// cin>>x>>y>>z>>s;
scanf("%d %d %d",&x,&y,&z);
getchar(), getchar();
// getchar();
// getchar();getchar();
arr[x].push_back({y,z});
arr[y].push_back({x,z});
// arr[x][y]=z;
// arr[y][x]=z; } ans=;
memset(mark,,sizeof(mark));
mark[]=;
dfs(,);
memset(mark,,sizeof(mark));
mark[xx]=;
dfs(xx,);//两轮dfs直接输出
cout<<ans<<endl;
return ;
}

B - Cow Marathon DFS+vector存图的更多相关文章

  1. POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 7536   Accepted: 3559 Case ...

  2. How far away(DFS+vector存图)

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  3. 【模板】Vector存图 + SPFA

    最近愉快地决定要由边集数组转向Vector存图,顺便开始图论集训 老惯例,以题写模板 P1339 [USACO09OCT]热浪Heat Wave 题目描述 The good folks in Texa ...

  4. POJ 1655.Balancing Act-树的重心(DFS) 模板(vector存图)

    Balancing Act Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17497   Accepted: 7398 De ...

  5. Neither shaken nor stirred(DFS理解+vector存图)

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2013 题目理解: 给定n个点的有向图: 下面n行,第一个数字表示点权,后面一个数字m表示 ...

  6. Codeforce-1106-D. Lunar New Year and a Wander(DFS遍历+vector存图+set)

    Lunar New Year is approaching, and Bob decides to take a wander in a nearby park. The park can be re ...

  7. 存图方式---邻接表&邻接矩阵&前向星

    基于vector存图 struct Edge { int u, v, w; Edge(){} Edge(int u, int v, int w):u(u), v(v), w(w){} }; vecto ...

  8. Safe Path(bfs+一维数组存图)

    题目链接:http://codeforces.com/gym/101755/problem/H 题目分析:先bfs一遍怪兽可以到达的点,再bfs人可以走的地方看可不可以到达终点: 很显然读到  2&l ...

  9. Treasure Island DFS +存图

    All of us love treasures, right? That's why young Vasya is heading for a Treasure Island. Treasure I ...

随机推荐

  1. hdu2795billboard线段树

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2795/ 题目大意:有一块长方形木板,从上到下被分成h*w的区域,现要将n个长条放进这些区域中,要求从上到下只要后 ...

  2. 201771010111-李瑞红 实验一 软件工程准备-<构建之法-现代软件工程-基础认识和理解>

    |||||||| | :--

  3. Visual Studio2019+OpenCV3.4.9环境搭建

    让人头疼的vs2019+opencv环境配置 准备: visual studio2019: opencv 3.4.9: 耐心: 说明:vs2019属性管理器没有Microsoft.Cpp.x64.us ...

  4. [源码分析] 从FlatMap用法到Flink的内部实现

    [源码分析] 从FlatMap用法到Flink的内部实现 0x00 摘要 本文将从FlatMap概念和如何使用开始入手,深入到Flink是如何实现FlatMap.希望能让大家对这个概念有更深入的理解. ...

  5. mysql-8.0.19-winx64下载

    mysql-8.0.19-winx64 下载链接 提取码:m7qp

  6. Numpy和OpenCV中的图像几何变换

    介绍 上面的图像使它不言而喻什么是几何变换.它是一种应用广泛的图像处理技术.例如,在计算机图形学中有一个简单的用例,用于在较小或较大的屏幕上显示图形内容时简单地重新缩放图形内容. 它也可以应用于扭曲一 ...

  7. 五大经典卷积神经网络介绍:LeNet / AlexNet / GoogLeNet / VGGNet/ ResNet

    欢迎大家关注我们的网站和系列教程:http://www.tensorflownews.com/,学习更多的机器学习.深度学习的知识! LeNet / AlexNet / GoogLeNet / VGG ...

  8. 2020 | 可替代Selenium的测试框架Top15

    本文首发于 微信公众号: 软测小生 Selenium是一种开源自动测试工具.它可以跨不同的浏览器和平台在Web应用程序上执行功能,回归,负载测试.Slenium是最好的工具之一,但确实有一些缺点. 业 ...

  9. Windows 7系统记录

    http://www.winwin7.com/ win7带USB3.0和NVME驱动 http://www.mohuishou.com/ 其中的青苹果家园 支持UEFI+GPT机器 http://ww ...

  10. flask操作数据库 以及 建表

    创建迁移仓库 首先,安装Flask-Migrate: pip install flask-migrate 将app项目注册,便于使用orm操作 from flask_sqlalchemy import ...