package util.graph;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

public class DynDjikstra {
    public static State FindShortest(Node start, Node target) {
        PriorityQueue<Node> openSet = new PriorityQueue<>();
        Set<Node> closedSet = new HashSet<>();
        openSet.add(start);
        while (!openSet.isEmpty()) {
            Node current = openSet.poll();
            for (Edge edge : current.edges) {
                Node end = edge.target;
                if (closedSet.contains(end)) {
                    continue;
                }
                State newState = edge.travel(current.last);
                //if (end.equals(target)) {
                    //return newState;
                //}
                if (openSet.contains(end)) {
                    if (end.last.compareTo(newState)>0) {
                        end.last = newState;
                    }
                } else {
                    openSet.add(end);
                    end.last = newState;
                }
            }
            closedSet.add(current);
            if (closedSet.contains(target)) {
                return target.last;
            }
        }

        return target.last;
    }
}

package dancing_with_googlers;

import java.io.*;
import java.util.*;

public class Start {
	
	/** Requires two arguments - the file name, and the encoding to use.  */
	  public static void main(String [] args) throws IOException {
		        ArrayList<String> input = ReadWriteTextFile.doReadTextFile("B-small-attempt4.in");
		        ArrayList<String> output = new ArrayList<String>();
		        
		        int n = Integer.parseInt(input.get(0));
		        for (int i=1;i<=n;i++) {
		            System.out.println("Case#: " + i + " ");
		        	output.add(Integer.toString(FindNumber.start(input.get(i))));
		        }
		        
		        ReadWriteTextFile.doWriteTextFile(output);
	}
	 
}