I - Network Flow

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

#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
struct str
{
    int  t,w,n;
} e[100010];
int a,b,c,tt,kk,ans,n,m,x,i,j,k,t,h[10000],d[10000],v[10000],l;
void add(int u,int v,int w)
{
    tt++;
    e[tt].t=v;
    e[tt].w=w;
    e[tt].n=h[u];
    h[u]=tt;
}
void spfa(int xx)
{
    queue<int>q;
    q.push(xx);
    v[xx]=1;
    memset(d,0x3f3f3f,sizeof(d));
    d[xx]=0;
    while(!q.empty())
    {
        int t1=q.front();
        v[t1]=0;
        q.pop();
        for (int i=h[t1]; i!=0; i=e[i].n)
        {
            int t2=e[i].t,w1=e[i].w;
            if (d[t1]+w1<d[t2])
            {
                d[t2]=w1+d[t1];
                if (v[t2]==0)
                {
                    v[t2]=1;
                    q.push(t2);
                }
            }
        }
    }
}
int main()
{
    while (~scanf("%d%d",&m,&n))
    {
        memset(e,0,sizeof(e));
         memset(v,0,sizeof(v));
           memset(h,0,sizeof(h));
        for (i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(a,b,c);
            add(b,a,c);
        }
        spfa(1);
        printf("%d\n",d[n]);
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值