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 mgg.utils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author manolo
 * @date 13/04/12
 */
public class FileUtil {

	FileReader reader;
	FileWriter writer;
	
	public FileUtil(String input, String output) {
		try {
			reader = new FileReader(input);
			writer = new FileWriter(output);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
		
	public String getLine(){
		try {
			
			StringBuilder builder = new StringBuilder();
			char c = (char) reader.read();
			while (c != '\n') {
				builder.append(c);
				//System.out.println("\t\t\t> char: '" + c + "'");
				c = (char) reader.read();
			} ;
			
			System.out.println("\t\t\t----------> line: '" + builder.toString() + "'");
			
			return builder.toString();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;
	}
	
	public void printLine(int lineNumber, String line){
		try {
			writer.write("Case #" + lineNumber + ": " + line + "\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void closeFiles(){
		try {
			reader.close();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
		
}
