• 算法提高-图论-单源最短路的综合应用


    单源最短路的综合应用

    AcWing 1135. 新年好

    多次dijkstra求每个点到其它点的最短距离, 此时相当于建好了一张图,每个点之间的最短距离都知道了,接下来dfs搜一下怎么走最短即可

    #include 
    #include 
    #include 
    
    using namespace std;
    
    typedef pair<int, int> PII;
    #define x first
    #define y second
    
    const int N = 5 * 1e4 + 10, M = 2 * 1e5 + 10, INF = 0x3f3f3f3f;
    
    int dist[6][N];
    int e[M], ne[M], w[M], h[N], idx;
    int source[6];
    bool st[N];
     int n, m;
    
    void add (int a, int b, int c)
    {
        e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ; 
    }
    
    
    
    
    void dijkstra(int start, int dist[])
    {
        memset(st, 0, sizeof st);//求多次dijkstra,st每次都要初始化
        memset(dist, 0x3f, N * sizeof (int));
        priority_queue<PII, vector<PII>, greater<PII>> heap;
        
        dist[start] = 0;//这里的dist是局部的,只不过重名了罢了,所以是一维的
            
        heap.push({dist[start], start});//PII根据第一个排序,所以dist放最前面,堆优化版dijksra就是存PII
        
        while (heap.size())
        {
            PII t = heap.top();
            heap.pop();
            int ver = t.y;
            
            if(st[ver]) continue;
            st[ver] = true;
            
            for (int i = h[ver]; ~i; i = ne[i])
            {
                int j = e[i];
                
                if (dist[j] > dist[ver] + w[i])
                {
                    dist[j] = dist[ver] + w[i];
                    heap.push({dist[j], j});
                }
            }
        }
    }
    
    
    int dfs(int u, int start, int distance)
    {
        if (u == 6) return distance;//最多6层(佳佳自己的家 + 5个亲戚),为什么不是7,因为到佳佳自己的家距离为0,我们不需要计算,只需要计算5层就行,不需要真正计算6层
        
        int res = INF;
        
        for (int i = 1; i <= 5; i ++ )//遍历next是哪个站,每个i对应一个source[i]是有实际意义的
        {
            if(!st[i])
            {
                int next = source[i];
                st[i] = true;
                res = min(res, dfs(u + 1, i, distance + dist[start][next]));//start传i的原因是到了下一层之后起点就是source[i]了,只不过在本层它是next而已
                st[i] = false;//要回溯
            }
        }
        
        return res;
    }
    
    
    int main()
    {
        memset(h, -1, sizeof h);
        
       
        cin >> n >> m;
        source[0] = 1;
        for (int i = 1; i <= 5; i ++ ) cin >> source[i];
    
        for (int i = 0; i < m; i ++ )
        {
            int a, b, c;
            cin >> a >> b >> c;
            add(a, b, c), add(b, a, c);
        }
        
        for (int i = 0; i < 6; i ++ ) dijkstra(source[i], dist[i]);
        
        memset(st, 0, sizeof st);//dijkstra和dfs共用一个st数组,因此做完dij后要重置st数组给dfs遍历用
        
        cout << dfs(1, 0, 0);//1指的是第一层,0指的是start是source[0]也就是车站1(佳佳的家),0指的是当前距离为0
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    AcWing 340. 通信线路

    #include 
    #include 
    #include 
    
    using namespace std;
    
    const int N = 1e3 + 10, M = 2 * 1e4 + 10;
    
    int n, m, k;
    bool st[N];
    int dist[N];
    deque<int> q;
    int e[M], h[N], w[M], ne[M], idx;
    
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    bool check(int bound)
    {
        memset(st, 0, sizeof st);//因为进行多从check,所以要重置
        memset(dist, 0x3f, sizeof dist);
        dist[1] = 0;
        q.push_back(1);
        
        while (q.size())
        {
            int t = q.front();
            q.pop_front();
            
            if (st[t]) continue;
            st[t] = true;
            
            for (int i = h[t]; ~i; i = ne[i])
            {
                int j = e[i], v = w[i] > bound;//如果大于,改边的边权就是1,代表当前大于mid的边增加1
                if (dist[j] > dist[t] + v)
                {
                    dist[j] = dist[t] + v;
                    if (v == 0) q.push_front(j);
                    else q.push_back(j);
                }
            }
        }
        return dist[n] <= k;//判断到n点的路径大于mid的边是否<=k
    }
    
    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), add(b, a, c);
        }
        
        int l = 0, r = 1e6 + 1;//为什么不是1-1e6,l取0是因为答案可能取0,比如3条边 k =3,那么答案就不用付钱。 
                               //答案可能取1e6,假如第k+1条边正好是1e6,但是题目有不连通的情况,因此取1e6+1,判断是否联通
        
        while (l < r)
        {
            int mid = l + r >> 1;
            if (check(mid)) r = mid;
            else l = mid + 1;
        }
        
        if (r == 1e6 + 1) cout << "-1";
        else cout << r;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75

    AcWing 342. 道路与航线

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    typedef pair<int, int> PII;
    #define x first
    #define y second
    
    const int N = 25000 + 10, M = 3 * 5 * 1e4 + 10, INF = 0x3f3f3f3f;
    
    int h[N], e[M], w[M], ne[M], idx;
    
    int id[N], din[N];
    vector<int> block[N];
    
    bool st[N];
    int dist[N];
    queue<int> q;//存储团
    
    int n, mR, mP, S;
    int bcnt;
    
    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, int bid)
    {
        id[u] = bid, block[bid].push_back(u);//记录每个团内有哪些点,有多个团,因此vector是二维的
        
        for (int i = h[u]; ~i; i = ne[i])
        {
            int j = e[i];
            if (id[j] == 0) dfs(j, bid);
        }
    }
    
    
    void dijkstra(int bid)
    {
        //虽然多次进行dijkstra,但是每次dij遍历的点的下标都是不同的,因此st数组不用重置
        priority_queue<PII, vector<PII>, greater<PII>> heap;//每个团都用一个堆去存这个团内的点
        
        for (auto u : block[bid])
            heap.push({dist[u], u});
        
        while (heap.size())
        {
            PII t = heap.top();
            heap.pop();
            int distance = t.x, ver = t.y; 
            if (st[ver]) continue;
            st[ver] = true;
            
            for (int i = h[ver]; ~i; i = ne[i])
            {
                int j = e[i];
                if (id[ver] != id[j] && -- din[id[j]] == 0) q.push(id[j]);//如果两点不在一个团内,将那个点的入度减1,如果为0,则加入拓扑序列中
                if (dist[j] > distance + w[i])
                {
                    dist[j] = distance + w[i];
                    if (id[ver] == id[j]) heap.push({dist[j], j});//如果这两个点在一个团内,再将j点加入当前团的堆
                }
            }
        }
    }
    
    
    //为什么按拓扑序会得到最少花费,因为每次入队也就是允许访问的节点的入度为0,
    //这保证我们到此点的所有路径已经被走过并且在此过程中不断更新最短距离,
    //当我们开始访问入度为0的点时,已经没有其他到此点的路径去更新它的最短距离
    
    //(没太懂下面这句话啥意思)
    //这道题没有存团之间的最短距离,也没什么意义,
    //而是当dij遍历到有向边时更新访问到的另一个团中点的最短距离,
    //实际上与上面的类似,也可以保证当访问每个入度为0的点时,
    //团类的所有点没有其他没有访问过的从其他团到此点的路径
    void topsort()
    {
        memset(dist, 0x3f, sizeof dist);
        dist[S] = 0;
        
        for (int i = 1; i <= bcnt; i ++ )
        {
            if (din[i] == 0) q.push(i);//入度为0的团就加入拓扑序列中
        }
        
        while (q.size())//遍历每个团
        {
            int t = q.front();
            q.pop();
            
            dijkstra(t);//dij每个团的同时更新拓扑序列 if (id[ver] != id[j] && -- din[id[j]] == 0) q.push(id[j]);
        }
    }
    
    int main ()
    {
        cin >> n >> mR >> mP >> S;
        memset(h, -1, sizeof h);
        
        for (int i = 0; i < mR; i ++ )
        {
            int a , b, c;
            cin >> a >> b >> c;
            add (a, b, c), add (b, a, c);
        }
        
        
        for (int i = 1; i <= n; i ++ )//初始化各个团以及各个团有哪些点
        {
            if (id[i] == 0)
            {
                id[i] = ++ bcnt;
                dfs(i, bcnt);
            }
        }
        
        for (int i = 0; i < mP; i ++ )
        {
            int a, b, c;
            cin >> a >> b >> c;
            add (a, b, c);
            din[id[b]] ++;//P是航线,连接各个团,b是单向边的目的地,因此b所在的团入度+1
        }
        
        topsort();
        
        for (int i = 1; i <= n; i ++ )
        {
            if (dist[i] > INF / 2) cout << "NO PATH" << endl;//因为有负边,所以只要判断结果大于一个比较大的数就说明不连通
            else cout << dist[i] << endl;
        }
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    AcWing 341. 最优贸易

    一篇博客解释了为什么一个正向建图求最小值,反向建图求最大值
    根本思想是保证1到n的买卖是连通的
    在这里插入图片描述

    #include 
    #include 
    
    using namespace std;
    
    const int N = 1e5 + 10, M = 2 * 2 * 5 * 1e5 + 10;//题目给的有的是双向边有的是单向边,同时我们需要建两个图再乘2
    
    int q[N], dmin[N], dmax[N];
    int n, m;
    int w[N];//存储的是每个城市的水晶球价格,而不是边权,所以开N的大小
    int rh[N], h[N], e[M], ne[M], idx;
    bool st[N];
    
    
    void add(int h[], int a, int b)
    {
        e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    void spfa(int h[], int dist[], int type)
    {
        int hh = 0, tt = 1;
        
        if (type == 0)
        {
            memset(dist, 0x3f, sizeof dmin);//或者sizeof int
            dist[1] = w[1];
            q[0] = 1;
        }
        else
        {
            memset(dist, -0x3f, sizeof dmax);
            dist[n] = w[n];
            q[0] = n;
        }
        
        while (hh != tt)
        {
            int t = q[hh ++ ];
            if (hh == N) hh = 0;
            st[t] = false;
            
            for (int i = h[t]; ~i; i = ne[i])
            {
                int j = e[i];
                if (type == 0 && dist[j] > min(dist[t], w[j]) || type == 1 && dist[j] < max(dist[t], w[j]))
                {
                    if (type == 0) dist[j] = min(dist[t], w[j]);
                    else dist[j] = max(dist[t], w[j]);
                    
                    if (st[j] == 0)
                    {
                        q[tt ++] = j;
                        if (tt == N) tt = 0;
                        st[j] = true;
                    }
                }
            }
        }
        
    }
    
    
    int main()
    {
        cin >> n >> m;
        for (int i = 1; i <= n; i ++ ) cin >> w[i];
        
        memset(h, -1, sizeof h), memset(rh, -1, sizeof rh);
        for (int i = 0; i < m; i ++ )
        {
            int a, b, type;
            cin >> a >> b >> type;
            add(h, a, b), add(rh, b, a);
            if (type == 2)
                add(h, b, a), add(rh, a, b);
        }
        
        spfa(h, dmin, 0);
        spfa(rh, dmax, 1);
        
        int res = 0;
        for (int i = 1; i <= n; i ++ )
            res = max(res, (dmax[i] - dmin[i]));//正向图遍历从1到i可以买入水晶球的最小值,反向图遍历从n到i可以卖出水晶求的最大值,两者求差
        cout << res;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
  • 相关阅读:
    uniapp实现在线PDF文件预览
    python:yolov算法修改数据集txt标签类别
    react16之前diff算法的理解和总结
    kibana报错和重定向次数过多
    C#/.NET/.NET Core技术前沿周刊 | 第 5 期(2024年9.9-9.15)
    kubesphere安装Maven+JDK17 流水线打包
    爬虫全网抓取
    二、OSPF协议基础
    C++ —— 引用
    通讯协议学习之路:IrDA协议协议理论
  • 原文地址:https://blog.csdn.net/chirou_/article/details/131076407