Guest User

Untitled

a guest
Jan 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import java.math.*;
  4.  
  5. public class Main {
  6. private BufferedReader in;
  7. private PrintWriter out;
  8. private StringTokenizer st;
  9. private Random rnd;
  10.  
  11. public void solve() throws IOException {
  12. int n = nextInt(), m = nextInt();
  13.  
  14. int[][] g = new int[n][n];
  15. for(int i = 0; i < n; i++) Arrays.fill(g[i], -1);
  16. for(int i = 0; i < m; i++) g[nextInt() - 1][nextInt() - 1] = nextInt();
  17.  
  18. int[] d = new int[n];
  19. Arrays.fill(d, Integer.MAX_VALUE / m);
  20. d[0] = 0;
  21.  
  22. int[] id = new int[n];
  23.  
  24. ArrayDeque<Integer> q = new ArrayDeque<Integer>();
  25. q.add(0);
  26.  
  27. while(q.size() > 0) {
  28. int u = q.pollFirst();
  29. out.println(u);
  30. out.flush();
  31. id[u] = 1;
  32.  
  33. for(int v = 0; v < n; v++) {
  34. if(g[u][v] == -1) continue;
  35. if(d[v] > d[u] + g[u][v]) {
  36. d[v] = d[u] + g[u][v];
  37.  
  38. if(id[v] == 0) {
  39. q.addLast(v);
  40. } else if(id[v] == 1) {
  41. q.addFirst(v);
  42. }
  43.  
  44. id[v] = 1;
  45. }
  46. }
  47. }
  48.  
  49. }
  50.  
  51. public static void main(String[] args) {
  52. new Main().run();
  53. }
  54.  
  55. public void run() {
  56. try {
  57. //in = new BufferedReader(new FileReader("bikers.in"));
  58. //out = new PrintWriter(new FileWriter("bikers.out"));
  59.  
  60. in = new BufferedReader(new InputStreamReader((System.in)));
  61. out = new PrintWriter(System.out);
  62.  
  63. st = null;
  64. rnd = new Random();
  65.  
  66. solve();
  67.  
  68. out.close();
  69. } catch(IOException e) {
  70. e.printStackTrace();
  71. }
  72. }
  73.  
  74. private String nextToken() throws IOException, NullPointerException {
  75. while(st == null || !st.hasMoreTokens()) {
  76. st = new StringTokenizer(in.readLine());
  77. }
  78.  
  79. return st.nextToken();
  80. }
  81.  
  82. private int nextInt() throws IOException {
  83. return Integer.parseInt(nextToken());
  84. }
  85.  
  86. private long nextLong() throws IOException {
  87. return Long.parseLong(nextToken());
  88. }
  89.  
  90. private double nextDouble() throws IOException {
  91. return Double.parseDouble(nextToken());
  92. }
  93.  
  94. }
Add Comment
Please, Sign In to add comment