记录下自己改写的用到了java优先队列

题目主要是https://blog.csdn.net/Clove_unique/article/details/71246633 这个模仿过来的,记录下 毕竟写了1个多小时呢

题目大意:给定平面上的n个点,定义(x1,y1)到(x2,y2)的费用为min(|x1-x2|,|y1-y2|),求从1号点走到n号点的最小费用。

package com.yinjun.spring.proority;

import java.util.*;

public class Dijkstra {
    public static final int MAX_LENGTH = 100;
    static int n, total = 0;
    //这边我开小一点数组 就是为了测试
    static Pointer[] pointer = new Pointer[MAX_LENGTH];
    static int[] p = new int[MAX_LENGTH], nxt = new int[MAX_LENGTH], v = new int[MAX_LENGTH], c = new int[MAX_LENGTH], visit = new int[MAX_LENGTH], dis = new int[MAX_LENGTH];

    static class Node {
        public int key;
        public int value;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    static class Pointer {
        public int x;
        public int y;
        public int index;
    }

    static void add(int x, int y, int z) {
        total++;
        nxt[total] = p[x];
        p[x] = total;
        v[total] = y;
        c[total] = z;
        total++;
        nxt[total] = p[y];
        p[y] = total;
        v[total] = x;
        c[total] = z;
    }

    static void dijkstra(int s, int t) {
        Queue manQueue = new PriorityQueue<>((Node a, Node b) -> {
            return a.value - b.value;
        });
        for(int i = 1; i  <= n ; i++){
            dis[i] = MAX_LENGTH + 1;
        }
        dis[s] = 0;
        manQueue.offer(new Node(s, 0));
        while (!manQueue.isEmpty()) {
            Node min = manQueue.poll();
            //拿到最小值
            int x = min.key;
            if (visit[x] == 1) {
                continue;
            }
            visit[x] = 1;
            for (int i = p[x]; i > 0; i = nxt[i]) {
                if (dis[v[i]] > dis[x] + c[i]) {
                    dis[v[i]] = dis[x] + c[i];
                    manQueue.offer(new Node(v[i],dis[v[i]]));
                }
            }
        }
        System.out.println(dis[t]);

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            pointer[i] = new Pointer();
            pointer[i].x = sc.nextInt();
            pointer[i].y = sc.nextInt();
            pointer[i].index = i;
        }
        //先按照x排序 从小到大排序
        Arrays.sort(pointer, 1, n+1, (Pointer a, Pointer b) -> {
            if (a.x == b.x) {
                return a.y - b.y;
            }
            return a.x - b.x;
        });

        for (int i = 1; i < n; i++) {
            add(pointer[i].index, pointer[i + 1].index, getMin(pointer[i], pointer[i + 1]));
        }
        //先按照y排序 从小到大排序
        Arrays.sort(pointer, 1, n+1, (Pointer a, Pointer b) -> {
            if (a.y == b.y) {
                return a.x - b.x;
            }
            return a.y - b.y;
        });

        for (int i = 1; i < n; i++) {
            add(pointer[i].index, pointer[i + 1].index, getMin(pointer[i], pointer[i + 1]));
        }

        dijkstra(1, n);
    }

    private static int getMin(Pointer pointer, Pointer pointer1) {
        return Math.min(Math.abs(pointer.x - pointer1.x), Math.abs(pointer.y - pointer1.y));
    }
}

 

你可能感兴趣的:(JAVA编程)