P1339

最短路板子题。。

写个spfa。。。嘿嘿嘿‘’

 

 

以下是AC代码

 

 

 

 

#include<bits/stdc++.h>
using namespace std;
inline int read()
{
    int X=0,w=0; char ch=0;
    while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
    while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
#define pb push_back
const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;
int tot,head[maxn];
struct node
{
    int t, v, nxt;
}ed[maxn];
void add(int a,int b,int v)
{
    ed[++tot].t = b;
    ed[tot].v = v;
    ed[tot].nxt = head[a];
    head[a] = tot;
}
bool vis[maxn];
int dis[maxn];
int t,c,ts,te;
void spfa()
{
    queue<int>q;
    vis[ts] = true;
    dis[ts] = 0;
    q.push(ts);
    while(q.size())
    {
        int u=q.front();q.pop();
        vis[u] = false;
        for(int i=head[u];~i;i=ed[i].nxt)
        {
            int t = ed[i].t;
            int v = ed[i].v;
            if(dis[t] >= dis[u]+v)
            {
                dis[t] = dis[u] + v;
                if(!vis[t])
                {
                    q.push(t);
                    vis[t] = true;
                }
            }
        }
    }
}
int main()
{
    t = read(); c = read();
    ts = read(); te =read();
    memset(vis, false, sizeof vis);
    memset(dis, INF, sizeof dis);
    memset(head, -1, sizeof head);
    tot = 0;
    for(int i=1;i<=c;i++)
    {
        int a=read(), b=read(), c=read();
        add(a,b,c);
        add(b,a,c);
    }
    spfa();
    printf("%d\n",dis[te]);
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值