1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| int min = Integer.MAX_VALUE; public void DFS(int [][] flights, int src, int dst, int K, int v, int[] visit) { if(src==dst && v<min) { min = v; return; } if(K<0||v>min) return; for(int i=0;i<flights.length;i++) { if(visit[src]==0 && flights[i][0]==src) { visit[src] = 1; DFS(flights, flights[i][1], dst, K-1, v+flights[i][2], visit); visit[src] = 0; } } }
public int findCheapestPrice(int n, int[][] f, int src, int dst, int K) { int[] visit = new int[n]; DFS(f, src, dst, K, 0, visit); if(min == Integer.MAX_VALUE) return -1; return min; }
|