题解 | #最短路径问题#

最短路径问题

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]);
    }
}

全部评论

相关推荐

牛客52811839...:实习要写出来业务和产出,你这写的像流水账没人看。项目经历也没有,换个极简简历试试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
11438次浏览 98人参与
# 你的实习产出是真实的还是包装的? #
2014次浏览 42人参与
# 米连集团26产品管培生项目 #
6120次浏览 216人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7693次浏览 43人参与
# 简历第一个项目做什么 #
31798次浏览 344人参与
# 重来一次,我还会选择这个专业吗 #
433637次浏览 3926人参与
# 巨人网络春招 #
11386次浏览 223人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
187263次浏览 1122人参与
# 牛客AI文生图 #
21459次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152511次浏览 888人参与
# 研究所笔面经互助 #
118985次浏览 577人参与
# 简历中的项目经历要怎么写? #
310476次浏览 4224人参与
# AI时代,哪些岗位最容易被淘汰 #
63991次浏览 832人参与
# 面试紧张时你会有什么表现? #
30527次浏览 188人参与
# 你今年的平均薪资是多少? #
213196次浏览 1039人参与
# 你怎么看待AI面试 #
180264次浏览 1262人参与
# 高学历就一定能找到好工作吗? #
64348次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76605次浏览 374人参与
# 我的求职精神状态 #
448218次浏览 3129人参与
# 正在春招的你,也参与了去年秋招吗? #
363624次浏览 2638人参与
# 腾讯音乐求职进展汇总 #
160711次浏览 1112人参与
# 校招笔试 #
471498次浏览 2964人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务