文章

2

粉丝

0

获赞

7

访问

1.2k

头像
最短路径条数统计—SPFA算法:
P5380 复旦大学2024年机试题
发布于 2026年3月
阅读数 607

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

using namespace std;

const int N = 1010, M = 10010, mode = 1e9 + 7;

int n, m, s, t;
int h[N], e[M], ne[M], w[M], idx;
bool st[N];
int dist[N], cnt[N], path[N];                                                       //dist统计最短路长度,cnt统计最短路数量,path统计最短路来自于哪个节点

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

void spfa(int s)
{
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0;
    cnt[s] = 1;
    queue<int> q;
    q.push(s);
    st[s] = true;
    
    while(q.size())
    {
        auto t ...

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发