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