day 57 图论part9

文章目录

  • [dijkstra(堆优化版)精讲 47. 参加科学大会(第六期模拟笔试)](#dijkstra(堆优化版)精讲 47. 参加科学大会(第六期模拟笔试))
  • [Bellman_ford 算法精讲 94. 城市间货物运输 I](#Bellman_ford 算法精讲 94. 城市间货物运输 I)

dijkstra(堆优化版)精讲 47. 参加科学大会(第六期模拟笔试)

加入小顶堆,每次从优先队列获取到最小值,不需要遍历整个数组。

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


class Edge {
    int to;
    int val;
    Edge(int to, int val) {
        this.to = to;
        this.val = val;
    }
}

class Pair {
    public int first;
    public int second;
    Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }
}

class MyComparision implements Comparator<Pair> {
    @Override
    public int compare(Pair r, Pair l) {
        return Integer.compare(r.second, l.second);
    }
}


class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] minDist = new int[n + 1];
        List<List<Edge>> grid = new ArrayList<>(n + 1);
        Arrays.fill(minDist, Integer.MAX_VALUE);
        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[] visited  = new boolean[n + 1];
        PriorityQueue<Pair> pq = new PriorityQueue<>(new MyComparision());
        pq.add(new Pair(1, 0));
        minDist[1] = 0;

        while (!pq.isEmpty()) {
            Pair cur = pq.poll();
            visited[cur.first] = true;
            for (Edge edge : grid.get(cur.first)) {
                if (!visited[edge.to] && minDist[edge.to] > minDist[cur.first] + edge.val) {
                    minDist[edge.to] = minDist[cur.first] + edge.val;
                    pq.add(new Pair(edge.to, minDist[edge.to]));
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println(-1);
        }
        else {
            System.out.println(minDist[n]);
        }
    }
}

Bellman_ford 算法精讲 94. 城市间货物运输 I

总共最多需要松弛边n - 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> edges = new ArrayList<>();
        
        for (int i = 0; i < m; i++) {
            int s = sc.nextInt();
            int t = sc.nextInt();
            int v = sc.nextInt();
            edges.add(new Edge(s, t, v));
        }
        int[] minDist = new int[n + 1];
        Arrays.fill(minDist, Integer.MAX_VALUE);
        minDist[1] = 0;

        for (int i = 1; i < n; i++) {
            for (Edge edge : edges) {
                if (minDist[edge.from] != Integer.MAX_VALUE && minDist[edge.from] + edge.val < minDist[edge.to]) {
                    minDist[edge.to] = minDist[edge.from] + edge.val;
                }
            }
        }
        if (minDist[n] == Integer.MAX_VALUE) {
            System.out.println("unconnected");
        }
        else {
            System.out.println(minDist[n]);
        }

    }
}
相关推荐
wno7042 分钟前
Spring Boot JdbcTemplate配置Druid多数据源
java·spring boot·后端
ShiXZ2135 分钟前
网络调试四剑客:ping / telnet / nc / netstat 速查指令集
运维·开发语言·网络·php
码农大叔的博客7 分钟前
golang示例:switch
开发语言·后端·golang
个 人 练 习 生13 分钟前
数据结构入门:算法复杂度
开发语言·数据结构·经验分享·学习·程序人生·算法
Ricardo-Yang18 分钟前
无人机单目深度估计测试:ZipDepth 与 AerialMetric
人工智能·算法·机器学习·计算机视觉·无人机
W_3260022 分钟前
Python-OpenCV边缘检测与阈值分割:Sobel、Scharr、Laplacian、Canny、全局与自适应阈值
开发语言·图像处理·python·opencv·机器学习
其实防守也摸鱼23 分钟前
红队技能总结导图:从入门到精通的完整知识体系
开发语言·人工智能·学习·安全·web安全
Nil20828 分钟前
golang解决单词转换
算法
聊浮游32 分钟前
JAVA2026最新全套学习资料、学习路线
java·开发语言·jvm·mysql·spring·maven·idea
MacroZheng1 小时前
完美替代 Navicat!这款内置 AI 的数据库工具,太香了!
java·后端·mysql