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实验作业

    实验九 异常.断言与日志 实验时间 2018-10-25 1.实验目的与要求 (1) 掌握java异常处理技术: Java的异常处理机制可以控制程序从错误产生的位置转移到能够进行错误处理的位置. Ja ...

  2. Effective python(五):内置模块

    1,考虑使用contextlib和with语句改写可复用的try/finally代码 with lock:print('lock is held')相当于try:print('lock is held ...

  3. 多线程之旅(Thread)

    在上篇文章中我们已经知道了多线程是什么了,那么它到底可以干嘛呢?这里特别声明一个前面的委托没看的同学可以到上上上篇博文查看,因为多线程要经常使用到委托.源码 一.异步.同步 1.同步(在计算的理解总是 ...

  4. CF1326A Bad Ugly Numbers 题解

    原题链接 简要题意: 构造一个长为 \(n\) 的数,使得每位均不为 \(0\),且 \(n\) 不被它的各位数字整除. 比方说, \(n = 239\) 是合法的.因为: \(2 \not | 23 ...

  5. 洛谷P1003 铺地毯 模拟

    这一题就是一个很普通的模拟,每次输入的时候存储四个角的值 把四个角的横纵坐标存储在一排.然后在倒序遍历一遍,查找的时候就看所要查找的坐标在不在这个范围内,如果找到了就标记一下再输出,如果没有找到就输出 ...

  6. SVM支持向量机——核函数、软间隔

    支持向量机的目的是寻找一个能讲两类样本正确分类的超平面,很多时候这些样本并不是线性分布的. 由此,可以将原始特征空间映射到更高维的特征空间,使其线性可分.而且,如果原始空间是有限维,即属性数量有限, ...

  7. Redis 缓存更新一致性

    当执行写操作后,需要保证从缓存读取到的数据与数据库中持久化的数据是一致的,因此需要对缓存进行更新. 因为涉及到数据库和缓存两步操作,难以保证更新的原子性. 在设计更新策略时,我们需要考虑多个方面的问题 ...

  8. TCP、UDP服务器模型 在网络程序里面,通常都是一

    TCP.UDP服务器模型 在网络程序里面,通常都是一个服务器处理多个客户机,为了出个多个客户机的请求,服务器端的程序有不同的处理方式. 目前最常用的服务器模型: 循环服务器:循环服务器在同一时刻只能响 ...

  9. 字节转换函数 htonl*的由来与函数定义...

    字节转换字符由来: 在网络上面有着许多类型的机器,这些机器在表示数据的字节顺序是不同的, 比如i386芯片是低字节在内存地址的低端, intel处理器将32位的整数分4个连续的字节,并以字节序1-2- ...

  10. SublimeのJedi (自动补全)

    关于 Sublime 3 - Jedi Package 的设置和使用方法 我是一枚小白,安装后 Sublime 后,想在码字时,达到如下效果: 打字时,自动提示相关内容 按Tab键,相关内容自动填充 ...