public void solve(InputReader in, OutputWriter out) { int n = in.nextInt(); int[][] g = new int[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { g[i][j] = in.nextInt(); } } Queue<int[]> queue = new PriorityQueue<int[]>( 1, new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[1]; } }); queue.add(new int[] {0, 0}); boolean[] visited = new boolean[n]; int res = 0; while (!queue.isEmpty()) { int[] head = queue.remove(); int node = head[0]; int dist = head[1]; if (!visited[node]) { res += dist; visited[node] = true; for (int i = 0; i < n; i++) { if (!visited[i]) { queue.add(new int[] {i, g[node][i]}); } } } } out.println(res); }
// finds the max prime number found last time computeTo was called //cycle through the queue and grab the last element public int getMax() { int max=0; int value=0; // say queuePrime is 2 3 5 7 11 for(int i=0; i<queuePrime.getsize(); i++) { value=(int)queuePrime.dequeue(i); //2 , 3, 5, 7, 11 queueMax.enqueue(value); //2, 3, 5, 7, 11 if( i= queuePrime.getsize()-1) { max=value; } } queuePrime= queueMax; return max; }
//computes all primes up to some n and store them the queue public void computeTo(int n) { // add int 2:n into queue int i=2; int count=0; int value1=0; int value2=0; int size=0; int whilesize=(int)Math.sqrt(n); while(i<=n)// n=10 { queueNumbers.enqueue(i);//2 3 4 5 6 7 8 9 10 i++; } do{ value1= (int)queueNumbers.dequeue(); //value1=2, queuePrime.enqueue(value1);// 2 for (int count=0; count<queueNumbers.getsize(); count++)// 0-10 { value2=(int)queueNumber.dequeue();// 3 if(value2%value1 !=0)//3%2 !=0 { queueNumbers.enqueue(value2);// store 3 in queueNumbers, } } }// end do while(value2 <= whilesize) { size= queueNumbers.getsize(); for(int k=0; i<size; i++) { queueNumbers.dequeue(value2); queuePrime.enqueue(value2); } }// end while /** create a queue and fill it with the consecutive integers 2 through n inclusive. create an empty queue to store primes. do { obtain the next prime by removing the first value in the queue of numbers. put the next prime into the queue of primes. go through the queue of numbers, eliminating numbers divisible by the next prime. } while (the next prime < sqrt(n)) all remaining values in numbers queue are prime, so transfer them to primes queue */ }
public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int N = sc.nextInt(); N > 0; N = sc.nextInt()) { int M = sc.nextInt(); int L = sc.nextInt(); boolean[][] map = new boolean[N][N]; int[][] D = new int[N][N]; int[][] E = new int[N][N]; while (M-- > 0) { int A = sc.nextInt() - 1; int B = sc.nextInt() - 1; map[A][B] = map[B][A] = true; D[A][B] = D[B][A] = sc.nextInt(); E[A][B] = E[B][A] = sc.nextInt(); } boolean[][] visited = new boolean[N][L + 1]; Queue<Edge> q = new PriorityQueue<Edge>(); q.offer(new Edge(0, 0, L)); while (!q.isEmpty()) { Edge e = q.poll(); if (visited[e.to][e.money]) continue; visited[e.to][e.money] = true; if (e.to == N - 1) { System.out.println(e.enemy); break; } for (int i = 0; i < N; i++) if (map[e.to][i]) { if (D[e.to][i] <= e.money) q.offer(new Edge(e.enemy, i, e.money - D[e.to][i])); q.offer(new Edge(e.enemy + E[e.to][i], i, e.money)); } } } }
public int printSmiles(int smiles) { boolean[][] visited = new boolean[2001][2001]; Queue<Integer> posQ = new LinkedList<>(); Queue<Integer> copyQ = new LinkedList<>(); Queue<Integer> countQ = new LinkedList<>(); posQ.add(1); copyQ.add(0); countQ.add(0); while (true) { int pos = posQ.poll(); int copy = copyQ.poll(); if (pos == smiles) break; int count = countQ.poll(); if (visited[pos][copy]) continue; visited[pos][copy] = true; if (pos < smiles) { // copy posQ.add(pos); copyQ.add(pos); countQ.add(count + 1); // paste posQ.add(pos + copy); copyQ.add(copy); countQ.add(count + 1); } // delete if (pos - 1 >= 0) { posQ.add(pos - 1); copyQ.add(copy); countQ.add(count + 1); } } return countQ.poll(); }
public Main() { try { in = new BufferedReader(new InputStreamReader(System.in)); // minimum distance from D to K int numCities = nextInt(); int tradeRoutes = nextInt(); int[][] adjacencyMatrix = new int[numCities][numCities]; int[] minDistance = new int[numCities]; Arrays.fill(minDistance, 100000000); // Arrays.fill(adjacencyMatrix, -1); // int [] pencilCosts = new int[ Node[] cities = new Node[numCities]; for (int x = 0; x < tradeRoutes; x++) { int cityA = nextInt() - 1; int cityB = nextInt() - 1; int cost = nextInt(); if (cities[cityA] == null) cities[cityA] = new Node(cityA); if (cities[cityB] == null) cities[cityB] = new Node(cityB); adjacencyMatrix[cityA][cityB] = cost; adjacencyMatrix[cityB][cityA] = cost; // cities[cityA].routes.add(new Edge(cost, cities[cityB])); // cities[cityB].routes.add(new Edge(cost, cities[cityA])); } int numStores = nextInt(); int[] pencilCosts = new int[numCities]; Arrays.fill(pencilCosts, -1); for (int x = 0; x < numStores; x++) { int ID = nextInt() - 1; int cost = nextInt(); pencilCosts[ID] = cost; } int destination = nextInt() - 1; // if (isGood[destination]){ // } int minCost = 100000000; Queue<Node> Q = new LinkedList<Node>(); // PriorityQueue<Node> Q = new PriorityQueue<Node>(); minDistance[destination] = 0; // cities[destination].distance = 0; Q.offer(cities[destination]); while (!Q.isEmpty()) { Node temp = Q.poll(); for (int x = 0; x < numCities; x++) { if (adjacencyMatrix[temp.ID][x] != 0 && (minDistance[x] == 100000000 || minDistance[x] > minDistance[temp.ID] + adjacencyMatrix[temp.ID][x])) { minDistance[x] = minDistance[temp.ID] + adjacencyMatrix[temp.ID][x]; if (pencilCosts[x] != -1 && minDistance[x] < minCost) { // System.out.println(minCost); minCost = Math.min(minDistance[x] + pencilCosts[x], minCost); Q.offer(cities[x]); } else { if (pencilCosts[x] == -1) { // why> Q.offer(cities[x]); } } // Q.offer(temp.routes.get(x).destination); } } } for (int x = 0; x < numCities; x++) { if (pencilCosts[x] != -1 && pencilCosts[x] + minDistance[x] < minCost && minDistance[x] != 100000000) { minCost = minDistance[x] + pencilCosts[x]; } } System.out.println(minCost); } catch (IOException e) { System.out.println("IO: General"); } }