G - Roads in the North

https://vjudge.net/contest/279738#problem/G

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct str
{
    int t,w,n;
}e[1000000];
int a,b,c,t,i,j,k,l,n,ans,m,max1,h[200010],v[100010],d[200010];
void add(int u,int v,int w)
{
    t++;
    e[t].t=v;
    e[t].w=w;
    e[t].n=h[u];
    h[u]=t;
}
void bfs(int x)
{
    queue<int>q;
    q.push(x);
    max1=0;
    memset(v,0,sizeof(v));
    memset(d,0x3f3f3f3f,sizeof(d));
    d[x]=0;
    ans=0;
    while (q.size())
    {
        int t1=q.front();
        q.pop();
        if (v[t1]==1) continue;
        v[t1]=1;
        for (int i=h[t1]; i; i=e[i].n)
        {
            int t2=e[i].t;
            if (d[t2]>d[t1]+e[i].w)
            {
                d[t2]=d[t1]+e[i].w;
                q.push(t2);
                if (d[t2]>max1)
                {
                    max1=d[t2];
                    ans=t2;
                }
            }
        }
    }
}
int main()
{
    while (~scanf("%d%d%d",&a,&b,&c))
    {
        add(a,b,c);
        add(b,a,c);
    }
    bfs(1);
    bfs(ans);
    printf("%d\n",max1);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值