Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class Main implements Runnable {
- public void solution() throws IOException {
- while (true) {
- int n = in.nextInt();
- if (n == -1) {
- break;
- }
- int m = in.nextInt();
- int[][] adj = new int[n][n];
- for (int i = 0; i < m; ++i) {
- int x = in.nextInt() - 1;
- int y = in.nextInt() - 1;
- int cost = in.nextInt();
- if (adj[x][y] != 0) {
- cost = Math.min(cost, adj[x][y]);
- }
- adj[x][y] = cost;
- adj[y][x] = cost;
- }
- int minValue = Integer.MAX_VALUE;
- List<Integer> path = null;
- for (int i = 0; i < n; ++i) {
- for (int j = i + 1; j < n; ++j) {
- if (adj[i][j] != 0) {
- int tmp = adj[i][j];
- adj[i][j] = adj[j][i] = 0;
- List<Integer> curPath = restorePath(i, j, adj);
- if (curPath != null) {
- int value = getValue(curPath, adj) + tmp;
- if (value < minValue) {
- path = curPath;
- minValue = value;
- }
- }
- adj[i][j] = adj[j][i] = tmp;
- }
- }
- }
- //debug(dist);
- //debug(first, second);
- if (path == null) {
- out.println("No solution.");
- } else {
- for (int i = 0; i < path.size(); ++i) {
- if (i != 0) {
- out.print(' ');
- }
- out.print(path.get(i) + 1);
- }
- out.println();
- }
- }
- }
- private int getValue(List<Integer> path, int[][] adj) {
- int res = 0;
- int prev = path.get(0);
- for (int i = 1; i < path.size(); ++i) {
- res += adj[prev][path.get(i)];
- prev = path.get(i);
- }
- return res;
- }
- private class State implements Comparable<State> {
- int v;
- int dist;
- public State() { }
- public State(int v, int dist) {
- this.v = v;
- this.dist = dist;
- }
- @Override
- public int compareTo(State other) {
- return dist - other.dist;
- }
- }
- private List<Integer> restorePath(int s, int t, int[][] adj) {
- int[] dist = new int[adj.length];
- int[] prev = new int[adj.length];
- Arrays.fill(dist, Integer.MAX_VALUE);
- Arrays.fill(prev, -1);
- dist[s] = 0;
- PriorityQueue<State> q = new PriorityQueue<State>();
- q.add(new State(s, 0));
- while (!q.isEmpty()) {
- State cur = q.poll();
- if (cur.v == t) {
- break;
- }
- for (int next = 0; next < adj[cur.v].length; ++next) {
- if (adj[cur.v][next] != 0 && dist[next] > adj[cur.v][next] + cur.dist) {
- dist[next] = adj[cur.v][next] + cur.dist;
- q.add(new State(next, dist[next]));
- prev[next] = cur.v;
- }
- }
- }
- if (prev[t] == -1) {
- return null;
- }
- List<Integer> res = new ArrayList<Integer>();
- int x = t;
- while (x >= 0) {
- res.add(x);
- x = prev[x];
- }
- Collections.reverse(res);
- return res;
- }
- public void debug(Object... o) {
- System.err.println(Arrays.deepToString(o));
- }
- @Override
- public void run() {
- try {
- String fileName = "";
- try {
- in = new Scanner(new FileReader(fileName + ".in"));
- out = new PrintWriter(fileName + ".out");
- } catch (Exception e) {
- }
- solution();
- in.reader.close();
- out.close();
- } catch(Exception e) {
- e.printStackTrace();
- System.exit(1);
- }
- }
- private class Scanner {
- StringTokenizer tokenizer;
- BufferedReader reader;
- public Scanner(Reader reader) {
- this.reader = new BufferedReader(reader);
- this.tokenizer = new StringTokenizer("");
- }
- public boolean hasNext() throws IOException {
- while (!tokenizer.hasMoreTokens()) {
- String line = reader.readLine();
- if (line == null) {
- return false;
- }
- tokenizer = new StringTokenizer(line);
- }
- return true;
- }
- public String next() throws IOException {
- hasNext();
- return tokenizer.nextToken();
- }
- public String nextLine() throws IOException {
- tokenizer = new StringTokenizer("");
- return reader.readLine();
- }
- public int nextInt() throws IOException {
- return Integer.parseInt(next());
- }
- public long nextLong() throws IOException {
- return Long.parseLong(next());
- }
- public double nextDouble() throws IOException {
- return Double.parseDouble(next());
- }
- }
- public static void main(String[] args) {
- new Thread(null, new Main(), "", 1 << 28).start();
- }
- private Scanner in = new Scanner(new InputStreamReader(System.in));
- private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement