1. import java.util.*;
    2. class Edge{
    3. public int to, w, next;
    4. Edge(){
    5. }
    6. Edge(int to, int w, int next){
    7. this.to = to;
    8. this.w = w;
    9. this.next = next;
    10. }
    11. }
    12. public class Test{
    13. static int cnt = 0;
    14. static int n = 5;
    15. static int m = 7;
    16. static Edge[] edges = new Edge[m];
    17. static int[] head = new int[n + 1];
    18. static void init(){
    19. for(int i = 0; i <= n; i++){
    20. head[i] = -1;
    21. }
    22. cnt = 0;
    23. }
    24. static void add_edge(int u, int v, int w){
    25. edges[cnt] = new Edge();
    26. edges[cnt].to = v;
    27. edges[cnt].w = w;
    28. edges[cnt].next = head[u];
    29. head[u] = cnt++;
    30. }
    31. public static void main(String[] args) {
    32. init();
    33. int[][] inputs = {{1,2,1}, {2,3,2}, {3, 4, 3}, {1, 3, 4}, {4, 1, 5}, {1, 5, 6}, {4, 5, 7}};
    34. for(int i = 0; i < inputs.length; i++){
    35. add_edge(inputs[i][0], inputs[i][1], inputs[i][2]);
    36. }
    37. for(int i = 1; i <= n; i++){
    38. for (int j = head[i]; j != -1; j = edges[j].next){
    39. System.out.println("start=" + i + " to=" + edges[j].to + " w=" + edges[j].w);
    40. }
    41. }
    42. }
    43. }