Пример #1
0
/**
 * SPOJ 203 POTHOLE (Potholers) Solution
 *
 * <p>this problem converted to a simple maximum flow problem. each capacity is 1 or infinite.
 */
public class Main implements Runnable {

  private static final MaximumFlowAlgorithm MAXFLOW = EdmondsKarpAlgorithm.getInstance();
  private static final IntegerNumberSystem NS = IntegerNumberSystem.getInstance();

  @SuppressWarnings("unused")
  @Override
  public void run() {
    Scanner in = new Scanner(new BufferedInputStream(System.in));

    for (int casei : ZeroTo.get(in.nextInt())) {
      MutableCapacityGraph<Integer, Integer> capacityGraph = MutableCapacityGraph.create();
      int n = in.nextInt();
      for (int v : FromTo.get(1, n + 1)) capacityGraph.insertVertex(v);

      for (int from : FromTo.get(1, n)) {
        for (int j : ZeroTo.get(in.nextInt())) {
          int to = in.nextInt();
          int capacity;
          if (from == 1 || to == n) capacity = 1;
          else capacity = 1000000;
          capacityGraph.addEdge(from, to, capacity);
        }
      }
      System.out.println(MAXFLOW.calc(capacityGraph, 1, n, NS).calcTotalFlow());
    }
  }

  public static void main(String[] args) throws Exception {
    if (args.length >= 1) System.setIn(new FileInputStream(args[0]));
    new Main().run();
  }
}
  @Test
  public void example() {

    // Let's construct a graph.

    MutableDirectedWeightedGraph<String, Integer> graph = MutableDirectedWeightedGraph.create();
    graph.insertVertex("A");
    graph.insertVertex("B");
    graph.insertVertex("C");
    graph.addEdge("A", "C", 100);
    graph.addEdge("A", "B", 10);
    graph.addEdge("B", "C", 20);

    // Let's get the shortest paths of all pairs.

    AllPairShortestPath floyd = FloydWarshallAlgorithm.getInstance();
    AllPairShortestPathResult<String, Integer, DirectedWeightedEdge<String, Integer>> res =
        floyd.calc(graph, IntegerNumberSystem.getInstance());

    int distanceAToB = res.getDistance("A", "B"); // must be 10
    int distanceBToC = res.getDistance("B", "C"); // must be 20

    Assert.assertEquals(10, distanceAToB);
    Assert.assertEquals(20, distanceBToC);
  }
public abstract class MaximumFlowAlgorithmTestBase {

  private static final IntegerNumberSystem NS2 = IntegerNumberSystem.getInstance();
  private MaximumFlowAlgorithm algorithm;

  public MaximumFlowAlgorithmTestBase(MaximumFlowAlgorithm algorithm) {
    this.algorithm = algorithm;
  }

  @Test
  public void testCLRS() {
    Object[][] data = {
      {"0", "1", 16},
      {"0", "2", 13},
      {"1", "2", 10},
      {"1", "3", 12},
      {"2", "1", 4},
      {"2", "4", 14},
      {"3", "2", 9},
      {"3", "5", 20},
      {"4", "3", 7},
      {"4", "5", 4}
    };
    Graph<String, CapacityEdge<String, Integer>> g = TestGraphFactory.createCapacityGraphNew(data);
    Assert.assertEquals(23, (int) algorithm.calc(g, "0", "5", NS2).calcTotalFlow());
  }
}
Пример #4
0
public class PointOnSegment2DTest {

  public static final IntegerNumberSystem NS = IntegerNumberSystem.getInstance();

  @Test
  public void test() {
    Segment2D<Integer> s = Segment2D.create(Point2D.create(0, 0), Point2D.create(10, 0));
    assertTrue(PointOnSegment2D.isOn(Point2D.create(0, 0), s, NS));
    assertTrue(PointOnSegment2D.isOn(Point2D.create(5, 0), s, NS));
    assertFalse(PointOnSegment2D.isOn(Point2D.create(11, 0), s, NS));
  }
}