前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 5636 Shortest Path(Floyed,枚举)

HDU 5636 Shortest Path(Floyed,枚举)

作者头像
ShenduCC
发布2018-04-26 11:27:55
5370
发布2018-04-26 11:27:55
举报
文章被收录于专栏:算法修养算法修养算法修养

Shortest Path

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1146 Accepted Submission(s): 358

Problem Description There is a path graph G=(V,E) with n vertices. Vertices are numbered from 1 to n and there is an edge with unit length between i and i+1 (1≤i

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
#define mod 1000000007
typedef long long int  LL;
int dp[10][10];
int n,m;
long long int s;
int main()
{
    int t;
    scanf("%d",&t);
    int a[10];
    int x,y;
    while(t--)
    {
        scanf("%d%d",&n,&m);
        scanf("%d%d%d%d%d%d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6]);
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=6;i++)
        {
            for(int j=1;j<=6;j++)
            {
               dp[i][j]=abs(a[j]-a[i]);
            }
        }
        dp[1][2]=1;dp[3][4]=1;dp[5][6]=1;
        dp[2][1]=1;dp[4][3]=1;dp[6][5]=1;
        for(int k=1;k<=6;k++)
        {
            for(int i=1;i<=6;i++)
            {
                for(int j=1;j<=6;j++)
                {
                    dp[i][j]=min(dp[i][j],dp[i][k]+dp[k][j]);
                }
            }
        }
        int res=0;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            int ans=abs(y-x);
            for(int i=1;i<=6;i++)
            {
                for(int j=1;j<=6;j++)
                {
                    ans=min(ans,abs(x-a[i])+abs(y-a[j])+dp[i][j]);

                }
            }
//            int num=i*ans%mod;
//            res+=num;
//            res%=mod;
            (res+=(LL)i*ans%mod)%=mod;

        }
        printf("%d\n",res);


    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档