Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. /**
  2.  * Created by default on 21.10.14.
  3.  */
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class path {
  9.  
  10.     FastScanner in;
  11.     PrintWriter out;
  12.  
  13.     int vertices;
  14.     int start;
  15.     int edges;
  16.     long [] dist;
  17.     boolean [] accessible;
  18.     boolean [] wasted;
  19.     ArrayList<Edge> edgeList;
  20.     ArrayList<ArrayList<Integer>> graph;
  21.  
  22.     public static final long INF = 5000000000000000000L;
  23.  
  24.     public void run() {
  25.         try {
  26.             in = new FastScanner(new FileInputStream("path.in"));
  27.             out = new PrintWriter(new FileOutputStream("path.out"));
  28.             solve();
  29.             out.close();
  30.         } catch (IOException e) {
  31.             e.printStackTrace();
  32.         }
  33.     }
  34.  
  35.     public void solve() throws IOException {
  36.  
  37.         try {
  38.  
  39.             vertices = in.nextInt();
  40.             edges = in.nextInt();
  41.             start = in.nextInt() - 1;
  42.             wasted = new boolean[vertices];
  43.             Arrays.fill(wasted, false);
  44.             dist = new long[vertices];
  45.             graph = new ArrayList<ArrayList<Integer>>();
  46.             edgeList = new ArrayList<Edge>();
  47.             accessible = new boolean[vertices];
  48.  
  49.             init_graph();
  50.             fordBellman(start);
  51.  
  52.             for (int i = 0; i < vertices; i++) {
  53.                 if (wasted[i])
  54.                     out.print("-");
  55.                 else if (dist[i] == INF)
  56.                     out.print("*");
  57.                 else
  58.                     out.print(dist[i]);
  59.                 out.print("\n");
  60.             }
  61.         } catch (Exception e){}
  62.  
  63.     }
  64.  
  65.     public void fordBellman(int vertex) {
  66.         bfs(vertex);
  67.         Arrays.fill(dist, INF);
  68.         dist[vertex] = 0;
  69.         for (int i = 0; i < vertices - 1; i++)
  70.             for (Edge e : edgeList)
  71.                 if (dist[e.end] > dist[e.start] + e.weight) {
  72.                     dist[e.end] = Math.max(-INF, dist[e.start] + e.weight);
  73.                 }
  74.  
  75.         for (Edge e : edgeList)
  76.             if (dist[e.end] > dist[e.start] + e.weight && accessible[e.end])
  77.                 dfs(e.end);
  78.  
  79.     }
  80.  
  81.     public void dfs(int vertex) {
  82.         wasted[vertex] = true;
  83.         for (int v : graph.get(vertex))
  84.             if (!wasted[v])
  85.                 dfs(v);
  86.     }
  87.  
  88.     public void bfs(int vertex) {
  89.         Deque<Integer> queue = new ArrayDeque<Integer>();
  90.         queue.add(vertex);
  91.         accessible[vertex] = true;
  92.         while (!queue.isEmpty()) {
  93.             for (int v : graph.get(queue.pollFirst())) {
  94.                 if (!accessible[v]) {
  95.                     accessible[v] = true;
  96.                     queue.add(v);
  97.                 }
  98.             }
  99.         }
  100.     }
  101.  
  102.     public void init_graph() throws IOException {
  103.  
  104.         for (int i = 0; i < vertices; i++) {
  105.             graph.add(new ArrayList<Integer>());
  106.         }
  107.  
  108.         for (int i = 0; i < edges; i++) {
  109.  
  110.             int edge_start = in.nextInt() - 1;
  111.             int edge_end = in.nextInt() - 1;
  112.             int weight = in.nextInt();
  113.  
  114.             edgeList.add(new Edge(edge_start, edge_end, weight));
  115.             graph.get(edge_start).add(edge_end);
  116.  
  117.         }
  118.     }
  119.  
  120.     public static class FastScanner {
  121.         private BufferedReader reader;
  122.         private StringTokenizer tokenizer;
  123.         public boolean hasMoreTokens() throws IOException{
  124.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  125.                 String s = nextLine();
  126.                 if (s == null) return false;
  127.                 tokenizer = new StringTokenizer(s);
  128.             }
  129.             return true;
  130.         }
  131.         FastScanner(InputStream inputStream) {
  132.             reader = new BufferedReader(new InputStreamReader(inputStream));
  133.         }
  134.  
  135.         public String nextLine() throws IOException {
  136.             StringBuilder sb = new StringBuilder();
  137.             while (tokenizer != null && tokenizer.hasMoreTokens()) {
  138.                 sb.append(" ");
  139.                 sb.append(tokenizer.nextToken());
  140.             }
  141.             if (sb.length() == 0) {
  142.                 return reader.readLine();
  143.             }
  144.             return sb.toString();
  145.         }
  146.  
  147.  
  148.         public String next() throws IOException {
  149.             while (tokenizer == null || !tokenizer.hasMoreTokens())
  150.                 tokenizer = new StringTokenizer(nextLine());
  151.             return tokenizer.nextToken();
  152.         }
  153.  
  154.         public int nextInt() throws NumberFormatException, IOException {
  155.             return Integer.parseInt(next());
  156.         }
  157.         public long nextLong() throws NumberFormatException, IOException {
  158.             return Long.parseLong(next());
  159.         }
  160.  
  161.  
  162.         public double nextDouble() throws NumberFormatException, IOException {
  163.             return Double.parseDouble(next());
  164.         }
  165.  
  166.     }
  167.  
  168.     public static void main(String[] args) {
  169.         new path().run();
  170.     }
  171.  
  172.     public class Edge {
  173.         int start;
  174.         int end;
  175.         long weight;
  176.  
  177.         public Edge(int start, int end, long weight) {
  178.             this.start = start;
  179.             this.end = end;
  180.             this.weight = weight;
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement