Cow Marathon

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. 

这道题与上一道题思路基本相同,上道题的链接:https://blog.csdn.net/liubang00001/article/details/81428683,虽然此题加入了方向,当时我也很纠结,但是在脑海中画完图之后也就发现了关键点,无论两点的方向如何,两点之间的距离是不会变得,所以我们只需要的是距离这一数据。走过该点后直接标记就不会再走该点了。

思路如下:

  1. 还是用vector与pair的关系来建立点与点之间的距离关系,由于方位点是无关紧要的,所以从开始我们可以设置一个数组存这一变量,它的任务也就结束了。
  2. 两次BFS一次用来求出距离1点距离最远的点x,另一次用来求距离x最远的点y然后再求出x与Y的距离就可以了。
  3. 常犯的毛病就是不清空队列,下次一定要注意。还有就是所有头文件都要写下来,能写就写,尽量不要用bits/stdc++。熟悉每个函数的用法。pair函数用make_pair()来出入数据,同时用first与second来代替pair里面的变量。

详细代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<vector>
#define ll long long
const int maxn=1001010;//数组尽量开的大些,不然可能会wa 
using namespace std;
int a,b,c,d,e,f,m,n,dis[maxn],point,ans[maxn];//dis用来存距离,point用来存距离最大的点,ans用来标记已被走过的点 
vector<pair<int,int> >ve[maxn];//用vector与pair的结合来记录点与点之间的距离关系 
queue<int>q;//队列用来存正在走的点 
int bfs(int e){
    memset(dis,0,sizeof(dis));
    memset(ans,0,sizeof(ans));
    while(!q.empty()) q.pop();//初始化数据,清空队列 
    q.push(e);//一般以数字1作为比较对象 
    ans[e]=1;
    point=0;
    while(!q.empty()){
        f=q.front();
        q.pop();
        if(dis[f]>d){
            d=dis[f];
            point=f;//记录在本次循环中距离最大的点 
        }
        pair<int ,int>it;//找出与点f相关连的点与二者相距的距离 
        for(int i=0;i<ve[f].size();i++){
            it=ve[f][i];
            if(ans[it.first]==0){
                ans[it.first]=1;//first表示相关连的点 
                dis[it.first]=it.second+dis[f];//记录从起始点到该点的距离之和 
                q.push(it.first);
            }
        }
    }
    return point;
}
int main(){
	char str[100];
	scanf("%d%d",&m,&n);
    for(int i=0;i<n;i++){
    	scanf("%d%d%d%s",&a,&b,&c,&str);//str为无关紧要的数据,可以不用 
        ve[a].push_back(make_pair(b,c));
        ve[b].push_back(make_pair(a,c));//建立双向关系 
    }
    d=0;
    point=bfs(1);
    d=0;
    bfs(point);
    printf("%d\n",d);
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺旺的碎冰冰~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值