JAVA代码—算法基础:航线问题

航线问题
题目描述:

给出一行数据,包含两个整数N(2<=N<=500),M(1<=M<=2000),用单个空格隔开。
其表示共有N个空港,M条航线,起点为1,终点为N。
接下来给出M行数据,每行包含5个整数:
P、Q(1<=P,Q<=n)、K(1<=K<=1000)、X、Y(0<=X,Y<=10000),
代表P、Q两个空港有航线并需要K天,并且该航线在第X天到第Y天天气恶劣不可通行。

计算最快需要多少天到达目的空港。

输入样例:

4 4
2 1 1 7 13
4 3 2 10 11
1 3 8 9 12
2 3 3 2 10

输出样例:
14

算法设计:
package com.bean.algorithmbasic;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class AirLineProblem {

    /*
     * 一行包含两个整数N(2<=N<=500),M(1<=M<=2000),用单个空格隔开。
     * 表示共有N个空港,M条航线,起点为1,终点为N。
     * 接下来M行,每行包含5个整数:
     * P、Q(1<=P,Q<=n)、K(1<=K<=1000)、X、Y(0<=X,Y<=10000),
     * 代表P、Q两个空港有航线并需要K天,并且该航线在第X天到第Y天天气恶劣不可通行。
     * 
     * 输入样例:
     * 
     * 4  4
     * 2 1 1 7 13
     * 4 3 2 10 11
     * 1 3 8 9 12
     * 2 3 3 2 10
     * 
     * 输出样例:
     * 14
     * 
     * */

    static class Flight {
        public int P, Q, K, X, Y;

        public Flight(int P, int Q) {
            this.P = Math.min(P, Q);
            this.Q = Math.max(P, Q);
        }

        public Flight(int P, int Q, int K, int X, int Y) {
            this.P = Math.min(P, Q);
            this.Q = Math.max(P, Q);
            this.K = K;
            this.X = X;
            this.Y = Y;
        }

        @Override
        public boolean equals(Object o) {
            Flight f = (Flight) o;

            return this.P == f.P && this.Q == f.Q;
        }
    }


    public static void main(String[] args) throws FileNotFoundException {
        System.setIn(new FileInputStream("G:\\AirLineProblem.txt"));
        Scanner sc = new Scanner(System.in);

        String s[] = sc.nextLine().split(" ");
        int N = Integer.parseInt(s[0]);
        int M = Integer.parseInt(s[1]);
        ArrayList<Flight> flights = new ArrayList<Flight>();
        int f[] = new int[N + 1];

        for (int i = 0; i < M; i++) {
            s = sc.nextLine().split(" ");
            int P = Integer.parseInt(s[0]);
            int Q = Integer.parseInt(s[1]);
            int K = Integer.parseInt(s[2]);
            int X = Integer.parseInt(s[3]);
            int Y = Integer.parseInt(s[4]);

            Flight flight = new Flight(P, Q, K, X, Y);
            flights.add(flight);
        }

        for (int i = 2; i <= N; i++) {
            f[i] = Integer.MAX_VALUE;

            for (int j = 1; j < i; j++) {
                Flight flight = new Flight(j, i);
                int index = flights.indexOf(flight);

                if (index != -1) {
                    flight = flights.get(index);

                    if (f[j] + flight.K < flight.X || f[j] > flight.Y) {
                        f[i] = Math.min(f[i], f[j] + flight.K + 1);
                    } else {
                        f[i] = Math.min(f[i], flight.Y + flight.K + 1);
                    }
                }
            }
        }

        System.out.println(f[N]);
    }

}

(完)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值