Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.io.*;
- public class Dijkstra {
- private static int z, size = 6; // размер матрици 6х6
- public static void dijkstra(Graph g, int s, long[] prio, int[] pred) {
- try { //считываем текст из ввода, буферизируя прочитанные символы
- BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
- System.out.print("Введите начальную точку: ");
- String line = buf.readLine();
- s = Integer.parseInt(line);
- System.out.print("Введите конечную точку: ");
- line = buf.readLine();
- z = Integer.parseInt(line);
- buf.close();
- } catch (Exception e) {
- System.exit(0);
- }
- Arrays.fill(pred, -1);
- Arrays.fill(prio, INF);
- prio[s] = 0;
- Queue<QItem> q = new PriorityQueue<QItem>(); // устанавливаем приоритет размеров путей
- q.add(new QItem(0, s));
- while (!q.isEmpty()) {
- QItem cur = q.poll();
- if (cur.prio != prio[cur.v]) {
- continue;
- }
- for (Edge e : g.nodeEdges[cur.v]) {
- long nprio = prio[cur.v] + e.cost;
- if (prio[e.t] > nprio) {
- prio[e.t] = nprio;
- pred[e.t] = cur.v;
- q.add(new QItem(nprio, e.t));
- }
- }
- }
- }
- public static int getZ(){return z;}
- public static int getSize(){return size;}
- public static final long INF = Long.MAX_VALUE / 10;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement