import java.io.*; import java.util.*; import java.math.*; /** * Solution to Gold Bandits * * @author vanb */ public class gold_vanb { public Scanner sc; public PrintStream ps; /** * A State of a Dijkstra-style search. * * @author vanb */ public class State implements Comparable { /** The node, the total distance form node 0, and the accumulated wealth. */ public int node, distance, wealth; /** The state we came from. */ public State from; public State( int node, int distance, int wealth, State from ) { this.node = node; this.distance = distance; this.wealth = wealth; this.from = from; } @Override /** * Compare two States, first by distance (increasing), then by wealth (decreasing). * * @param s Another State to compare with * @return The usual for compareTo */ public int compareTo( State s ) { int diff = distance-s.distance; if( diff==0 ) diff = s.wealth - wealth; return diff; } } /** * Driver. * @throws Exception */ public void doit() throws Exception { sc = new Scanner( System.in ); //new File( "gold.in" ) ); ps = System.out; //new PrintStream( new FileOutputStream( "gold.vanb" ) ); // The Gold in each village int gold[] = new int[40]; // The system of roads boolean road[][] = new boolean[40][40]; // Whether or not a node has been visited in the current search boolean visited[] = new boolean[40]; // The number of nodes int n; // A priority queue for the Dijkstras PriorityQueue pq = new PriorityQueue(); for(;;) { n = sc.nextInt(); int m = sc.nextInt(); if( n==0 && m==0 ) break; for( int i=0; i