day 59 图论part10

文章目录

  • [Bellman_ford 队列优化算法(又名SPFA)卡码网 94. 城市间货物运输 I](#Bellman_ford 队列优化算法(又名SPFA)卡码网 94. 城市间货物运输 I)
  • [bellman_ford之判断负权回路 卡码网 95. 城市间货物运输 I](#bellman_ford之判断负权回路 卡码网 95. 城市间货物运输 I)
  • [bellman_ford之单源有限最短路 卡码网 96. 城市间货物运输 III](#bellman_ford之单源有限最短路 卡码网 96. 城市间货物运输 III)

Bellman_ford 队列优化算法(又名SPFA)卡码网 94. 城市间货物运输 I

使用队列来优化。

java 复制代码
import java.util.*;

class Edge {
    int to;
    int val;
    Edge(int to, int val) {
        this.to = to;
        this.val = val;
    }
}
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Edge>> grid = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            grid.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int val = sc.nextInt();
            grid.get(s).add(new Edge(t, val));
        }

        boolean[] inque = new boolean[n + 1]; 
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;
        Queue<Integer> dq = new LinkedList<>();
        dq.add(1);
        inque[1] = true;
        while(!dq.isEmpty()) {
            int cur = dq.poll();
            inque[cur] = false;
            for (Edge edge : grid.get(cur)) {
                if (minDist[edge.to] > minDist[cur] + edge.val) {
                    minDist[edge.to] = minDist[cur] + edge.val;
                    if (!inque[edge.to]) {
                        dq.add(edge.to);
                        inque[edge.to] = true;
                    }
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println("unconnected");
        }
        else {
            System.out.println(minDist[n]);
        }
    }
}

bellman_ford之判断负权回路 卡码网 95. 城市间货物运输 I

判断是否一个节点真正的入队了n次,从而判断是否成环。

java 复制代码
import java.util.*;

class Edge {
    int to;
    int val;
    Edge(int to, int val) {
        this.to = to;
        this.val = val;
    }
}
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<List<Edge>> grid = new ArrayList<>();
        for (int i = 0; i <= n; i++) {
            grid.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int val = sc.nextInt();
            grid.get(s).add(new Edge(t, val));
        }

        boolean[] inque = new boolean[n + 1];
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;
        Queue<Integer> dq = new LinkedList<>();
        dq.add(1);
        inque[1] = true;
        boolean flag = false;
        int[] count = new int[n + 1];
        while(!dq.isEmpty()) {
            int cur = dq.poll();
            inque[cur] = false;
            for (Edge edge : grid.get(cur)) {
                if (minDist[edge.to] > minDist[cur] + edge.val) {
                    minDist[edge.to] = minDist[cur] + edge.val;
                    if (!inque[edge.to]) {
                        dq.add(edge.to);
                        inque[edge.to] = true;
                        count[edge.to]++;
                    }
                    
                    if (count[edge.to] >= n) {
                        flag = true;
                        while (!dq.isEmpty()) dq.poll();
                        break;
                    } 
                }
            }
        }
         if (flag) {
            System.out.println("circle");
        } else if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println("unconnected");
        } else {
            System.out.println(minDist[n]);
        }
    }
}

bellman_ford之单源有限最短路 卡码网 96. 城市间货物运输 III

从原点开始松弛边,松弛一次能找到距离原点1条边的最短路径,k个点就需要松弛k + 1边。

java 复制代码
import java.util.*;
class Edge {
    int from;
    int to;
    int val;
    Edge(int from, int to, int val) {
        this.from = from;
        this.to = to;
        this.val = val;
    }
}

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        List<Edge> grid = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int val =sc.nextInt();
            grid.add(new Edge(s,t,val));
        }
        int src = sc.nextInt();
        int dst = sc.nextInt();
        int k = sc.nextInt();

        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        int[] copyDist = new int[n + 1];
        minDist[src] = 0;
        for (int i =0; i < k + 1; i++) {
            copyDist = Arrays.copyOf(minDist, n + 1);
            for (Edge edge : grid) {
                if (copyDist[edge.from] != Integer.MAX_VALUE && minDist[edge.to] > copyDist[edge.from] + edge.val) {
                    minDist[edge.to] = copyDist[edge.from] + edge.val;
                }
            }
        }
        if (minDist[dst] == Integer.MAX_VALUE) {
            System.out.println("unreachable");
        } else {
            System.out.println(minDist[dst]);
        }

    }
}
相关推荐
木木子225 小时前
# 密码强度检测深度解析:正则表达式实时分析、多维度评分算法与可视化反馈
mysql·算法·华为·正则表达式·harmonyos
大圣编程6 小时前
Java 多维数组详解
java·开发语言
Sw1zzle8 小时前
算法入门(四):二叉树 - 递归遍历三件套
算法·leetcode
万法若空9 小时前
【算法-查找】查找算法
java·数据结构·算法
海石9 小时前
子树怎么找?树的3种遍历方式来帮忙!
算法·leetcode
海石9 小时前
难度分 1588:思路 + 技巧 = AC
算法·leetcode
殳翰9 小时前
下服务器端开发流程及相关工具介绍(C++)
开发语言·c++
落寞的星星9 小时前
这个对象就包含了已经转换好的DFA和各种词法分析器运转所需要的参数。下一步,我们就可以用ScannerInfo对象创建出Scanner对象,请看下面的代码:
开发语言·c#
nothing&nowhere10 小时前
用 Python 做问卷数据清洗:无效样本检测与处理实战
开发语言·python·数据清洗·数据处理·问卷星·问卷星脚本·刷问卷
2601_9615934210 小时前
Rust 开发环境配置繁琐?RustRover 开箱即用搞定编码调试
开发语言·后端·macos·rust