【力扣leetcode】-787. K站中轉內最便宜的航班

  • 2021 年 8 月 25 日
  • 筆記

題目描述:

有 n 個城市通過一些航班連接。給你一個數組 flights ,其中 flights[i] = [fromi, toi, pricei] ,表示該航班都從城市 fromi 開始,以價格 pricei 抵達 toi。

現在給定所有的城市和航班,以及出發城市 src 和目的地 dst,你的任務是找到出一條最多經過 k 站中轉的路線,使得從 src 到 dst 的 價格最便宜 ,並返回該價格。 如果不存在這樣的路線,則輸出 -1。

來源:力扣(LeetCode)
鏈接://leetcode-cn.com/problems/cheapest-flights-within-k-stops
著作權歸領扣網路所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。


package leetcode;

import java.util.ArrayList;
import java.util.List;

/* 
Author:Samba
Time :2021年8月24日
Data:下午10:17:04
*/
//K 站中轉內最便宜的航班
public class t787 {
    public static void main(String[] args) {
        Solution787 S = new Solution787();
        int[][] test = {{3,4,4},{2,5,6},{4,7,10},{9,6,5},{7,4,4},{6,2,10},{6,8,6},{7,9,4},{1,5,4},{1,0,4},{9,7,3},{7,0,5},{6,5,8},{1,7,6},{4,0,9},{5,9,1},{8,7,3},{1,2,6},{4,1,5},{5,2,4},{1,9,1},{7,8,10},{0,4,2},{7,2,8}}
        ;
        int result = S.findCheapestPrice(100,test,6, 0, 7);
        System.out.println(result);
    }
}

class Solution787 {
    private int minResult;
    private int[][] flights;
    private int n;
    private int k;
    private int dst;
    //判斷是否已經抵達過
    public static boolean isArrived(int Node,List<Integer> arrivedNodes) {
        boolean flag = false;
        for(int i:arrivedNodes) {
            if(i==Node) flag = true;
        }
        return flag;    
    }
    //起始地點為s且到達地點沒有飛過的數組
    public List<int[]> fromS(int s,List<Integer> arrivedNodes) {
        List<int[]> result = new ArrayList<int[]>();
        for(int[] flight:flights) {
            if(flight[0] == s&&!isArrived(flight[1],arrivedNodes)) {
                result.add(flight);
            }
        }
        return result;
    }
    public int findCheapestPrice(int n, int[][] flights, int src, int dst, int k) {
        this.flights = flights;
        this.n = n;
        this.k = k;
        this.dst = dst;
        this.minResult = Integer.MAX_VALUE;
        List<Integer> arrivedNodes = new ArrayList<Integer>();
        int result = findNext(src,0,0,arrivedNodes);
        return result==Integer.MAX_VALUE?-1:result;
    }
    
    public int findNext(int src, int value,int time,List<Integer> arrivedNodes) {
        if(value>=this.minResult) {
            return Integer.MAX_VALUE;
        }
        if(src == dst) {
            this.minResult = this.minResult>value?value:this.minResult;
            return value;
        }
        if(time>k) {
            //超過了k條的限制
            return Integer.MAX_VALUE;
        }
        List<int[]> Next = fromS(src,arrivedNodes);
        if(Next == null) {
            //如果最後結果為Integer.MAX_VALUE那麼沒有一條路走通
            return Integer.MAX_VALUE;
        }
        
        List<Integer> tempList = new ArrayList<Integer>(arrivedNodes);
        tempList.add(src);
        int result = Integer.MAX_VALUE;
        for (int[] is : Next) {
            int tempTime = time + 1;    
            int temp = findNext(is[1],value + is[2],tempTime,tempList);
            if(temp < result) {
                result = temp;
            }
        }    
        return result;
    }
}

一開始純BFS,到10個結點就超時了,然後加了不能往回走,17個超時,然後加了到了終點剪枝,100超時,沒有再嘗試下去了。

Tags: