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. Java日期处理易踩的十个坑

    前言 整理了Java日期处理的十个坑,希望对大家有帮助. 一.用Calendar设置时间的坑 反例: Calendar c = Calendar.getInstance(); c.set(Calend ...

  2. PAT-B 1003. 我要通过!(20) Java版

    "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&quo ...

  3. 两个看似相同,结果不同的SQL逻辑

    一朋友问我的,看着挺有意思,记录一下: 表item1,里面有上面三个字段,一共10条数据,接下来有两个相似的sql语句得到了不一样的查询结果. 表数据如下:  两条SQL及查询结果: 我的理解答: S ...

  4. 使用PyTorch进行迁移学习

    概述 迁移学习可以改变你建立机器学习和深度学习模型的方式 了解如何使用PyTorch进行迁移学习,以及如何将其与使用预训练的模型联系起来 我们将使用真实世界的数据集,并比较使用卷积神经网络(CNNs) ...

  5. javascript创建函数的方法

    函数对任何语言来说都是一个核心的概念.函数,是一种封装(将一些语句,封装到函数里面). 通过函数可以封装任意多条语句,而且可以在任何地方.任何时候调用执行. ECMAScript中的函数使用funct ...

  6. JVM中垃圾回收机制如何判断是否死亡?详解引用计数法和可达性分析 !

    因为热爱,所以坚持. 文章下方有本文参考电子书和视频的下载地址哦~ 这节我们主要讲垃圾收集的一些基本概念,先了解垃圾收集是什么.然后触发条件是什么.最后虚拟机如何判断对象是否死亡. 一.前言   我们 ...

  7. Spring Boot 整合视图层技术,application全局配置文件

    目录 Spring Boot 整合视图层技术 Spring Boot 整合jsp Spring Boot 整合freemarker Spring Boot 整合视图层技术 Spring Boot 整合 ...

  8. A 组队参赛

    时间限制 : - MS   空间限制 : - KB  评测说明 : 1s,256m 问题描述 一年一度的ioiAKer大赛即将来临,何老板打算让信竞队的同学们组队参赛.信竞队共n名队员,他们的CF积分 ...

  9. 微服务架构盛行的时代,你需要了解点 Spring Boot

    随着互联网的高速发展,庞大的用户群体和快速的需求变化已经成为了传统架构的痛点. 在这种情况下,如何从系统架构的角度出发,构建出灵活.易扩展的系统来快速响应需求的变化,同时,随着用户量的增加,如何保证系 ...

  10. 直播回顾 | IOT、AI、云计算等融合技术推进制造业产业转型(二)

    3月31日,BoCloud博云.京东智联云.海尔集团联手,以“制造”到“智造”为主题,进行了IT赋能企业数字化转型实践分享. 博云售前解决方案架构师尹贺杰,京东云与AI企业云业务部高级业务技术经理吴世 ...