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;
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Conny
 */
public class ProbQ2 {
	public static void main(String[] args) {

		String fileContent = CodeJamTools.readFileAsString("c:\\temp\\inputQ2.txt");
		String[] rows = fileContent.split("\\n");
		int T = new Integer(rows[0]);
		String output = "";

		int counter = 1;

		for (int i=0; i<T; i++) {
			System.out.println(rows[counter]);
                        long[] data = CodeJamTools.readLongsFromRow(rows[counter]);
			counter++;

                        long Ni = data[0];
                        long p = data[2];
                        long S = data[1];

                        long limit1 = Math.max(1,3*p-4);
                        long limit2 = 3*p-2;

                        long overs = 0;
                        long borderlines = 0;
                        for (int j=3; j<data.length; j++) {
                            if (data[j] >= limit2)
                                overs++;
                            else if (data[j] >= limit1)
                                borderlines++;
                        }

			output += "Case #"+(i+1)+": ";
			output += (overs+Math.min(borderlines, S));
                        output += "\n";
		}

		System.out.print(output);
		CodeJamTools.writeStringToFile("C:\\Users\\Conny\\Documents\\conny\\own files\\java_workspace\\codejam2\\src\\outputQ2.txt", output);
	}

}
