day 4

题目Roads in the North

  1. 问题描述

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.

The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22

算法1解题思路
寻找最长的路径,对该数组进行vector和pair进行存储可以把三个数联系起来,然后运用bfs算法找出一点距离最长的另一点a,然后再次运用bfs寻找a点在该条路上的距离最长的路径,即为该条路得最长距离
算法1.源代码
#include
#include
#include
#include
#include
#include
using namespace std;
#define N 10010
vector< pair < int, int > > v[N]; //定义一个含pair的wector可以存放三个数

int vis[N];//标记走过的位置
int dis[N];//计算距离
int ans;//最大距离
int bfs(int x)
{
memset(vis,0,sizeof(vis));//初始化加清空功能
memset(dis,0,sizeof(dis));
int point=0;//距离最大的点
queue q;//建立一个队列
q.push(x);//把初始值拉入队列
vis[x]=1;//标记
while(!q.empty())
{
int f=q.front();
q.pop();//弹出第一个值
if(dis[f]>ans)
{
ans=dis[f];
point=f;//当距离大于之前的距离时进行距离的更换,点的存储
}
pair<int,int> t;//建立新的pair
for(int i=0;i<v[f].size();i++)
{
t=v[f][i];//把f对应的连接点以及距离复制给pair
if(vis[t.first]==0)//判断是否标记过
{
vis[t.first]=1;//标记
dis[t.first]=dis[f]+t.second;//距离累加
q.push(t.first);//加入队列
}
}
}
return point;//返回距离最大的点
}
int main()
{
int x,y,z;
while(cin>>x>>y>>z)
{
v[x].push_back(make_pair(y,z));
v[y].push_back(make_pair(x,z));//把点与点的连接以及距离进行存储
}
ans=0;
int point=bfs(1);//寻找距离1最远的点
ans=0;
bfs(point);//寻找树的直径
cout<<ans<<endl;//输出
return 0;
}

  1. 总结
    int bfs(int x)
    {
    memset(vis,0,sizeof(vis));
    memset(dis,0,sizeof(dis));
    int point=0;
    queue q;
    q.push(x);
    vis[x]=1;
    while(!q.empty())
    {
    int f=q.front();
    q.pop();
    if(dis[f]>ans)
    {
    ans=dis[f];
    point=f;
    }
    pair<int,int> t;
    for(int i=0;i<v[f].size();i++)
    {
    t=v[f][i];
    if(vis[t.first]==0)
    {
    vis[t.first]=1;
    dis[t.first]=dis[f]+t.second;
    q.push(t.first);
    }
    }
    }
    return point;
    }
    树的直径的模板
    V[x].push_back(make_pair(y,z));
    进行存储三个数据
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值