H - Disjoint Set

https://vjudge.net/contest/279463#problem/H

#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#define ll long long
using namespace std;
struct str
{
    ll  t,n;
    ll w;
} e[1000010];
struct node
{
    ll  x;
    ll y;
    node(ll xx=0,ll yy=0)
    {
        x=xx;
        y=yy;
    }
    bool operator<(const node &s)const
    {
        if (y>s.y) return 1;
        else return 0;
    }
};
priority_queue<node> q;
ll a,b,tt,n,m,i,s,j,k,t,h[100100],v[100100],l,d[100100],ans,kk,x,y,z;
char c[100];
void add(ll u,ll v,ll w)
{
    tt++;
    e[tt].t=v;
    e[tt].w=w;
    e[tt].n=h[u];
    h[u]=tt;
}
void dij()
{
    for (i=1; i<=n; i++)
        d[i]=10000000000;
    d[1]=0;
    q.push(node(1,0));
    while (!q.empty())
    {
        int k=q.top().x;
        q.pop();
        if (v[k]==1) continue;
        v[k]=1;
        for (int i=h[k];i!=0;i=e[i].n)
        {
            if (d[e[i].t]>d[k]+e[i].w)
            {
                d[e[i].t]=d[k]+e[i].w;
                q.push(node(e[i].t,d[e[i].t]));
            }
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (i=2;i<=n;i++)
    for (j=1; j<i; j++){
        scanf("%s",c);
        l=0;
        if (c[0]!='x'){
            for (k=0;k<strlen(c);k++){
                kk=c[k]-48;
                l=l*10+kk;
            }
            add(i,j,l);
            add(j,i,l);
        }
    }
    dij();
    ans=0;
    for (i=1;i<=n;i++)
        ans=max(ans,d[i]);
    printf("%d\n",ans);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值