java蓝桥杯练习 Car的旅行路线

java蓝桥杯练习 Car的旅行路线

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
  又到暑假了,住在城市A的Car想和朋友一起去城市B旅游。她知道每个城市都有四个飞机场,分别位于一个矩形的四个顶点上,同一个城市中两个机场之间有一 条笔直的高速铁路,第I个城市中高速铁路了的单位里程价格为Ti,任意两个不同城市的机场之间均有航线,所有航线单位里程的价格均为t。
  那么Car应如何安排到城市B的路线才能尽可能的节省花费呢?她发现这并不是一个简单的问题,于是她来向你请教。
  找出一条从城市A到B的旅游路线,出发和到达城市中的机场可以任意选取,要求总的花费最少。
输入格式
  第一行有四个正整数s,t,A,B。
  S表示城市的个数,t表示飞机单位里程的价格,A,B分别为城市A,B的序号,(1<=A,B<=S)。
  接下来有S行,其中第I行均有7个正整数x1,y1,x2,y2,x3,y3,Ti,这当中的(x1,y1),(x2,y2),(x3,y3)分别是第I个城市中任意三个机场的坐标,Ti为第i个城市高速铁路单位里程的价格。
输出格式
  共有n行,每行一个数据对应测试数据,保留一位小数。
样例输入
1 10 1 3
1 1 1 3 3 1 30
2 5 7 4 5 2 1
8 6 8 8 11 6 3
样例输出
47.5
数据规模和约定
  0<S<=100

//java code

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int s = sc.nextInt();
        int t = sc.nextInt();
        int A = sc.nextInt();
        int B = sc.nextInt();
        City cities[] = new City[s];
        for (int i = 0; i < s; i++) {
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int x3 = sc.nextInt();
            int y3 = sc.nextInt();
            int T = sc.nextInt();
            City city = new City(x1, y1, x2, y2, x3, y3, T);
            cities[i] = city;
        }
        double a[][] = new double[s * 4][s * 4];
        for (int j = 0; j < s * 4; j++) {
            for (int k = 0; k < s * 4; k++) {
                if (j / 4 == k / 4)
                    a[j][k] = dist(cities[j / 4].x[j % 4], cities[j / 4].y[j % 4], cities[k / 4].x[k % 4], cities[k / 4].y[k % 4]) * cities[j / 4].T;
                else
                    a[j][k] = dist(cities[j / 4].x[j % 4], cities[j / 4].y[j % 4], cities[k / 4].x[k % 4], cities[k / 4].y[k % 4]) * t;
            }
        }
        for (int j = 0; j < s * 4; j++) {
            for (int k = 0; k < s * 4; k++) {
                for (int l = 0; l < s * 4; l++) {
                    a[k][l] = Math.min(a[k][l], a[k][j] + a[j][l]);
                }
            }
        }
        double min = a[(A - 1) * 4][(B - 1) * 4];
        for (int j = (A - 1) * 4; j < A * 4; j++) {
            for (int k = (B - 1) * 4; k < B * 4; k++) {
                if (min > a[j][k])
                    min = a[j][k];
            }
        }
        System.out.printf("%.1f", min);
    }
    private static double dist(int x1, int y1, int x2, int y2) {
        return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }
}
class City {
    int x[] = new int[4];
    int y[] = new int[4];
    int T;
    public City(int x1, int y1, int x2, int y2, int x3, int y3, int T) {
        super();
        x[0] = x1;
        y[0] = y1;
        x[1] = x2;
        y[1] = y2;
        x[2] = x3;
        y[2] = y3;
        double t12 = dist(x1, y1, x2, y2);//斜率
        double t23 = dist(x2, y2, x3, y3);
        double t13 = dist(x1, y1, x3, y3);
        if (Math.abs(t12 * t12 + t13 * t13 - t23 * t23) < 0.000001) {
            x[3] = x3 + x2 - x1;
            y[3] = y3 + y2 - y1;
        }
        if (Math.abs(t12 * t12 + t23 * t23 - t13 * t13) < 0.000001) {
            x[3] = x3 + x1 - x2;
            y[3] = y3 + y1 - y2;
        }
        if (Math.abs(t23 * t23 + t13 * t13 - t12 * t12) < 0.000001) {
            x[3] = x1 + x2 - x3;
            y[3] = y1 + y2 - y3;
        }
        this.T = T;
    }
    private double dist(int x1, int y1, int x2, int y2) {
        return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值