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 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 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"); } }