Advertisement
VladNitu

14weblabAD

Jan 21st, 2023 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package weblab;
  2.  
  3. import nl.tudelft.weblab.runner.*;
  4. import java.io.*;
  5. import java.util.*;
  6.  
  7. /**
  8.  * WARNING: The spec tests are not necessarily equal to your grade! You can use them help you test
  9.  * for the correctness of your algorithm, but the final grade is determined by a manual inspection
  10.  * of your implementation.
  11.  */
  12. class TheQuestForTheHolyGrail {
  13.  
  14.     /**
  15.      * You should implement this method.
  16.      *
  17.      * @param n the number of nodes.
  18.      * @param m the number of edges.
  19.      * @param k the number of times we can use the research team
  20.      * @param g the index of the holy grail in V.
  21.      * @param V a list of Nodes, where V[1] is the current state and V[g] is the holy grail. You
  22.      *     should not use V[0].
  23.      * @param E a set of Edges
  24.      * @return The length of the shortest path that uses the research team at most k times.
  25.      */
  26.     public static double solve(int n, int m, int k, int g, Node[] V, Set<Edge> E) {
  27.        
  28.         double[][] dp = new double[n + 1][k + 1];
  29.         for (int node = 1; node <= n; ++node)
  30.           for (int j = 0; j <= k; ++j)
  31.             dp[node][j] = Double.MAX_VALUE / 2;
  32.          
  33.         for (int j = 0; j <= k; ++j)
  34.           dp[1][j] = 0;
  35.          
  36.          
  37.         for (int it = 0; it < n - 1; ++it) // at most "n-1" times repeat the process, so that the distances can succesfully propagate in Graph
  38.         {
  39.           for (int node = 1; node <= n; ++node) {
  40.             for (Edge e: E)
  41.               if (e.to.getId() == node) {
  42.                 int from = e.from.getId();
  43.                 int to = e.to.getId();
  44.                 int cost = e.cost;
  45.                
  46.                 dp[to][0] = Math.min(dp[to][0], dp[from][0] + cost);
  47.                 for (int j = 1; j <= k; ++j)
  48.                   dp[to][j] = Math.min(dp[to][j], Math.min(dp[from][j] + cost, dp[from][j-1] + cost * 0.5));
  49.                
  50.               }
  51.                
  52.           }
  53.       }
  54.      
  55.       if (dp[g][k] == Double.MAX_VALUE / 2)  
  56.         return -1;
  57.       else
  58.         return dp[g][k];
  59.   }
  60. }
  61.  
  62. class Node {
  63.  
  64.     protected int id;
  65.  
  66.     public Node(int id) {
  67.         this.id = id;
  68.     }
  69.  
  70.     public int getId() {
  71.         return id;
  72.     }
  73.  
  74.     public boolean equals(Object other) {
  75.         if (other instanceof Node) {
  76.             Node that = (Node) other;
  77.             return this.id == that.id;
  78.         }
  79.         return false;
  80.     }
  81.  
  82.     @Override
  83.     public int hashCode() {
  84.         return Objects.hash(id);
  85.     }
  86. }
  87.  
  88. class Edge {
  89.  
  90.     protected int cost;
  91.  
  92.     protected Node from;
  93.  
  94.     protected Node to;
  95.  
  96.     protected Edge(Node from, Node to, int cost) {
  97.         this.from = from;
  98.         this.to = to;
  99.         this.cost = cost;
  100.     }
  101.  
  102.     public Node getFrom() {
  103.         return from;
  104.     }
  105.  
  106.     public Node getTo() {
  107.         return to;
  108.     }
  109.  
  110.     public int getCost() {
  111.         return cost;
  112.     }
  113.  
  114.     @Override
  115.     public boolean equals(Object o) {
  116.         if (this == o) return true;
  117.         if (o == null || getClass() != o.getClass()) return false;
  118.         Edge edge = (Edge) o;
  119.         return cost == edge.cost && Objects.equals(from, edge.from) && Objects.equals(to, edge.to);
  120.     }
  121.  
  122.     @Override
  123.     public int hashCode() {
  124.         return Objects.hash(cost, from, to);
  125.     }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement