Advertisement
daskalot

Untitled

Jan 12th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. package Task10;
  2. import java.util.*;
  3. class Edge{
  4.     int to, weight;
  5.     Edge(int t, int w){ this.to = t;    this.weight = w; }
  6. }
  7. class Node{
  8.     int heurisitcValue, price, id; 
  9.     Node(){
  10.         this.price = 0;     this.id = -1;       this.id = 0;
  11.         }
  12. }
  13. class Graph{
  14.     int nodes;
  15.     ArrayList<Edge>[] adj;
  16.     Node[] nodesContainer;
  17.     boolean[] visited;
  18.     @SuppressWarnings("unchecked")
  19.     Graph(int t){
  20.         this.nodes = t;
  21.         this.nodesContainer = new Node[nodes];
  22.         this.visited = new boolean[nodes];
  23.         adj = (ArrayList<Edge>[])new ArrayList[nodes];
  24.         for(int i=0;i<t;i++) {         
  25.             adj[i] = new ArrayList<Edge>();
  26.             this.nodesContainer[i] = new Node();
  27.         }
  28.     }
  29.     void addEdge(int from, int to, int weight) {
  30.         adj[from].add(new Edge(to,weight));
  31.     }
  32.     int findMin(int s, int d) {
  33.         if(adj[s].size() == 0 && s != d) {
  34.             updated = true;
  35.             nodesContainer[s].price = Integer.MAX_VALUE;
  36.             nodesContainer[s].heurisitcValue = Integer.MAX_VALUE;
  37.             return -3;
  38.         }
  39.         if(s == d) {
  40.            
  41.             return -2;
  42.         }
  43.         int id = -1 , val = Integer.MAX_VALUE, weight = 0;
  44.         for(int i=0;i<adj[s].size();i++) {
  45.             Edge e = adj[s].get(i);
  46.             if(!visited[e.to]) {
  47.                 int tempPrice = e.weight + nodesContainer[e.to].heurisitcValue;
  48.                 if(tempPrice < val) {
  49.                     id = e.to;         
  50.                     val = tempPrice;           
  51.                     weight = e.weight;
  52.                 }
  53.                 if(tempPrice == val) id = Math.min(id, e.to);              
  54.             }          
  55.         }
  56.         if(id == -1) {
  57.             return -1;
  58.         }
  59.         else {
  60.             visited[s] = true;
  61.             nodesContainer[id].price = val;
  62.             int prevHeuristic = nodesContainer[s].heurisitcValue;
  63.             nodesContainer[s].heurisitcValue = Math.min(nodesContainer[s].heurisitcValue + weight, val);
  64.             if(prevHeuristic != nodesContainer[s].heurisitcValue) {
  65.                 updated = true;
  66.             }
  67.         //  else debug();
  68.             return id;
  69.         }
  70.     }
  71.     void debug() {
  72.         System.out.println("TUKA");
  73.     }
  74.     void reset() {
  75.         for(int i=0;i<nodes;i++)    visited[i] = false;
  76.     }
  77.     boolean updated = true;
  78.     void findPath(int s, int t) {
  79.         int nextNode = s, prevNode = 0;
  80.         while(updated) {
  81.             nextNode = s;
  82.             updated = false;
  83.             for(int i=0;i<nodes;i++) {
  84.                 prevNode = nextNode;
  85.                 nextNode = findMin(nextNode,t);
  86.                 if(nextNode < 0) {
  87.                     System.out.println(prevNode);
  88.                     break;
  89.                 }
  90.                 System.out.print(prevNode+",");
  91.             }
  92.             reset();       
  93.         }
  94.     }  
  95. }
  96. public class Naloga10 {
  97.     public static void main(String[] args) {
  98.         Scanner sc = new Scanner(System.in);
  99.         Graph g = new Graph(5004);
  100.         int totalNodes = 0;
  101.         int m = sc.nextInt();
  102.         for(int i=0;i<m;i++) {
  103.             int f = sc.nextInt(), t = sc.nextInt(), w = sc.nextInt();
  104.             totalNodes = Math.max(Math.max(f, t), totalNodes);
  105.             g.addEdge(f,t,w);
  106.         }
  107.         g.nodes = totalNodes;
  108.         g.findPath(sc.nextInt(), sc.nextInt());
  109.         sc.close();
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement