Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static double solve(int n, int m, int k, int g, Node[] V, Set<Edge> E) {
- double[][] dp = new double[k + 1][n + 1];
- // dp[i][j] = min cost to reach 'j'-th node and having left `i` collobarators
- for (int j = 1; j <= n; ++j) {
- Node node = V[j];
- for (int i = 0; i <= k; ++i)
- dp[i][node.getId()] = Double.MAX_VALUE / 2;
- }
- for (int i = 0; i <= k; ++i)
- dp[i][V[1].getId()] = 0;
- for (int node_idx = 1; node_idx <= n; ++node_idx) {
- Node node = V[node_idx];
- for (Edge edge: E)
- if (edge.from == node) // optimise later
- {
- dp[k][edge.to.getId()] = Math.min(dp[k][edge.to.getId()] , dp[k][edge.from.getId()] + edge.cost);
- for (int j = k - 1; j >= 0; --j)
- dp[j][edge.to.getId()] = Math.min(dp[j][edge.to.getId()],
- Math.min(dp[j][edge.from.getId()] + edge.getCost(), dp[j + 1][edge.from.getId()] + edge.getCost() / 2.0));
- }
- }
- // after using all `k` colaborators, min cost of reaching `g`
- double ans = dp[0][V[g].getId()];
- if (ans != Double.MAX_VALUE / 2)
- return ans;
- else return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement