HDU 5636 Shortest Path(最短路)

题目大意:给定n个点i和i+1(i

#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<stack>
#include<cstring>
#include<algorithm>
#define LL __int64
#define inf 0x3f3f3f3f
#define ls l,mid,rt<<1
#define rs mid+1,r,rt<<1|1
const double PI=acos(-1.0);
using namespace std;
#define mod 1000000007
int mp[10][10],cnt,arr[10];
int main(){
    int n,m,i,j,k;
    scanf("%d",&k);
    while(k--){
        cnt=0;
        scanf("%d%d",&n,&m);

        for(i=1;i<=6;++i){
            scanf("%d",&arr[i]);
        }
        for(i=1;i<=6;i++){
            for(j=1;j<=6;++j){
               mp[i][j]=abs(arr[i]-arr[j]);
            }
        }

        for(i=1;i<=6;++i){
            if(i%2 == 0){
                if(arr[i] != arr[i-1] ){
                    mp[i][i-1] = mp[i-1][i] = 1;
                }
            }
        }

        for(int o=1;o<=6;o++){
            for(i=1;i<=6;i++){
                for(j=1;j<=6;j++){
                    if(mp[i][j]>mp[i][o]+mp[o][j]){
                        mp[i][j] = mp[i][o] + mp[o][j];
                    }
                }
            }
        }
        LL ans=0,t;

        for(int o = 1;o <= m;o++){
            int a,b;
            scanf("%d%d",&a,&b);
            t = abs(a-b);
            for(i = 1;i <= 6;i++ ){
                for(j = i;j <= 6 ;j++ ){
                    t=min( t, min((LL) abs(arr[i]-a)+mp[i][j]+abs(arr[j]-b ) ,(LL) abs(arr[j]-a)+mp[i][j]+abs(arr[i]-b )  ) );
                }
            }
            ans=(ans+t*o)%mod;
        }

        printf("%I64d\n",ans);
    }
    return 0;
}
相关文章
相关标签/搜索