@@ -0,0 +1,2651 @@
+package me.alivecode.algs4;%0A%0Aimport edu.princeton.cs.algs4.In;%0A%0Aimport java.util.Objects;%0A%0A/**%0A * Created by oujt on 17-3-9.%0A */%0Apublic class EdgeWeightedDigraph %7B%0A    private static final String NEWLINE = System.getProperty(%22line.separator%22);%0A%0A    private Bag%3CDirectedEdge%3E%5B%5D adj;%0A    private int V;%0A    private int E;%0A    private int%5B%5D indegree;%0A%0A    public EdgeWeightedDigraph(int V) %7B%0A        if (V %3C 0) throw new IllegalArgumentException(%22argument to EdgeWeightedDigraph must be nonnegative%22);%0A        this.V = V;%0A        indegree = new int%5BV%5D;%0A        adj = (Bag%3CDirectedEdge%3E%5B%5D) new Bag%5BV%5D;%0A        for(int v = 0; v %3C V; v++) %7B%0A            adj%5Bv%5D = new Bag%3C%3E();%0A        %7D%0A    %7D%0A%0A    public EdgeWeightedDigraph(In in) %7B%0A        this.V = in.readInt();%0A        this.E = in.readInt();%0A        this.indegree = new int%5BV%5D;%0A        this.adj = (Bag%3CDirectedEdge%3E%5B%5D) new Bag%5BV%5D;%0A        for(int v = 0; v %3C V; v++) %7B%0A            adj%5Bv%5D = new Bag%3C%3E();%0A        %7D%0A        for(int i = 0; i %3C E; i++) %7B%0A            int v = in.readInt();%0A            int w = in.readInt();%0A            double weight = in.readDouble();%0A            DirectedEdge e = new DirectedEdge(v, w, weight);%0A            adj%5Bv%5D.put(e);%0A            indegree%5Bw%5D++;%0A        %7D%0A    %7D%0A%0A    public EdgeWeightedDigraph(EdgeWeightedDigraph G) %7B%0A        this(G.V());%0A        for(int v = 0; v %3C G.V(); v++) %7B%0A            Stack%3CDirectedEdge%3E edges = new Stack%3C%3E();%0A            for(DirectedEdge e : G.adj(v)) %7B%0A                edges.push(e);%0A            %7D%0A%0A            for(DirectedEdge e : edges) %7B%0A                DirectedEdge c = new DirectedEdge(e.from(), e.to(), e.weight());%0A                addEdge(c);%0A            %7D%0A        %7D%0A    %7D%0A%0A    public void addEdge(DirectedEdge e) %7B%0A        validateVertex(e.from());%0A        validateVertex(e.to());%0A        adj%5Be.from()%5D.put(e);%0A        E++;%0A        indegree%5Be.to()%5D++;%0A    %7D%0A%0A    public Iterable%3CDirectedEdge%3E adj(int v) %7B%0A        validateVertex(v);%0A        return adj%5Bv%5D;%0A    %7D%0A%0A    public int V() %7B%0A        return V;%0A    %7D%0A%0A    public int E() %7B%0A        return E;%0A    %7D%0A%0A    public int indegree(int v) %7B%0A        validateVertex(v);%0A        return indegree%5Bv%5D;%0A    %7D%0A%0A    public String toString() %7B%0A        StringBuilder sb = new StringBuilder();%0A        return %22%22;%0A    %7D%0A%0A    private void validateVertex(int v) %7B%0A        if (v %3C 0 %7C%7C v %3E= V) %7B%0A            throw new IllegalArgumentException(v + %22 is not between 0 and %22 + (V-1));%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        In in = new In(args%5B0%5D);%0A        EdgeWeightedDigraph G = new EdgeWeightedDigraph(in);%0A        EdgeWeightedDigraph G1 = new EdgeWeightedDigraph(G);%0A    %7D%0A%7D%0A

Add weighted edge directed graph.