Previous
0 / 4
Module DS-14DSJAVA

Bellman-Ford Distance

DSJAVA β€’ Data Structures and Algorithms in Java

Browser-only practice

Problem Statement

Challenge lab in Shortest Paths and Relaxation

Mission

Find a directed shortest path when edges may be negative.

Learning outcome: Handle negative edges with Bellman-Ford

Correctness contract

Invariant: Each stored distance is a discovered path cost, and relaxation only improves it with a valid path.

Required technique: Relax all directed edges for up to V - 1 passes while guarding unreachable sources.

Complexity target: time O(VE); space O(V+E).

Input and output

Input: V and E, then E directed triples (from, to, weight), then source and target vertices. Whitespace may be spaces or line breaks.

Output: Print the shortest distance as one integer, or INF when the target is unreachable. Return it as a String; Main.java prints it without adding other text.

Assumptions:

  • No negative cycle is reachable from the source.
  • Every vertex index is valid.

Before you code

  1. Restate the input and output contract, then predict the visible example without running code.
  2. Implement the core state transition: Relax all directed edges for up to V - 1 passes while guarding unreachable sources.
  3. Trace the smallest boundary case, verify exact formatting, and justify the authored time and auxiliary-space bounds.

Implement Practice.solve(Scanner sc). Keep every provided filename and public class name unchanged.

Sample input

4 4 0 1 1 1 2 -2 2 3 3 0 3 10 0 3

Sample output

2

Why the sample works: Visible walkthrough for the ordinary non-trivial path. Repeated full-edge relaxation propagates valid improvements across paths containing up to V - 1 edges. Input `4 4 0 1 1 1 2 -2 2 3 3 0 3 10 0 3` therefore produces `2`.

Progressive hints

Try the trace and first milestone before opening a hint. Open them in order.

Open hint 1

Hint 1 β€” Contract: identify what each parsed variable represents and write the invariant beside the loop or recursive method.

Open hint 2

Hint 2 β€” Next step: A complete pass with no updates means all reachable shortest distances are already stable.

Open hint 3

Hint 3 β€” Verification: compare the structure state before and after one operation, then test the smallest valid input and a duplicate or unreachable case when allowed.

Constraints

Input contract: V and E, then E directed triples (from, to, weight), then source and target vertices.

  • No negative cycle is reachable from the source.
  • Every vertex index is valid.

Required technique: Relax all directed edges for up to V - 1 passes while guarding unreachable sources.

Output contract: Print the shortest distance as one integer, or INF when the target is unreachable.

Use Java 8-compatible code only. Keep the public class names and Practice.solve(Scanner sc) signature from the starter files.

Input Format

V and E, then E directed triples (from, to, weight), then source and target vertices.

Output Format

Print the shortest distance as one integer, or INF when the target is unreachable.

Sample Testcases

Submit runs every public testcase in this browser. Results and code never leave this device.

Sample #1
Public sample
4 4 0 1 1 1 2 -2 2 3 3 0 3 10 0 3
2
Visible walkthrough for the ordinary non-trivial path. Repeated full-edge relaxation propagates valid improvements across paths containing up to V - 1 edges. Input `4 4 0 1 1 1 2 -2 2 3 3 0 3 10 0 3` therefore produces `2`.

Web terminal

C, C++, Java, and Python run locally in a browser VM. No worker or visualizer is used.

Saved in this browser
Editor settings