import java.io.File; import java.io.IOException; import java.util.Scanner; import java.util.ArrayList; import java.util.Set; import java.util.TreeSet; import java.util.Arrays; public class Instance { private int numAuthors; private int numPapers; private int numEdges; private int quotaSum = -1; private int[] authorQuotas; private int[] paperScores; private int[][] apEdges; private ArrayList> authorPaperSets; public Instance(Scanner sc) throws IOException{ // Iterate over lines. If line begins with "#", ignore. String ln; State state = State.INIT; int count = 0; int remaining = 1; while (sc.hasNextLine()) { ln = sc.nextLine(); if (ln.length() > 0 && ln.charAt(0) != '#') switch (state){ case INIT: // Read in 3 numbers on the same line: // \-> number authors // \-> number papers // \-> number a->p edges String[] nums = ln.trim().split(" "); numAuthors = Integer.parseInt(nums[0]); numPapers = Integer.parseInt(nums[1]); numEdges = Integer.parseInt(nums[2]); // Initialise storage authorQuotas = new int[numAuthors]; paperScores = new int[numPapers]; apEdges = new int[numEdges][2]; Arrays.fill(authorQuotas, 4); authorPaperSets = new ArrayList>(numAuthors); for(int i=0; i()); // Now move to next state. state = State.PAPER_SCORES; break; // case QUOTAS: // // Author ~count~ may have 0 or ~value~ papers assigned. // authorQuotas[count] = Integer.parseInt(ln.trim()); // // Next... state. // if (++count >= numAuthors) { // count = 0; // state = State.PAPER_SCORES; // } // break; case PAPER_SCORES: // Paper ~count~ has a score of ~value~. paperScores[count] = Integer.parseInt(ln.trim()); // Next state! if (++count >= numPapers) { count = 0; state = State.EDGES; } break; case EDGES: // Two nums on one line, sanitise and read. String[] parts = ln.trim().split(" "); int a = apEdges[count][0] = Integer.parseInt(parts[0]); int p = apEdges[count][1] = Integer.parseInt(parts[1]); // Place this value into the author's set of papers. authorPaperSets.get(a).add(p); // Mark work as finished. if (++count >= numEdges) state = State.DONE; break; } } // If unfinished, scream about it. if (state != State.DONE) throw new IOException("Bad input -- file ended unexpectedly!"); } public static Instance fromDir(String dir) throws IOException { // Take directory of a file, open it and construct. try (Scanner sc = new Scanner(new File(dir))) { return new Instance(sc); } catch (IOException e) { throw e; } } public int getNumAuthors() { return numAuthors; } public int getNumPapers() { return numPapers; } public int getNumEdges() { return numEdges; } public int[] getQuotas() { return authorQuotas; } public int getQuota(int authorId) { return authorQuotas[authorId]; } public int getQuotaSum() { if(quotaSum == -1) quotaSum = Arrays.stream(authorQuotas).sum(); return quotaSum; } public int[] getScores() { return paperScores; } public int getScore(int paperId) { return paperScores[paperId]; } public int[][] getEdges() { return apEdges; } public int[] getEdge(int edgeIndex) { return apEdges[edgeIndex]; } public TreeSet getAuthorPapers(int authorId) { return authorPaperSets.get(authorId); } private static enum State { INIT, QUOTAS, PAPER_SCORES, EDGES, DONE } }