【无标题】

在这里插入图片描述
spfa 复杂度最好的情况是o(m),最差的时候是o(nm).
而且spfa对于图论问题无论正权还是负权都可以用,很全能(大多数情况,一般不会超复杂度的),形似bfs。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;
const int N = 1e5 + 10;

int n, m;
int h[N], w[N], ne[N], idx, e[N];
int dist[N];
int st[N];
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

int spfa()
{
    //初始化
    memset(dist, 0x3f, sizeof dist);
    //自己距离自己是1
    dist[1] = 0;
    //定义  
    queue<int> q;
    //将序号1放入
    q.push(1);
    //标记序号1已经入队
    st[1] = true;
    //循环队列
    while(q.size())
    {
        //取出队头
        auto t = q.front();
        //弹出对头
        q.pop();
        st[t] = false;
        //更新出边
        for(int i = h[t]; i != -1; i = ne[i])
        {
            
            int j = e[i];
            //寻找最近的出边
            if(dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                //如果已经在队列了,就跳过
                if(!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }
    if(dist[n] == 0x3f3f3f3f) return -1;
    return dist[n];
}

int main()
{
    cin >> n >> m;
    //构建邻接表
    memset(h, -1, sizeof h);
    while(m --)
    {
        int a, b, c;
        
        cin >> a >> b >> c;
        add(a, b, c);
    }
    int t = spfa();
    
    if(t == -1) puts("impossible");
    
    else
    cout << t << endl;
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值