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

public class Googlers {
	public static int google(int N, int S, int P, int[] googler) {
		int counter = 0;
		for (int j = 0; j < googler.length; j++) {
			if (googler[j] % 3 == 0 && P >= 0) {
				if (googler[j] != 0) {
					if (googler[j] / 3 >= P) {
						counter++;
					} else if (googler[j] / 3 + 1 >= P && S != 0) {
						counter++;
						S--;
					}	
				}
				else if (googler[j] == 0 && P == 0) {
					counter++;
				}
			} else if (googler[j] % 3 == 1) {
				if (googler[j] / 3 + 1 >= P) {
					counter++;
				}
			} else if (googler[j] % 3 == 2) {
				if (googler[j] / 3 + 1 >= P) {
					counter++;
				}
				else if (googler[j] / 3 + 2 >= P && S != 0) {
					counter++;
					S--;
				}
			}
		}
		return counter;
	}
}
