kadai1

 

import java.util.*;
import java.io.*;
public class Graph {
 int num = 0;
  ArrayList<Edge> list = new ArrayList<Edge>();  //隣接ノード、重さ//
  ArrayList<Node> nodes = new ArrayList<Node>();  //ノード番号、隣接数、先頭オフセット//
 class Edge {
  private int to; private double weight;
  Edge(int to, double weight) {this.to = to; this.weight = weight;}
  int To() { return to;}
  double Weight() { return weight;}
 }
 class Node {
  int id; int Num; int pointer;
  Node (int id,int Num, int pointer) {this.id = id; this.Num = Num; this.pointer = pointer;}
  int Num() {return this.Num;}
  int Id(){ return this.id;}
  int Pointer() { return this.pointer;}
  }

 void LoadGraph(String filename){
 try{
  Scanner scan = new Scanner(new File(filename));
  int pointer = 0;
 while(true){
 if( !scan.hasNextLine()) {
   break;
 }
 String line = scan.nextLine();
 String s = line.split(":",0);
 if (s.length != 1){
 String t = s[1].split(",",0);
 for (int i = 0; i < t.length; i++) {
 String u = t[i].split("@",2);
 Edge e = new Edge(Integer.parseInt(u[0]), Double.parseDouble(u[1]));
 list.add(e);
  }
 
 Node n = new Node(Integer.parseInt(s[0]), t.length, pointer);
 nodes.add(n);
 num ++; //ノード数//
 pointer = pointer + t.length ;
  }
 else {
  Node n = new Node(Integer.parseInt(s[0]),0 , pointer);
 nodes.add(n);
 num ++; //ノード数//
 }
 }
 } catch (java.io.FileNotFoundException e){
  System.out.println(e);
  System.exit(0);
  }
 }

 void printGraph(){
 for (int j = 0; j < num ; j++) {
 System.out.print(nodes.get(j).Id() + ":");
 for (int k = 0; k < nodes.get(j).Num(); k++) {
 System.out.print(list.get(nodes.get(j).Pointer() + k).To() + "@" + list.get(nodes.get(j).Pointer()+k).Weight());
 if (k < nodes.get(j).Num() - 1)
 System.out.print(",");
   }
 System.out.println();
  }
 }
}
 
 
 
 
 
 
import java.util.*;
import java.io.*;
class GraphTester {
 public static void main (String args){
 Graph g = new Graph();
 g.LoadGraph("graph001.txt");
 g.printGraph();
 }
 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0:1@2.0,3@6.0,4@1.0
1:6@3.0
2:
3:6@2.0
4:5@2.0
5:3@1.0,2@2.0
6:2@4.0