Cheapest Flights Within K Stops

在这里插入图片描述

 public static void main(String[] args) {
        int n=4;
        int src = 0, dst = 2, k = 1;
        int[][] fights={{0,1,100},{1,2,300},{1,2,100},{0,2,500}};
        Test9 test9=new Test9();
        int cheapestPrice = test9.findCheapestPrice(n, fights, src, dst, k);
        System.out.println(cheapestPrice);
    }

    //深度优先
  /*  Map<Integer, List<int[]>> graph;
    int ans = Integer.MAX_VALUE;

    public int findCheapestPrice(int N, int[][] flights, int src, int dst, int K) {
        //构建图 u->v w
        graph = new HashMap<>();
        for (int[] f : flights) {
            graph.putIfAbsent(f[0], new ArrayList<>());
            graph.get(f[0]).add(new int[]{f[1], f[2]});
        }
        dfs(src, dst, K + 1, 0);
        return ans == Integer.MAX_VALUE ? -1 : ans;
    }

  private void dfs(int src, int dst, int K, int dist) {
        if (K < 0) return;
        if (src == dst) {
            ans = dist;
            return;
        }
        if (graph.containsKey(src)) {
            for (int[] f : graph.get(src)) {
                int u = f[0], w = f[1];
                if (dist + w > ans) continue;
                dfs(u, dst, K - 1, dist + w);
            }
        }
    }*/
   //广度优先
  public int findCheapestPrice(int N, int[][] flights, int src, int dst, int K) {
      Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
      for (int[] f : flights) {
          graph.putIfAbsent(f[0], new HashMap<>());
          graph.get(f[0]).put(f[1], f[2]);
      }
      //最小堆   a[0] 为消费
      Queue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] - b[0]));
      pq.offer(new int[]{0, src, K + 1}); //存的依次是到达当前点v的距离,当前点v,已经经过的站数
      while (!pq.isEmpty()) {
          int[] curr = pq.poll();
          int dist = curr[0], u = curr[1], stops = curr[2];
          if (u == dst) return dist;
          if (stops > 0) {
              if (graph.containsKey(u)) {
                  for (Map.Entry<Integer, Integer> e : graph.get(u).entrySet()) {
                      int v = e.getKey(), w = e.getValue();
                      pq.offer(new int[]{dist + w, v, stops - 1});
                  }
              }
          }

      }
      return -1;
  }
//动态规划
    /*//[t][i] 表示通过恰好 t 次航班,从出发城市src 到达城市 i 需要的最小花费
    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
        final int INF = 10000 * 101 + 1;
        int[][] f = new int[k + 2][n];
        for (int i = 0; i < k + 2; ++i) {
            Arrays.fill(f[i], INF);
        }
        f[0][src] = 0;
        for (int t = 1; t <= k + 1; ++t) {
            for (int[] flight : flights) {
                /飞机从j-i 花费 cost
                int j = flight[0], i = flight[1], cost = flight[2];
                f[t][i] = Math.min(f[t][i], f[t - 1][j] + cost);
            }
        }
        int ans = INF;
        for (int t = 1; t <= k + 1; ++t) {
            ans = Math.min(ans, f[t][dst]);
        }
        return ans == INF ? -1 : ans;
    }*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值