Cod sursa(job #1703702)

Utilizator cristian.diaconuDiaconu Cristian cristian.diaconu Data 17 mai 2016 14:55:41
Problema Algoritmul lui Dijkstra Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 4.37 kb
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

class MyScanner2 {
    BufferedReader br;
    StringTokenizer st;
 
    public MyScanner2() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    public MyScanner2(String name) throws FileNotFoundException {
    	InputStream input = new FileInputStream(name);
        br = new BufferedReader(new InputStreamReader(input));
    }
 
    public String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }
 
    public int nextInt() {
        return Integer.parseInt(next());
    }
 
    public long nextLong() {
        return Long.parseLong(next());
    }
 
    public double nextDouble() {
        return Double.parseDouble(next());
    }
 
    public String nextLine(){
        String str = "";
        try {
            str = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }
}

class Pair {
	
	public int destination;
	public int distance;
	
	public Pair(int n, int p) {
		destination = n;
		distance = p;
	}

	public int getDestination() {
		return destination;
	}

	public void setDestination(int node) {
		this.destination = node;
	}

	public int getDistance() {
		return distance;
	}

	public void setDistance(int parent) {
		this.distance = parent;
	}
	
}

class Graph2 {
	
	int nodes;
	ArrayList<ArrayList<Pair>> adjList;
	
	public Graph2(int N) {
		nodes = N;
		adjList = new ArrayList<ArrayList<Pair>>();
		for(int i = 0; i <= nodes; i++) {
			adjList.add(i, new ArrayList<Pair>());
		}
	}
	
	public void addEdge(int src, int dest, int dist) {
		adjList.get(src).add(new Pair(dest, dist));
	}
	
	public void readData(MyScanner2 sc, int nrEdges) {
		
		int src, dest, dist;
		
		for(int i = 0; i < nrEdges; i++) {
			src = sc.nextInt();
			dest = sc.nextInt();
			dist = sc.nextInt();
			
			this.addEdge(src,  dest, dist);
		}
		
	}
	
}

class Main {
	
	public static int M;
	public static int N;
	public static int[] d;
	//public static int[] finalDistances;
	public static boolean[] visited;
	public static Queue<Integer> queue;
	
	public static void Dijkstra(Graph2 g, int root) {
		
		int i, u;
		Pair v;
		visited[root] = true;
		for(i = 0; i < g.adjList.get(root).size(); i++) {
			v = g.adjList.get(root).get(i);
			d[v.getDestination()] = v.getDistance();
			queue.add(v.getDestination());
		}
		
		while(!queue.isEmpty()) {
			u = queue.poll();
			visited[u] = true;
			for(i = 0; i < g.adjList.get(u).size(); i++) {
				v = g.adjList.get(u).get(i);
				if(!visited[v.getDestination()]) {
					if(d[v.getDestination()] > d[u] + v.getDistance()) {
						d[v.getDestination()] = d[u] + v.getDistance();
						queue.add(v.getDestination());
					}
				}
			}
		}
		
	}
	
	public static void main(String[] args) throws FileNotFoundException {
		
		MyScanner2 sc = new MyScanner2("dijkstra.in");
		N = sc.nextInt();
		M = sc.nextInt();
		d = new int[N + 1];
		for(int i = 1; i <= N; i++) {
			d[i] = Integer.MAX_VALUE;
		}
		//finalDistances = new int[N + 1];
		visited = new boolean[N + 1];
		queue = new PriorityQueue<Integer>(N, new Comparator<Integer>() {
			public int compare(Integer a,Integer b) {
				if(d[a] < d[b]) {
					return -1;
				}
				if(d[a] > d[b]) {
					return 1;
				}
				return 0;
			}
		});
		Graph2 g = new Graph2(N);
		g.readData(sc, M);
		Dijkstra(g, 1);
		try {
			PrintWriter out = new PrintWriter(new File("sortaret.out"));
			for(int i = 2; i <= N - 1; i++) {
				out.print(d[i] + " ");
			}
			out.print(d[N]);
			out.close();
		} catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
		
	}
	
}