This commit is contained in:
huangge1199@hotmail.com 2021-07-02 19:55:23 +08:00
parent 485f342ca9
commit 00c8b3dbbe
2 changed files with 78 additions and 38 deletions

View File

@ -43,10 +43,7 @@
package leetcode.editor.cn; package leetcode.editor.cn;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//815:公交路线 //815:公交路线
public class BusRoutes { public class BusRoutes {
@ -59,45 +56,89 @@ public class BusRoutes {
//力扣代码 //力扣代码
//leetcode submit region begin(Prohibit modification and deletion) //leetcode submit region begin(Prohibit modification and deletion)
class Solution { class Solution {
// public int numBusesToDestination(int[][] routes, int source, int target) {
// Map<Integer, List<Integer>> site = new HashMap<>();
// Map<Integer, List<Integer>> bus = new HashMap<>();
// for (int i = 0; i < routes.length; i++) {
// List<Integer> busList = new ArrayList<>();
// for (int j = 0; j < routes[i].length; j++) {
// List<Integer> list = site.getOrDefault(routes[i][j], new ArrayList<>());
// list.add(i);
// site.put(routes[i][j], list);
// busList.add(routes[i][j]);
// }
// bus.put(i, busList);
// }
// int count = minCount(site, bus, source, target, 0, new ArrayList<>());
// return count == Integer.MAX_VALUE ? -1 : count;
// }
//
// private int minCount(Map<Integer, List<Integer>> map, Map<Integer, List<Integer>> bus, int source, int target, int count, List<Integer> use) {
// if (source == target) {
// return count;
// }
// if (map.get(source).size() == 1 && use.contains(map.get(source).get(0))) {
// return Integer.MAX_VALUE;
// }
// int min = Integer.MAX_VALUE;
// for (int busIndex : map.get(source)) {
// if (use.contains(busIndex)) {
// continue;
// }
// for (int stata : bus.get(busIndex)) {
// if (stata == source) {
// continue;
// }
// use.add(busIndex);
// min = Math.min(min, minCount(map, bus, stata, target, count + 1, use));
// use.remove(use.size() - 1);
// }
// }
// return min;
// }
public int numBusesToDestination(int[][] routes, int source, int target) { public int numBusesToDestination(int[][] routes, int source, int target) {
Map<Integer, List<Integer>> site = new HashMap<>(); if (source == target) {
Map<Integer, List<Integer>> bus = new HashMap<>(); return 0;
for (int i = 0; i < routes.length; i++) {
List<Integer> busList = new ArrayList<>();
for (int j = 0; j < routes[i].length; j++) {
List<Integer> list = site.getOrDefault(routes[i][j], new ArrayList<>());
list.add(i);
site.put(routes[i][j], list);
busList.add(routes[i][j]);
}
bus.put(i, busList);
}
int count = minCount(site, bus, source, target, 0, new ArrayList<>());
return count == Integer.MAX_VALUE ? -1 : count;
} }
private int minCount(Map<Integer, List<Integer>> map, Map<Integer, List<Integer>> bus, int source, int target, int count, List<Integer> use) { int n = routes.length;
if (source == target) { boolean[][] edge = new boolean[n][n];
return count; Map<Integer, List<Integer>> rec = new HashMap<Integer, List<Integer>>();
for (int i = 0; i < n; i++) {
for (int site : routes[i]) {
List<Integer> list = rec.getOrDefault(site, new ArrayList<Integer>());
for (int j : list) {
edge[i][j] = edge[j][i] = true;
} }
if (map.get(source).size() == 1 && use.contains(map.get(source).get(0))) { list.add(i);
return Integer.MAX_VALUE; rec.put(site, list);
}
int min = Integer.MAX_VALUE;
for (int busIndex : map.get(source)) {
if (use.contains(busIndex)) {
continue;
}
for (int stata : bus.get(busIndex)) {
if (stata == source) {
continue;
}
use.add(busIndex);
min = Math.min(min, minCount(map, bus, stata, target, count + 1, use));
use.remove(use.size() - 1);
} }
} }
return min;
int[] dis = new int[n];
Arrays.fill(dis, -1);
Queue<Integer> que = new LinkedList<Integer>();
for (int bus : rec.getOrDefault(source, new ArrayList<Integer>())) {
dis[bus] = 1;
que.offer(bus);
}
while (!que.isEmpty()) {
int x = que.poll();
for (int y = 0; y < n; y++) {
if (edge[x][y] && dis[y] == -1) {
dis[y] = dis[x] + 1;
que.offer(y);
}
}
}
int ret = Integer.MAX_VALUE;
for (int bus : rec.getOrDefault(target, new ArrayList<Integer>())) {
if (dis[bus] != -1) {
ret = Math.min(ret, dis[bus]);
}
}
return ret == Integer.MAX_VALUE ? -1 : ret;
} }
} }
//leetcode submit region end(Prohibit modification and deletion) //leetcode submit region end(Prohibit modification and deletion)

File diff suppressed because one or more lines are too long