AcWing 342. 道路与航线

题目链接

算法流程

在这里插入图片描述

样例

1.png


一些思考

dijkstra算法不能有负权边,所以更新时只能把正权边入堆。而遇到负权边时,也能更新dist数组,但不入堆。

按照拓扑序进行连通块内的dijkstra算法,保证了拓扑排列,在源点S所在的连通块后的所有连通块都能被更新, 所以算出的路径也还是最短的路径

时间复杂度

O(mlogn),m为边的数,n为顶点数

C++ 代码

#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;

typedef pair<int,int> PII;

const int N = 25010, M = 150010, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];

int n, mr, mp, s;
//id:点所在的连通块号, bcnt:连通块数量
int id[N], bcnt;
//连通块的入度数组
int deg[N];
vector<int> block[N];

int q[N], hh, tt = -1;

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

void dfs(int u)
{
    block[bcnt].push_back(u);
    id[u] = bcnt;
    
    for(int i = h[u]; i != -1; i = ne[i])
    {
        int j = e[i];
        if(!id[j]) dfs(j);
    }
}

void dijkstra(int block_id)
{
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    
    //取出该连通块所有的点, 入堆
    for(int u : block[block_id])
    {
        heap.push({dist[u], u});
    }
    
    while(heap.size())
    {
        PII t = heap.top();
        heap.pop();
        int u = t.second;
        
        if(st[u]) continue;
        st[u] = true;
        
        for(int i = h[u]; i != -1; i = ne[i])
        {
            int j = e[i];
            if(dist[j] > dist[u] + w[i])
            {
                dist[j] = dist[u] + w[i];
                if(id[j] == block_id) heap.push({dist[j], j});
            }
            
            if(id[j] != block_id && --deg[id[j]] == 0) q[++tt] = id[j];
        }
        
    }
}

void topoSort()
{
    //dist赋初值
    memset(dist, 0x3f, sizeof dist);
    dist[s] = 0;
    
    //入度为0的连通块入队列
    for(int i = 1; i <= bcnt; i++)
    {
        if(!deg[i]) q[++tt] = i;
    }
    
    while(hh <= tt)
    {
        int t = q[hh++];
        dijkstra(t);
    }
}

int main()
{
    scanf("%d%d%d%d", &n, &mr, &mp, &s);
    memset(h, -1, sizeof h);
    for(int i = 0; i < mr; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), add(b, a, c);
    }
    
    //建立所有的连通块
    for(int i = 1; i <= n; i++)
    {
        if(!id[i]) {
            ++bcnt;
            dfs(i);
        }
    }
    //在连通块之间建边
    for(int i = 0; i < mp; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c), deg[id[b]]++;
    }
    
    //算法开始
    topoSort();
    
    for(int i = 1; i <= n; i++)
    {
        if(dist[i] > INF / 2) puts("NO PATH");
        else printf("%d\n", dist[i]);
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值