bfs可以代替bellman-ford解决边数限制的最短路吗?

众所周知,bellman-ford可以解决边数限制的最短路问题也可以判断负环,但时间复杂度太高,判断负环也都开始使用spfa,而不是它,好像bfs也可以解决边数限制的最短路问题,就是在bfs中,我们用来判断是否走过这个点所用的数组,不再是一个单纯的只记录是否走过这个点,而是增加了一些特殊含义,变成了到这个点所用的边数,当有不同的点都可以在边数限制下到达这个点时,我们则取最小的那个点,但是呢,如果出现负权回路的话,bfs是不会转圈的,所以也就不能支持所有的边数限制的最短路问题,但可以支持不存在负权回路的最短路问题

边数限制的最短路问题

bfs code

(用num数组记录边数)


#include<bits/stdc++.h>
using namespace std;
const int N = 2e4 + 10;

int n, m, k;
int h[N], e[N], w[N], ne[N], idx;
int num[N], dist[N];

void add(int u, int v, int c)
{
    
    
    e[idx] = v;
    w[idx] = c;
    ne[idx] = h[u];
    h[u] = idx ++;
}

int main()
{
    
    
    cin >> n >> m >> k;
    memset(h, -1, sizeof(h));
    for(int i = 0; i < m; i ++)
    {
    
    
        int a, b, c;
        cin >> a >> b >> c;
        add(a, b, c);
    }
    queue<int> q;
    q.push(1);
    memset(dist, 0x3f, sizeof(dist));
    dist[1] = 0;
    num[1] = 1;
    while(q.size())
    {
    
    
        int t = q.front();
        q.pop();
        for(int i = h[t]; ~i; i = ne[i])
        {
    
    
            int j = e[i];
            if(!num[j])
            {
    
    
                num[j] = num[t] + 1;
                if(num[j] <= k + 1)
                dist[j] = dist[t] + w[i];
                if(num[j] < k + 1)
                {
    
    
                    q.push(j);
                }
            }
            else if(num[j] <= k + 1 && num[j] == num[t] + 1)
            {
    
    
                dist[j] = min(dist[j], dist[t] + w[i]);
            }
        }
    }
    if(dist[n] == 0x3f3f3f3f)
    {
    
    
        puts("impossible");
    }
    else
    printf("%d\n",dist[n]);
    return 0;
}

(不用num记录边数解决边数限制问题)
微博转发

#include<bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
int n, m, k;
bool st[N];
int h[N], e[N*100], ne[N*100], idx;

void add(int a, int b)
{
    
    
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx ++;
}

int bfs(int x)
{
    
    
    int res = 0;
    memset(st, 0, sizeof(st));
    queue<int> q;
    q.push(x);
    st[x] = 1;
    for(int i = 0; i < m; i ++)
    {
    
    
        int z = q.size();
        while(z --)
        {
    
    
            int t = q.front();
            q.pop();
            for(int i = h[t]; ~i; i = ne[i])
            {
    
    
                int j = e[i];
                if(!st[j])
                {
    
    
                    res ++;
                    st[j] = 1;
                    q.push(j);
                }
            }
        }
    }
    return res;
}

int main()
{
    
    
    cin >> n >> m;
    memset(h, -1, sizeof(h));
    for(int i = 1; i <= n; i ++)
    {
    
    
        int num;
        cin >> num;
        for(int j = 0; j < num; j ++)
        {
    
    
            int x;
            cin >> x;
            add(x, i);
        }
    }
    cin >> k;
    for(int i = 0; i < k; i ++)
    {
    
    
        int x;
        cin >> x;
        printf("%d\n", bfs(x));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_63092029/article/details/129779685