题意:
http://acm.hdu.edu.cn/showproblem.php?pid=1690
给定分段函数,表示两点之间距离为一定时的花费,给定t个case,n个点,m个询问,问需要花费金额最小值
题解:
#include<bits/stdc++.h>
#define INF 0x7fffffffffffffff
#define ll long long
using namespace std;
int n,m;
int t;
int l[10],c[10];
long long dis[10000][10000];
void init()
{
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dis[i][j]=dis[j][i]=(i==j?0:INF);
}
}
}
void floyed()
{
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(dis[i][k]!=INF&&dis[k][j]!=INF&&dis[i][j]>dis[i][k]+dis[k][j]){
dis[i][j]=dis[i][k]+dis[k][j];
}
}
}
}
}
int main()
{
cin>>t;
for(int k=1;k<=t;k++){
cin>>l[1]>>l[2]>>l[3]>>l[4]>>c[1]>>c[2]>>c[3]>>c[4];
cin>>n>>m;
int x[1000];
for(int i=1;i<=n;i++)
{
cin>>x[i];
}
init();
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
if(abs(x[i]-x[j])<=l[1]){
dis[i][j]=dis[j][i]=c[1];
}
else if(abs(x[i]-x[j])<=l[2]){
dis[i][j]=dis[j][i]=c[2];
}else if(abs(x[i]-x[j])<=l[3]){
dis[i][j]=dis[j][i]=c[3];
}else if(abs(x[i]-x[j])<=l[4]){
dis[i][j]=dis[j][i]=c[4];
}
else {
dis[i][j]=dis[j][i]=INF;
}
}
}
floyed();
cout<<"Case "<<k<<":"<<endl;
for(int i=1;i<=m;i++){
int ax,ay;
cin>>ax>>ay;
if(dis[ax][ay]!=INF){
cout<<"The minimum cost between station "<<ax<<" and station "<<ay<<" is "<<dis[ax][ay]<<"."<<endl;
}else{
cout<<"Station "<<ax<<" and station "<<ay<<" are not attainable."<<endl;
}
}
}
}