Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Scanner;
  8. import java.util.ArrayList;
  9. import java.util.Collections;
  10. import java.util.*;
  11.  
  12. public class Main {
  13.     static class Task {
  14.         public static final String INPUT_FILE = "in";
  15.         public static final String OUTPUT_FILE = "out";
  16.         public static final int NMAX = 50005;
  17.  
  18.         int n;
  19.         int m;
  20.         int source;
  21.  
  22.         class pqComp implements Comparator<Entry>{
  23.             public int compare(Entry e1, Entry e2){
  24.                 return e2.dist - e1.dist;
  25.             }
  26.         }
  27.  
  28.         public class Entry {
  29.             public int node;
  30.             public int dist;
  31.  
  32.             Entry (int node, int distance){
  33.                 this.node = node;
  34.                 dist = distance;
  35.             }
  36.         }
  37.         public class Edge {
  38.             public int node;
  39.             public int cost;
  40.  
  41.             Edge(int _node, int _cost) {
  42.                 node = _node;
  43.                 cost = _cost;
  44.             }
  45.         }
  46.  
  47.         @SuppressWarnings("unchecked")
  48.         ArrayList<Edge> adj[] = new ArrayList[NMAX];
  49.  
  50.         private void readInput() {
  51.             try {
  52.                 Scanner sc = new Scanner(new BufferedReader(new FileReader(
  53.                                 INPUT_FILE)));
  54.                 n = sc.nextInt();
  55.                 m = sc.nextInt();
  56.                 source = sc.nextInt();
  57.  
  58.                 for (int i = 1; i <= n; i++) {
  59.                     adj[i] = new ArrayList<>();
  60.                 }
  61.                 for (int i = 1; i <= m; i++) {
  62.                     int x, y, w;
  63.                     x = sc.nextInt();
  64.                     y = sc.nextInt();
  65.                     w = sc.nextInt();
  66.                     adj[x].add(new Edge(y, w));
  67.                 }
  68.                 sc.close();
  69.             } catch (IOException e) {
  70.                 throw new RuntimeException(e);
  71.             }
  72.         }
  73.  
  74.         private void writeOutput(ArrayList<Integer> result) {
  75.             try {
  76.                 BufferedWriter bw = new BufferedWriter(new FileWriter(
  77.                                 OUTPUT_FILE));
  78.                 StringBuilder sb = new StringBuilder();
  79.                 for (int i = 1; i <= n; i++) {
  80.                     sb.append(result.get(i)).append(' ');
  81.                 }
  82.                 sb.append('\n');
  83.                 bw.write(sb.toString());
  84.                 bw.close();
  85.             } catch (IOException e) {
  86.                 throw new RuntimeException(e);
  87.             }
  88.         }
  89.  
  90.         private ArrayList<Integer> getResult() {
  91.             // TODO: Gasiti distantele minime de la nodul source la celelalte noduri
  92.             // folosind Dijkstra pe graful orientat cu n noduri, m arce stocat in adj.
  93.             //  d[node] = costul minim / lungimea minima a unui drum de la source la
  94.             //  nodul node;
  95.             //  d[source] = 0;
  96.             //  d[node] = -1, daca nu se poate ajunge de la source la node.
  97.             // Atentie:
  98.             // O muchie este tinuta ca o pereche (nod adiacent, cost muchie):
  99.             //  adj[x].get(i).node = nodul adiacent lui x,
  100.             //  adj[x].get(i).cost = costul.
  101.  
  102.             //Entry - int node, int dist
  103.  
  104.             ArrayList<Integer> d = new ArrayList<>();
  105.             PriorityQueue<Entry> q = new PriorityQueue<>(new pqComp());
  106.             int vizitat[] = new int[n+1];
  107.  
  108.             for(int i = 0; i <= n; i++){
  109.                 vizitat[i] = 0;
  110.             }
  111.  
  112.             for (int i = 0; i <= n; i++)
  113.                 d.add(0);
  114.  
  115.             for(int i = 0; i <= n; i++){
  116.                 d.set(i,9999999);
  117.             }
  118.  
  119.             d.set(source,0);
  120.  
  121.             q.add(new Entry(source,0));
  122.             while(q.size() != 0){
  123.                 Entry u = q.poll();
  124.                 int aux_node = u.node;
  125.                 int aux_dist = u.dist;
  126.                 vizitat[aux_node] = 1;
  127.                 for (Edge ed : adj[aux_node]){
  128.                     int vec = ed.node;
  129.                     int c = ed.cost;
  130.                     if(vizitat[vec] == 0 && d.get(vec) > d.get(aux_node) + c){
  131.                         d.set(vec, d.get(aux_node) + c);
  132.                         q.add(new Entry(vec,d.get(vec)));
  133.                     }
  134.                 }
  135.             }
  136.             for(int i = 0; i <= n; i ++){
  137.                 if (d.get(i) == 9999999) d.set(i,-1);
  138.             }
  139.       return d;
  140.         }
  141.  
  142.         public void solve() {
  143.             readInput();
  144.             writeOutput(getResult());
  145.         }
  146.     }
  147.  
  148.     public static void main(String[] args) {
  149.         new Task().solve();
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement