10.4 新鲜OA 求米!!!!!
如地理所说,
我还准备了常见A->C->B的代码但没用上,而且不一定保证对,放在下面要是有人需要自取,求米!!!!!!!!- [hide=188<img src="https://www.1point3acres.com/bbs/forum.php?mod=image&aid=477382&size=300x300&key=ddaecc605bdc30ad&nocache=yes&type=fixnone" border="0" aid="attachimg_477382" alt=""><img src="https://www.1point3acres.com/bbs/forum.php?mod=image&aid=477383&size=300x300&key=e9522bef61b98f12&nocache=yes&type=fixnone" border="0" aid="attachimg_477383" alt=""><img src="https://www.1point3acres.com/bbs/forum.php?mod=image&aid=477384&size=300x300&key=e1c7e1631d7def91&nocache=yes&type=fixnone" border="0" aid="attachimg_477384" alt="">]import java.util.*;
- public class DoorDash {
- public static long getShortestRoute(int roadsNotes, List<Integer> roadsFrom, List<Integer> roadsTo, List<Integer> roadsWeight, int a, int b, int c) {
- int[][] adjMatrix = new int[roadsNotes][roadsNotes];
- for(int i = 0; i < roadsFrom.size(); i++) {
- adjMatrix[roadsFrom.get(i)][roadsTo.get(i)] = roadsWeight.get(i);
- adjMatrix[roadsTo.get(i)][roadsFrom.get(i)] = roadsWeight.get(i);
- }
- HashSet<Integer> set = new HashSet<>();
- long l1 = dijkstra(adjMatrix,set, a, c);
- System.out.println(set);
- long l2 = Long.MAX_VALUE;
- for(int node : set) {
- l2 = Math.min(l2, dijkstra(adjMatrix, node, b));
- }
- System.out.println(l1);
- System.out.println(l2);
- if(l1 == Long.MAX_VALUE || l2 == Long.MAX_VALUE) {
- return -1;
- }
- return l1 + l2;
- }
- public static long dijkstra(int[][] matrix, int from, int to) {
- long[] d = new long[matrix.length];
- for(int i = 0; i < d.length; i++) {
- d[i] = Long.MAX_VALUE;
- }
- d[from] = 0;
- PriorityQueue<long[]> pq = new PriorityQueue<>((a,b) -> (int) (a[1] - b[1]));
- pq.offer(new long[]{from, d[from]});
- while(!pq.isEmpty()) {
- long[] currNode = pq.poll();
- int node = (int)currNode[0];
- long distance = currNode[1];
- if(node == to) {
- return distance;
- }
- for(int i = 0; i < matrix.length; i++) {
- if(matrix[node][i] > 0) {
- long dU = distance;
- long dV = d[i];
- int wUV = matrix[node][i];
- if(dU + (long)wUV < dV) {
- d[i] = dU + (long)wUV;
- pq.offer(new long[]{i, d[i]});
- }
- }
- }
- }
- return d[to];
- }
- public static long dijkstra(int[][] matrix,HashSet<Integer> set, int from, int to) {
- long[] d = new long[matrix.length];
- for(int i = 0; i < d.length; i++) {
- d[i] = Long.MAX_VALUE;
- }
- d[from] = 0;
- PriorityQueue<long[]> pq = new PriorityQueue<>((a,b) -> (int) (a[1] - b[1]));
- pq.offer(new long[]{from, d[from]});
- set.add(from);
- while(!pq.isEmpty()) {
- long[] currNode = pq.poll();
- int node = (int)currNode[0];
- long distance = currNode[1];
- set.add(node);
- if(node == to) {
- return distance;
- }
- for(int i = 0; i < matrix.length; i++) {
- if(matrix[node][i] > 0) {
- long dU = distance;
- long dV = d[i];
- int wUV = matrix[node][i];
- if(dU + (long)wUV < dV) {
- d[i] = dU + (long)wUV;
- pq.offer(new long[]{i, d[i]});
- }
- }
- }
- }
- return d[to];
- }
- public static void main(String[] args) {
- List<Integer> from = new ArrayList<>();
- from.add(0);
- from.add(0);
- from.add(0);
- List<Integer> to = new ArrayList<>();
- to.add(2);
- to.add(1);
- to.add(3);
- List<Integer> weight = new ArrayList<>();
- weight.add(1);
- weight.add(2);
- weight.add(3);
- long d = getShortestRoute(4, from, to, weight, 1, 3, 2);
- System.out.println(d);
- }
- }[/hide]
复制代码 |