Bellman-Ford算法 邻接表&邻接矩阵实现

邻接表

#include <cstdio>
#include <vector>
using namespace std;
#define INF 0x7FFFFFFF
typedef struct
{    int w, adj;    }Arcnode;
vector<vector<Arcnode>> G;
vector<int > Path, nPath, Dist;
bool check( int N )
{
    bool flag = false;
    for( int i = 0; i < N; ++i )
        for( int j = 0; j < G[i].size(); ++j )
            if( Dist[i] != INF && Dist[ G[i][j].adj ] > Dist[i] + G[i][j].w )
            {
                flag = true;
                Dist[ G[i][j].adj ] = Dist[i] + G[i][j].w;
                Path[ G[i][j].adj ] = i;
                nPath[ G[i][j].adj ] = nPath[i];
            }
            else if( Dist[i] != INF && Dist[ G[i][j].adj ] == Dist[i] + G[i][j].w  && i != Path[ G[i][j].adj ] )
                nPath[ G[i][j].adj ] += nPath[i];
    return flag;
}
bool BF( int N )
{
    for( int i = 0; i < N - 1; ++i )
        if( !check(N) )
            return true;
    return !check(N);
}
int main()
{
    int N, M, V1, V2, a, b, w;
    Arcnode temp;
    scanf("%d %d %d %d", &N, &M, &V1, &V2);
    Path.resize(N);
    Dist.resize(N);
    nPath.resize(N);
    G.resize(N);
    fill( Dist.begin(), Dist.end(), INF );
    fill( Path.begin(), Path.end(), -1 );
    Dist[V1] = 0;
    nPath[V1] = 1;
    for( int i = 0; i < M; ++i )
    {
        scanf("%d %d %d", &a, &temp.adj, &temp.w);
        G[a].push_back( temp );
    }
    if( BF( N ) )
    {
        for( int i = 0; i < N; ++i )
            printf("%d%s", Dist[i], i == N - 1 ? "\n":" ");
        for( int i = V2; i != -1; i = Path[i] )
            printf("%d ", i);
        printf("\n%d", nPath[V2]);
    }
}

邻接矩阵

#include <cstdio>
#include <algorithm>
using namespace std;
#define INF 0x7FFFFFFF
int G[500][500], Dist[500], Path[500], nPath[500];
bool check( int N )
{
    bool flag = false;
    for( int i = 0; i < N; ++i )
        for( int j = 0; j < N; ++j )
            if( G[i][j] != INF && Dist[i] != INF && Dist[j] >= Dist[i] + G[i][j] )
            {
                if( Dist[j] > Dist[i] + G[i][j] )
                {
                    flag = true;
                    Dist[j] = Dist[i] + G[i][j];
                    Path[j] = i;
                    nPath[j] = nPath[i];
                }
                else if( i != Path[j] )
                    nPath[j] += nPath[i];
            }
    return flag;
}
bool BF( int N )
{
    for( int i = 0; i < N - 1; ++i )
        if( !check(N) )
            return true;
    return !check(N);
}
int main()
{
    int N, M, V1, V2, a, b, w;
    scanf("%d %d %d %d", &N, &M, &V1, &V2);
    fill( G[0], G[0] + 500 * 500, INF );
    fill( Dist, Dist + N, INF );
    fill( Path, Path + N, -1 );
    nPath[V1] = 1;
    Dist[V1] = 0;
    for( int i = 0; i < M; ++i )
    {
        scanf("%d %d %d", &a, &b, &w);
        G[a][b] = w;
    }
    if( BF( N ) )
    {
        for( int i = 0; i < N; ++i )
            printf("%d%s", Dist[i], i == N - 1 ? "\n":" ");
        for( int i = V2; i != -1; i = Path[i] )
            printf("%d ", i);
        printf("\n%d", nPath[V2]);
    }
    else    printf("FALSE");
}

测试用例:给点节点数与路径数, 给定任意两存在节点,输出最短距离,及最短路径
6 8 0 2
0 1 1
0 3 4
0 4 4
1 3 2
2 5 1
3 2 2
3 4 3
4 5 3
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值