Advertisement
Guest User

Untitled

a guest
May 27th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. package lab3;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Random;
  7. import java.util.StringTokenizer;
  8.  
  9. public class TaskB {
  10.  
  11. FastScanner in;
  12. PrintWriter out;
  13.  
  14. void solve() throws FileNotFoundException {
  15. int n = in.nextInt();
  16. int s = in.nextInt() - 1;
  17. int f = in.nextInt() - 1;
  18.  
  19. long[][] edges = new long[n][n];
  20.  
  21. for (int i = 0; i < n; i++) {
  22. for (int j = 0; j < n; j++) {
  23. edges[i][j] = in.nextLong();
  24. }
  25. }
  26.  
  27. long[] d = new long[n];
  28. boolean[] marked = new boolean[n];
  29.  
  30. Arrays.fill(d, -1);
  31. d[s] = 0;
  32.  
  33. // System.out.println(Arrays.toString(d));
  34.  
  35. for (int i = 0; i < n; i++) {
  36.  
  37. Integer x = null;
  38.  
  39. for (int j = 0; j < n; j++) {
  40. if (!marked[j] && (x == null || d[j] < d[x]) && d[j] != -1) {
  41. x = j;
  42. }
  43. }
  44.  
  45. System.out.println(x);
  46.  
  47. if (x == null || d[x] == -1) break;
  48.  
  49. marked[x] = true;
  50.  
  51. for (int j = 0; j < n; j++) {
  52. if (edges[x][j] != -1 && edges[x][j] != 0) {
  53. if (d[j] == -1 || d[x] + edges[x][j] < d[j]) {
  54. d[j] = d[x] + edges[x][j];
  55. }
  56. }
  57. }
  58. // System.out.println(Arrays.toString(d));
  59.  
  60. }
  61.  
  62. out.println(d[f]);
  63.  
  64. }
  65.  
  66. class Edge implements Comparable<Edge> {
  67.  
  68. int weight;
  69. int a, b;
  70.  
  71. Edge(int a, int b, int weight) {
  72. this.a = a - 1;
  73. this.b = b - 1;
  74. this.weight = weight;
  75. }
  76.  
  77. @Override
  78. public int compareTo(Edge o) {
  79. return Integer.compare(weight, o.weight);
  80. }
  81. }
  82.  
  83. public void run() {
  84. try {
  85. in = new FastScanner(new File("pathmgep.in"));
  86. out = new PrintWriter(new File("pathmgep.out"));
  87.  
  88. solve();
  89.  
  90. out.close();
  91. } catch (IOException e) {
  92. e.printStackTrace();
  93. }
  94. }
  95.  
  96. public static void main(String[] args) throws FileNotFoundException {
  97. new TaskB().run();
  98. }
  99.  
  100. class FastScanner {
  101. BufferedReader br;
  102. StringTokenizer st;
  103. String str;
  104.  
  105. FastScanner(File f) {
  106. try {
  107. br = new BufferedReader(new FileReader(f));
  108. } catch (FileNotFoundException e) {
  109. e.printStackTrace();
  110. }
  111. }
  112.  
  113. String next() {
  114. while (st == null || !st.hasMoreTokens()) {
  115. try {
  116. if ((str = br.readLine()) == null) return null;
  117. st = new StringTokenizer(str);
  118. } catch (Exception e) {
  119. e.printStackTrace();
  120. return null;
  121. }
  122. }
  123. return st.nextToken();
  124. }
  125.  
  126. int nextInt() {
  127. return Integer.parseInt(next());
  128. }
  129.  
  130. long nextLong() {
  131. return Long.parseLong(next());
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement