题解 | #最短路径问题#

最短路径问题

https://www.nowcoder.com/practice/e372b623d0874ce2915c663d881a3ff2

import java.util.*;
import java.io.*;



public class Main {

    int[] he, ne, e, w, p;
    int idx;
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(
                new InputStreamReader(System.in)));

    public static int nextInt() throws IOException {
        in.nextToken();
        return (int)in.nval;
    }

    public Main() {
        he = new int[1001];
        Arrays.fill(he, -1);
        ne = new int[200003];
        e = new int[200003];
        w = new int[200003];
        p = new int[200003];
        idx = 0;
    }

    public void add(int a, int b, int d, int m) {
        e[idx] = b;
        ne[idx] = he[a];
        he[a] = idx;
        w[idx] = d;
        p[idx] = m;
        idx++;
    }

    public int[] dijkstra(int s, int end) {
        int[][] dists = new int[1001][2];
        PriorityQueue<int[]> q = new PriorityQueue<>((a, b)->a[1] - b[1]);
        for (int i = 1; i < 1001; i++) {
            dists[i][0] = 0x3f3f3f3f;
        }
        boolean[] st = new boolean[1001];
        dists[s][0] = 0;
        q.add(new int[] {s, dists[s][0]});
        while (!q.isEmpty()) {
            int[] cur = q.poll();
            int cur_p = cur[0];
            if (st[cur_p]) continue;
            st[cur_p] = true;
            for (int i = he[cur_p]; i != -1; i = ne[i]) {
                int j = e[i];
                if (dists[j][0] > dists[cur_p][0] + w[i]) {
                    dists[j][0] = dists[cur_p][0] + w[i];
                    dists[j][1] = dists[cur_p][1] + p[i];
                    q.add(new int[] {j, dists[j][0]});
                }else if(dists[j][0] == dists[cur_p][0] + w[i]){
                    dists[j][1] = Math.min(dists[cur_p][1] + p[i],dists[j][1]);
                }
            }
        }
        return dists[end];
    }


    public static void main(String[] args) throws IOException {
        Main g = new Main();
        int n = nextInt();
        int m = nextInt();
        for (int i = 0; i < m; i++) {
            int r1 = nextInt();
            int r2 = nextInt();
            int r3 = nextInt();
            int r4 = nextInt();
            g.add(r1, r2, r3, r4);
            g.add(r2, r1, r3, r4);
        }
        int[] ret = g.dijkstra(nextInt(), nextInt());
        System.out.print(ret[0] + " " + ret[1]);
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
有很多问题,求大佬们解答,谢谢大佬们:不知道现在该怎么投实习,该怎么准备内心很纠结学校课程和实习到底怎么选择,&nbsp;自己也不想课程学业这边出问题,&nbsp;是不是只能投暑期实习,具体时间该怎么安排前端面试也需要准备算法么,&nbsp;自己的算法能力很薄弱,&nbsp;面试题需要准备到什么程度?没有ai项目经验的话,我该如何去补充,如何去找好的ai项目
smile丶snow:1.简历尽量一页,比如教育经历那里,全日制,计算机学院这些可以去掉没啥用好浪费空间。 熟悉三件套就没必要写了吧。js基本上是这样写 * JavaScript核心:深入理解 JS 运行机制(事件循环 Event Loop、微任务/宏任务),熟练掌握 Promise/Async 异步编程 模型。 熟悉可以改成熟练掌握。组件库写一个ant感觉就行,多写了浪费空间。 旅游项目是不是jonas的natours啊,我之前简历也有这个。我之前是这样写的 全栈思维: 熟悉 Node.js/Express 后端架构,掌握 MongoDB 数据库设计与聚合查询 工程化我觉得还是少些吧,不写就问的少,如果你真的了解的话可以写。 1.实习的话推荐大厂官网和aoob上面投,我自己有写一个校招网站的小网站可以直达~github主页上面有,顺便求个关注( 2.大三下一般课程比较少了吧,如果学校比较严的话可以多沉淀一会,如果不太严可以请dai课然后去实习,尽量找个近一些的就行。暑期实习不是暑假才实习哦,基本是上3月底4月初发offer就可以过去了,然后大概暑假的时候走转正流程答辩。 3.大厂算法题+js手写体。hot100+常见的比如数组转树,Promise.all,deepClone,之类 js手写都不难其实。算法看自己能力吧,我其实算法能力也不行。 4.自己平时没有用AI Coding吗?自己想一下怎么让AI帮你更好的写代码~比如Skill的诞生,OpenSpec的诞生,不都是我们想让AI更好帮我们写代码吗。
我的实习日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务