Java实现 洛谷 Car的旅行路线
输入输出样例
输入样例#1:
1
3 10 1 3
1 1 1 3 3 1 30
2 5 7 4 5 2 1
8 6 8 8 11 6 3
输出样例#1:
47.5
import java.util.Scanner;
//洛谷
public class car的旅行路线 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int cout=in.nextInt();
for (int ss = 0; ss <cout; ss++) {
int s = in.nextInt();//s表示城市的个数
int t = in.nextInt();//t代表飞机的单价
int A = in.nextInt();
int B = in.nextInt();
City []cities = new City[s];
for(int i = 0; i < s; i++){
int x1 = in.nextInt();
int y1 = in.nextInt();
int x2 = in.nextInt();
int y2 = in.nextInt();
int x3 = in.nextInt();
int y3 = in.nextInt();
int price = in.nextInt();
City city = new City(x1, y1, x2, y2, x3, y3, price);
cities[i] = city;
}
double [][]dp = new double[s*4][s*4];
for(int i = 0; i < s*4; i++){
for( int j = 0; j < s*4 ;j++){
if(i/4==j/4){
dp[i][j] = dist(cities[i/4].x[i%4],cities[i/4].y[i%4],cities[j/4].x[j%4],cities[j/4].y[j%4])*cities[i/4].price;
}else{
dp[i][j] = dist(cities[i/4].x[i%4],cities[i/4].y[i%4],cities[j/4].x[j%4],cities[j/4].y[j%4])*t;
}
//System.out.println(i+" "+j+" "+dp[i][j]);
}
}
for(int i = 0; i < 4*s;i++){
for(int j = 0; j < 4*s; j++){
for(int k = 0; k < 4*s; k++){
dp[j][k] = Math.min(dp[j][k], dp[j][i]+dp[i][k]);
//System.out.println(j+" "+k+" "+dp[j][k]);
}
}
}
double min = dp[(A-1)*4][(B-1)*4];
//System.out.println(min);
for(int i = (A-1)*4; i <A*4; i++){
for(int j = (B-1)*4; j <B*4; j++){
if(min>dp[i][j]){
min = dp[i][j];
//System.out.println(i+" "+j+" "+min);
}
}
}
System.out.printf("%.1f", min);
}
}
private static double dist(int x12, int y12, int x22, int y22) {
// TODO Auto-generated method stub
return Math.sqrt((x22-x12)*(x22-x12)+(y22-y12)*(y22-y12));
}
}
class City{
int []x = new int[4];
int []y = new int[4];
int price;
public City(int x1, int y1, int x2, int y2, int x3, int y3, int price) {
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 t13 = dist(x1,y1,x3,y3);
double t23 = dist(x2,y2,x3,y3);
if(Math.abs(t12*t12+t13*t13-t23*t23)<0.000001){
x[3] = x2+x3-x1;
y[3] = y2+y3-y1;
}
if(Math.abs(t12*t12+t23*t23-t13*t13)<0.000001){
x[3] = x1+x3-x2;
y[3] = y1+y3-y2;
}
if(Math.abs(t13*t13+t23*t23-t12*t12)<0.000001){
x[3] = x1+x2-x3;
y[3] = y1+y2-y3;
}
this.price = price;
}
private double dist(int x12, int y12, int x22, int y22) {
// TODO Auto-generated method stub
return Math.sqrt((x22-x12)*(x22-x12)+(y22-y12)*(y22-y12));
}
}