算法提高-图论-单源最短路的扩展应用

单源最短路的扩展应用

AcWing 1137. 选择最佳线路

多源点单终点最短路建图:

  1. 创建虚拟源点(创建虚拟源点的时候以是spfa为例 可以在建图的时候建出来,也可以在spfa这直接入队,也是虚拟源点的意思)
  2. 反向建图变成单源点多终点,然后遍历终点的dist即可找出最短路
#include <iostream>
#include <cstring>

using namespace std;

const int N = 1010, M = 20010, INF = 0x3f3f3f3f;

int q[N], dist[N];
bool st[N];
int e[M], h[N], ne[M], w[M], idx;
int n, m, T;

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

void spfa()
{
    int scnt;
    scanf("%d", &scnt);
    
     
    int hh = 0, tt = 0;
    while (scnt -- )//虚拟源点可以在建图的时候建出来,也可以在spfa这直接入队,也是虚拟源点的意思
    {
        int s;
        scanf("%d", &s);
        dist[s] = 0;
        q[tt ++ ] = s;
        st[s] = true;
    }
    
    while (hh != tt)//spfa循环队列,就是这样判断队列是否为空的
    {
        int t = q[hh ++ ];
        st[t] = false;
        if (hh == N) hh = 0;
        
        for (int i = h[t]; ~i; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                if (!st[j]) 
                {
                    q[tt ++ ] = j;
                    if (tt == N) tt = 0;
                    st[j] = true;
                }
            }
        }
    }
}




int main ()
{
    while (scanf("%d%d%d", &n, &m, &T) != -1)
    {
        memset(h, -1, sizeof h);//多组数据,每组都要初始化
        memset(dist, 0x3f, sizeof dist);
        idx = 0;
        //memset(st, 0, sizeof st); st不用初始化,出队的时候自动false,safa结束的时候st状态自动全是false
       
        
        while (m -- )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add(a, b, c);
        }
        spfa();
        
        if (dist[T] == INF) dist[T] = -1;
        cout << dist[T] << endl;
    }
    
    return 0;
}


AcWing 1131. 拯救大兵瑞恩

AcWing 1134. 最短路计数

这题挺简单的就不详细说了,主要是第一次遇到计数问题

#include <iostream>
#include <cstring>

using namespace std;

const int N = 1e5 + 10, M = 2 * 2 * 1e5 + 10, mod = 1e5 + 3;//无向边M要开成两倍,wa了好几发
int n, m;
int e[M], h[N], w[M], ne[M], idx;
int q[N], dist[N], cnt[N];

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

void bfs()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    cnt[1] = 1;
    
    int hh = 0, tt = 0;
    q[0] = 1;
    while (hh <= tt)
    {
        int t = q[hh ++ ];
        
        for (int i = h[t]; ~i; i = ne[i] )
        {
            int j = e[i];
            if (dist[j] > dist[t] + 1)
            {
                cnt[j] = cnt[t];
                dist[j] = dist[t] + 1;
                q[++ tt] = j; 
            }
            else if (dist[j] == dist[t] + 1)
            {
                cnt[j] = (cnt[t] + cnt[j]) % mod;
            }
        }
    }
}




int main()
{
    scanf("%d%d", &n, &m);
    memset(h, -1, sizeof h);

    while (m -- )
    {
        int a, b;
        scanf("%d%d", &a, &b);
        add(a, b), add(b, a);
    }

    bfs();

    for (int i = 1; i <= n; i ++ ) printf("%d\n", cnt[i]);

    return 0;
}

AcWing 383. 观光

在上一题的基础上拓展出来同时记录一个点到源点的次短路和最短路的条数

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

using namespace std;

const int N = 1e3 + 10, M = 1e4 + 10;//单向边,我们不用×2

int cases, n, m;
int S, T;

int h[N], w[M], ne[M], e[M], idx;
int st[N][2];
int cnt[N][2], dist[N][2];//0表示最短路,1表示次短路

struct Ver
{
    int id, type, dist;
    bool operator > (const Ver &t) const//小根堆虽然是最小的在上面,但是重载的是大于号,sort是重载小于号
    {                                   //而且小根堆是greater,就是告诉我们重载大于号的
        return dist > t.dist;
    }
};


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


int dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    memset(cnt, 0, sizeof cnt);
    memset(st, 0, sizeof st);
    dist[S][0] = 0;
    cnt[S][0] = 1;
    
    priority_queue<Ver, vector<Ver>, greater<Ver>> heap;
    heap.push({S, 0, dist[S][0]});
    
    while (heap.size())
    {
        Ver t = heap.top();
        heap.pop();
        int id = t.id, type = t.type, distance = t.dist, count = cnt[id][type];
        if (st[id][type]) continue;//放在while里面 for循环外面,dij的本质就是每个点都是贪心出来的最小的,只用这个点松弛一次其它边就行了
                                   //
        st[id][type] = true;
        
        
        for (int i = h[id]; ~i; i = ne[i])//找到当前点的领边,用当前点去松弛
        {
            int j = e[i];
            
            if (dist[j][0] > distance + w[i])//如果到j点的最短路可以更新,那么把次短路更新为之前的最短路,把最短路更新,两个都入堆
            {
                dist[j][1] = dist[j][0], cnt[j][1] = cnt[j][0];   
                dist[j][0] = distance + w[i], cnt[j][0] = count;
                heap.push({j, 1, dist[j][1]});
                heap.push({j, 0, dist[j][0]});
            }
            else if (dist[j][0] == distance + w[i])//找到一条到j点的新的最短路
                cnt[j][0] += count;
            else if (dist[j][1] > distance + w[i])//如果到j点的次短路可以更新,那么把次短路更新,并且入堆
            {
                dist[j][1] = distance + w[i], cnt[j][1] = count;
                
                heap.push({j, 1, dist[j][1]});
            }
            else if (dist[j][1] == distance + w[i])//如果找到一条到j点的新的最短路
                cnt[j][1] += count;
        }
    }
    
    int res = cnt[T][0];
    if (dist[T][0] + 1 == dist[T][1]) res += cnt[T][1];
    return res;
}



int main ()
{
    scanf("%d", &cases);
    
    while (cases -- )
    {
        scanf("%d%d", &n, &m);
        memset(h, -1, sizeof h);
        idx = 0;
        
        for (int i = 0; i < m; i ++ )
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            add (a, b, c);
        }
        scanf("%d%d", &S, &T);

        printf("%d\n", dijkstra());
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值