// Copyright 2010-2011 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.ortools.flow.samples;

import com.google.ortools.flow.MaxFlow;
import com.google.ortools.flow.MinCostFlow;
import com.google.ortools.flow.StarGraph;

/**
 * Sample showing how to model using the flow solver.
 *
 */

public class FlowExample {

  static {
    System.loadLibrary("jniflow");
  }


  private static void solveMinCostFlow() {
    System.out.println("Min Cost Flow Problem");
    final int numSources = 4;
    final int numTargets = 4;
    final int[][] costs = {{90, 75, 75, 80},
                           {35, 85, 55, 65},
                           {125, 95, 90, 105},
                           {45, 110, 95, 115}};
    final int expectedCost = 275;
    StarGraph graph = new StarGraph(numSources + numTargets,
                                    numSources * numTargets);
    MinCostFlow  minCostFlow = new MinCostFlow(graph);
    for (int source = 1; source <= numSources; ++source) {
      for (int target = 1; target <= numTargets; ++target) {
        final long arc = graph.addArc(source, numSources + target);
        minCostFlow.setArcUnitCost(arc, costs[source -  1][target - 1]);
        minCostFlow.setArcCapacity(arc, 1);
      }
    }

    for (int source = 1; source <= numSources; ++source) {
      minCostFlow.setNodeSupply(source, 1);
    }
    for (int target = 1; target <= numTargets; ++target) {
      minCostFlow.setNodeSupply(numSources + target, -1);
    }

    final long totalFlowCost = minCostFlow.computeMinCostFlow();
    System.out.println("total flow = " + totalFlowCost + "/" + expectedCost);
  }

  private static void solveMaxFlow() {
    System.out.println("Max Flow Problem");
    final int numNodes = 6;
    final int numArcs = 9;
    final int[] tails = {1, 1, 1, 1, 2, 3, 4, 4, 5};
    final int[] heads = {2, 3, 4, 5, 4, 5, 5, 6, 6};
    final int[] capacities = {5, 8, 5, 3, 4, 5, 6, 6, 4};
    final int[] expectedFlows = {4, 4, 2, 0, 4, 4, 0, 6, 4};
    final int expectedTotalFlow = 10;
    StarGraph graph = new StarGraph(numNodes, numArcs);
    MaxFlow maxFlow = new MaxFlow(graph, 1, numNodes);
    for (int i = 0; i < numArcs; ++i) {
      final long arc = graph.addArc(tails[i], heads[i]);
      maxFlow.setArcCapacity(arc, capacities[i]);
    }
    final long totalFlow = maxFlow.computeMaxFlow();
    System.out.println("total flow " + totalFlow + "/" + expectedTotalFlow);
    for (int i = 0; i < numArcs; ++i) {
      System.out.println("Arc " + i + ": " + maxFlow.flow(i + 1) + "/" +
                         expectedFlows[i]);
    }
  }

  public static void main(String[] args) throws Exception {
    FlowExample.solveMinCostFlow();
    FlowExample.solveMaxFlow();
  }
}

@@ -1457,33 +1457,33 @@
 r (int source = 
-1
+0
 ; source %3C= numS
@@ -1468,33 +1468,32 @@
 ce = 0; source %3C
-=
  numSources; ++s
@@ -1517,33 +1517,33 @@
 r (int target = 
-1
+0
 ; target %3C= numT
@@ -1528,33 +1528,32 @@
 et = 0; target %3C
-=
  numTargets; ++t
@@ -1685,21 +1685,16 @@
 urce
- -  1
 %5D%5Btarget
  - 1
@@ -1693,12 +1693,8 @@
 rget
- - 1
 %5D);%0A
@@ -1774,17 +1774,17 @@
 ource = 
-1
+0
 ; source
@@ -1785,17 +1785,16 @@
 source %3C
-=
  numSour
@@ -1882,17 +1882,17 @@
 arget = 
-1
+0
 ; target
@@ -1893,17 +1893,16 @@
 target %3C
-=
  numTarg
@@ -2294,23 +2294,26 @@
 ails = %7B
-1, 1, 1
+0, 0, 0, 0
 , 1, 2, 
@@ -2319,15 +2319,12 @@
  3, 
-4
+3
 , 4
-, 5
 %7D;%0A 
@@ -2347,16 +2347,19 @@
 eads = %7B
+1, 
 2, 3, 4,
@@ -2363,24 +2363,21 @@
  4, 
-5
+3
 , 4, 
-5
+4
 , 5, 
-6, 6
+5
 %7D;%0A 
@@ -2629,17 +2629,17 @@
 (graph, 
-1
+0
 , numNod
@@ -2640,16 +2640,20 @@
 numNodes
+ - 1
 );%0A    f
@@ -3033,12 +3033,8 @@
 ow(i
- + 1
 ) + 

fix java example