HDU 1690 最短路

来源:互联网 发布:数据库高级培训 编辑:程序博客网 时间:2024/05/22 17:51
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;

typedef __int64 ll;
#define inf 0x3f3f3f3f
//#define inf 0x7f7f7f7f7f7f7f7fLL
#define min(x,y) (x<y?x:y)
ll l1,l2,l3,l4,c1,c2,c3,c4;
int n,m;
ll x[102];
ll dist[102][102];

void floyd(){
    for(int k=1;k<=n;k++){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(dist[i][k]==inf||dist[k][j]==inf){
                    continue;
                }
                ll res=dist[i][k]+dist[k][j];
                if(res<dist[i][j]){
                    dist[i][j]=res;
                }
            }
        }
    }
}

int main(){
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++){
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&l1,&l2,&l3,&l4,&c1,&c2,&c3,&c4);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%I64d",&x[i]);
        }
        for(int i=1;i<=n;i++){
            for(int j=i+1;j<=n;j++){
                ll d=x[i]-x[j];
                if(d<0){
                    d*=-1;
                }
                if(0<d&&d<=l1){
                        dist[i][j]=dist[j][i]=c1;
                }
                else if(l1<d&&d<=l2){
                    dist[i][j]=dist[j][i]=c2;
                }
                else if(l2<d&&d<=l3){
                    dist[i][j]=dist[j][i]=c3;
                }
                else if(l3<d&&d<=l4){
                    dist[i][j]=dist[j][i]=c4;
                }
                else{
                    dist[i][j]=dist[j][i]=inf;
                }
            }
        }
        floyd();
        printf("Case %d:\n",t);
        for(int i=0;i<m;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            if(dist[a][b]==inf){
                    printf("Station %d and station %d are not attainable.\n",a,b);
            }
            else{
                printf("The minimum cost between station %d and station %d is %I64d.\n",a,b,dist[a][b]);
            }
        }
    }
}


原创粉丝点击