Advertisement
Guest User

Untitled

a guest
Oct 21st, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.39 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.Queue;
  4. import java.util.Scanner;
  5.  
  6. public class Solution {
  7.     static Node[] nodes;
  8.     static boolean[] visited;
  9.     static ArrayList<Node> queue = new ArrayList<Node>();
  10.  
  11.     public static void main(String args[]) {
  12.         Scanner input = new Scanner(System.in);
  13.         int T = input.nextInt();
  14.         for (int j = 0; j < T; j++) {
  15.             int N = input.nextInt();
  16.             int M = input.nextInt();
  17.             nodes = new Node[N + 1];
  18.             visited = new boolean[N + 1];
  19.             for (int i = 0; i < N + 1; i++) {
  20.                 nodes[i] = new Node();
  21.             }
  22.             for (int i = 0; i < N; i++) {
  23.                 int x = input.nextInt();
  24.                 int y = input.nextInt();
  25.                 int r = input.nextInt();
  26.                 nodes[x].data = x;
  27.                 nodes[y].data = y;
  28.                 nodes[x].distance = (int) 10e9;
  29.                 nodes[y].distance = (int) 10e9;
  30.                 nodes[x].addNeighbor(y, r);
  31.                 nodes[y].addNeighbor(x, r);
  32.                 if (!queue.contains(nodes[x]))
  33.                     queue.add(nodes[x]);
  34.                 if (!queue.contains(nodes[y]))
  35.                     queue.add(nodes[y]);
  36.             }
  37.             int S = input.nextInt();
  38.             dijkstra(nodes[S]);
  39.             for (int i = 0; i < nodes.length; i++) {
  40.                 if (nodes[i].data != 0 && nodes[i].data != S)
  41.                     System.out.print(nodes[i].distance + " ");
  42.             }
  43.         }
  44.     }
  45.  
  46.     public static void dijkstra(Node source) {
  47.         source.distance = 0;
  48.         while (!queue.isEmpty()) {
  49.             Node current = getShortestDistance();
  50.             for (int i = 0; i < current.neighbors.size(); i++) {
  51.                 if (!visited[current.neighbors.get(i)]) {
  52.                     int newDistance = current.distance + current.getEdgeDistance(current.neighbors.get(i));
  53.                     if (newDistance < nodes[current.neighbors.get(i)].distance) {
  54.                         nodes[current.neighbors.get(i)].distance = newDistance;
  55.                         nodes[current.neighbors.get(i)].previous = current.data;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     public static Node getShortestDistance() {
  63.         Node shortest = queue.get(0);
  64.         for (int i = 1; i < queue.size(); i++) {
  65.             if (queue.get(i).distance < shortest.distance) {
  66.                 shortest = queue.get(i);
  67.             }
  68.         }
  69.         visited[shortest.data] = true;
  70.         return queue.remove(queue.indexOf(shortest));
  71.  
  72.     }
  73. }
  74.  
  75. class Node {
  76.     int data;
  77.     int previous;
  78.     int distance;
  79.     ArrayList<Integer> neighbors = new ArrayList<Integer>();
  80.     int[] edgeDistances = new int[3001];
  81.  
  82.     public void addNeighbor(int n, int r) {
  83.         neighbors.add(n);
  84.         edgeDistances[n] = r;
  85.     }
  86.  
  87.     public int getEdgeDistance(int n) {
  88.         return edgeDistances[n];
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement