Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- import java.math.*;
- public class Main {
- private BufferedReader in;
- private PrintWriter out;
- private StringTokenizer st;
- private Random rnd;
- public void solve() throws IOException {
- int n = nextInt(), m = nextInt();
- int[][] g = new int[n][n];
- for(int i = 0; i < n; i++) Arrays.fill(g[i], -1);
- for(int i = 0; i < m; i++) g[nextInt() - 1][nextInt() - 1] = nextInt();
- int[] d = new int[n];
- Arrays.fill(d, Integer.MAX_VALUE / m);
- d[0] = 0;
- int[] id = new int[n];
- ArrayDeque<Integer> q = new ArrayDeque<Integer>();
- q.add(0);
- while(q.size() > 0) {
- int u = q.pollFirst();
- out.println(u);
- out.flush();
- id[u] = 1;
- for(int v = 0; v < n; v++) {
- if(g[u][v] == -1) continue;
- if(d[v] > d[u] + g[u][v]) {
- d[v] = d[u] + g[u][v];
- if(id[v] == 0) {
- q.addLast(v);
- } else if(id[v] == 1) {
- q.addFirst(v);
- }
- id[v] = 1;
- }
- }
- }
- }
- public static void main(String[] args) {
- new Main().run();
- }
- public void run() {
- try {
- //in = new BufferedReader(new FileReader("bikers.in"));
- //out = new PrintWriter(new FileWriter("bikers.out"));
- in = new BufferedReader(new InputStreamReader((System.in)));
- out = new PrintWriter(System.out);
- st = null;
- rnd = new Random();
- solve();
- out.close();
- } catch(IOException e) {
- e.printStackTrace();
- }
- }
- private String nextToken() throws IOException, NullPointerException {
- while(st == null || !st.hasMoreTokens()) {
- st = new StringTokenizer(in.readLine());
- }
- return st.nextToken();
- }
- private int nextInt() throws IOException {
- return Integer.parseInt(nextToken());
- }
- private long nextLong() throws IOException {
- return Long.parseLong(nextToken());
- }
- private double nextDouble() throws IOException {
- return Double.parseDouble(nextToken());
- }
- }
Add Comment
Please, Sign In to add comment