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 qualification.taskB;

import Parser.MyReader;
import Parser.MyWriter;

public class Main {

	public static void main(String[] args) {
		MyReader myReader = new MyReader(
				"/Users/shami13/Downloads/B-small-attempt0.in");
		MyWriter myWriter = new MyWriter(
				"/Users/shami13/Downloads/B-small-attempt0.out");

		int taskLength = Integer.parseInt(myReader.read());
		for (int i = 1; i <= taskLength; i++) {
			String[] taskString = myReader.read().split(" ");
			int T = Integer.parseInt(taskString[0]);
			int S = Integer.parseInt(taskString[1]);
			int P = Integer.parseInt(taskString[2]);
			int[] googlers = new int[T];
			for(int p = 3; p < taskString.length; p++){
				googlers[p-3] = Integer.parseInt(taskString[p]);
			}
			
			Task task = new Task(T,S,P,googlers);
			String result = task.execute();
			myWriter.writeString("Case #" + i + ": " + result);
			System.out.println("Case #" + i + ": " + result);
		}
		myWriter.close();
	}
}
