http://acm.hdu.edu.cn/showproblem.php?pid=1690
题目 | 算法 | 备注 |
import java.util.Scanner;
public class Main {
static long z=1000000000000000000l;
static int l[]=new int[4];
static int c[]=new int[4];
static long a[][]=new long[101][101];
static public long cmon(int l[],int c[],long dis){
if(dis<=l[0]){return c[0];}
else if(dis>l[0]&&dis<=l[1]){return c[1];}
else if(dis>l[1]&&dis<=l[2]){return c[2];}
else if(dis>l[2]&&dis<=l[3]){return c[3];}
else{return z;}
}
public static void main(String[] args) {
int n,i,j,k,t,q,p;
Scanner oo=new Scanner(System.in);
p=oo.nextInt();
for(t=1;t<=p;t++){
for(i=0;i<4;i++){l[i]=oo.nextInt();}
for(i=0;i<4;i++){c[i]=oo.nextInt();}
n=oo.nextInt();
q=oo.nextInt();
int loc[]=new int[n];
for(i=0;i<n;i++){
loc[i]=oo.nextInt();
}
int q1[]=new int[q];
int q2[]=new int[q];
for(i=0;i<q;i++){
q1[i]=oo.nextInt();
q2[i]=oo.nextInt();
}
for(i=0;i<101;i++){
for(j=0;j<101;j++){
if(i==j){a[i][j]=0;}
else{a[i][j]=z;}
}
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
a[i][j]=cmon(l,c,Math.abs(loc[j]-loc[i]));
a[j][i]=a[i][j];
}
}
for(k=0;k<n;k++){
for(i=0;i<n;i++){
if(a[i][k]<z){
for(j=0;j<n;j++){
if(a[i][k]+a[k][j]<a[i][j]){
a[i][j]=a[i][k]+a[k][j];
}
}
}
}
}
System.out.println("Case "+t+":");
for(i=0;i<q;i++){
if(a[q1[i]-1][q2[i]-1]>=z){
System.out.println("Station "+q1[i]+" and station "+q2[i]+" are not attainable.");
}
else{
System.out.println("The minimum cost between station "+q1[i]+" and station "+q2[i]+" is "+a[q1[i]-1][q2[i]-1]+".");
}
}
}
}
}