Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main implements Runnable {
  5.    
  6.     public void solution() throws IOException {
  7.         while (true) {
  8.             int n = in.nextInt();
  9.             if (n == -1) {
  10.                 break;
  11.             }
  12.             int m = in.nextInt();
  13.             int[][] adj = new int[n][n];
  14.             for (int i = 0; i < m; ++i) {
  15.                 int x = in.nextInt() - 1;
  16.                 int y = in.nextInt() - 1;
  17.                 int cost = in.nextInt();
  18.                 if (adj[x][y] != 0) {
  19.                     cost = Math.min(cost, adj[x][y]);
  20.                 }
  21.                 adj[x][y] = cost;
  22.                 adj[y][x] = cost;
  23.             }
  24.        
  25.             int minValue = Integer.MAX_VALUE;
  26.             List<Integer> path = null;
  27.             for (int i = 0; i < n; ++i) {
  28.                 for (int j = i + 1; j < n; ++j) {
  29.                     if (adj[i][j] != 0) {
  30.                         int tmp = adj[i][j];
  31.                         adj[i][j] = adj[j][i] = 0;
  32.                         List<Integer> curPath = restorePath(i, j, adj);
  33.                         if (curPath != null) {
  34.                             int value = getValue(curPath, adj) + tmp;
  35.                             if (value < minValue) {
  36.                                 path = curPath;
  37.                                 minValue = value;
  38.                             }
  39.                         }
  40.                         adj[i][j] = adj[j][i] = tmp;
  41.                     }
  42.                 }
  43.             }
  44.            
  45.             //debug(dist);
  46.             //debug(first, second);
  47.        
  48.             if (path == null) {
  49.                 out.println("No solution.");
  50.             } else {
  51.                 for (int i = 0; i < path.size(); ++i) {
  52.                     if (i != 0) {
  53.                         out.print(' ');
  54.                     }
  55.                     out.print(path.get(i) + 1);
  56.                 }
  57.                 out.println();
  58.             }
  59.         }
  60.     }
  61.    
  62.     private int getValue(List<Integer> path, int[][] adj) {
  63.         int res = 0;
  64.         int prev = path.get(0);
  65.         for (int i = 1; i < path.size(); ++i) {
  66.             res += adj[prev][path.get(i)];
  67.             prev = path.get(i);
  68.         }
  69.         return res;
  70.     }
  71.    
  72.     private class State implements Comparable<State> {
  73.         int v;
  74.         int dist;
  75.        
  76.         public State() { }
  77.         public State(int v, int dist) {
  78.             this.v = v;
  79.             this.dist = dist;
  80.         }
  81.        
  82.         @Override
  83.         public int compareTo(State other) {
  84.             return dist - other.dist;
  85.         }
  86.     }
  87.    
  88.     private List<Integer> restorePath(int s, int t, int[][] adj) {
  89.         int[] dist = new int[adj.length];
  90.         int[] prev = new int[adj.length];
  91.        
  92.         Arrays.fill(dist, Integer.MAX_VALUE);
  93.         Arrays.fill(prev, -1);
  94.        
  95.         dist[s] = 0;
  96.        
  97.         PriorityQueue<State> q = new PriorityQueue<State>();
  98.         q.add(new State(s, 0));
  99.        
  100.         while (!q.isEmpty()) {
  101.             State cur = q.poll();
  102.             if (cur.v == t) {
  103.                 break;
  104.             }
  105.             for (int next = 0; next < adj[cur.v].length; ++next) {
  106.                 if (adj[cur.v][next] != 0 && dist[next] > adj[cur.v][next] + cur.dist) {
  107.                     dist[next] = adj[cur.v][next] + cur.dist;
  108.                     q.add(new State(next, dist[next]));
  109.                     prev[next] = cur.v;
  110.                 }
  111.             }
  112.         }
  113.        
  114.         if (prev[t] == -1) {
  115.             return null;
  116.         }
  117.        
  118.         List<Integer> res = new ArrayList<Integer>();
  119.         int x = t;
  120.         while (x >= 0) {
  121.             res.add(x);
  122.             x = prev[x];
  123.         }
  124.        
  125.         Collections.reverse(res);
  126.         return res;
  127.     }
  128.    
  129.     public void debug(Object... o) {
  130.         System.err.println(Arrays.deepToString(o));
  131.     }
  132.  
  133.     @Override
  134.     public void run() {
  135.         try {
  136.             String fileName = "";
  137.             try {
  138.                 in = new Scanner(new FileReader(fileName + ".in"));
  139.                 out = new PrintWriter(fileName + ".out");
  140.             } catch (Exception e) {
  141.             }
  142.             solution();
  143.             in.reader.close();
  144.             out.close();
  145.         } catch(Exception e) {
  146.             e.printStackTrace();
  147.             System.exit(1);
  148.         }
  149.     }
  150.    
  151.     private class Scanner {
  152.         StringTokenizer tokenizer;
  153.         BufferedReader reader;
  154.        
  155.         public Scanner(Reader reader) {
  156.             this.reader = new BufferedReader(reader);
  157.             this.tokenizer = new StringTokenizer("");
  158.         }
  159.        
  160.         public boolean hasNext() throws IOException {
  161.             while (!tokenizer.hasMoreTokens()) {
  162.                 String line = reader.readLine();
  163.                 if (line == null) {
  164.                     return false;
  165.                 }
  166.                 tokenizer = new StringTokenizer(line);
  167.             }
  168.             return true;
  169.         }
  170.        
  171.         public String next() throws IOException {
  172.             hasNext();
  173.             return tokenizer.nextToken();
  174.         }
  175.        
  176.         public String nextLine() throws IOException {
  177.             tokenizer = new StringTokenizer("");
  178.             return reader.readLine();
  179.         }
  180.        
  181.         public int nextInt() throws IOException {
  182.             return Integer.parseInt(next());
  183.         }
  184.        
  185.         public long nextLong() throws IOException {
  186.             return Long.parseLong(next());
  187.         }
  188.        
  189.         public double nextDouble() throws IOException {
  190.             return Double.parseDouble(next());
  191.         }
  192.     }
  193.    
  194.     public static void main(String[] args) {
  195.         new Thread(null, new Main(), "", 1 << 28).start();
  196.     }
  197.        
  198.     private Scanner in = new Scanner(new InputStreamReader(System.in));
  199.     private PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement