hdu 5636 Shortest Path【floyd+思维】

针对大规模路径图,本文介绍了一种通过预处理特定节点间最短路径以优化查询效率的方法。该方法避免了直接使用Floyd算法所带来的超时问题,特别适用于包含额外边的大规模图。

Shortest Path

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


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(1i<n). To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is1.

You are given the graph and several queries about the shortest path between some pairs of vertices.
 

Input
There are multiple test cases. The first line of input contains an integerT, indicating the number of test cases. For each test case:

The first line contains two integer n and m(1n,m105) -- the number of vertices and the number of queries. The next line contains 6 integersa1,b1,a2,b2,a3,b3(1a1,a2,a3,b1,b2,b3n), separated by a space, denoting the new added three edges are (a1,b1),(a2,b2),(a3,b3).

In the next m lines, each contains two integers si and ti(1si,tin), denoting a query.

The sum of values of m in all test cases doesn't exceed 106.
 

Output
For each test cases, output an integer S=(i=1mizi) mod (109+7), where zi is the answer for i-th query.
 Sample Input
1
10 2
2 4 5 7 8 10
1 5
3 1
 


Sample Output
7

N非常大,floyd直接写会超时,这里我们需要技巧来floyd、

除了新建立起来的三个点,我们的图保证任何相邻两个点的距离为1,我们新建三个点之后,我们不必要floyd整个图,因为有很多操作是不必要的、我们这里挑出这三个点来讨论、

我们可以做出这三个点所影响的路径的最短路,例如用样例来说:

我们做出2-4、2-5、2-7、2-8、2-10、4-2、4-5、4-7、4-8、4-10..................的最短路。然后假如我们需要求从1-5的最短路的时候,我们枚举i,j点对,使得从起点到终点的路径经过每一条最短路,挑出最小的值,就是从1-5的最短路。

我们这里口述可能说的比较乱,我们对应代码理解:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define ll long long int
const int mod=1000000007;
int n,m,a[3][2];
int dis[6][6];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=0;i<3;i++)
        {
            scanf("%d%d",&a[i][0],&a[i][1]);
        }
        for(int i=0;i<6;i++)//建立以这六个节点构成的图、
        {
            for(int j=0;j<6;j++)
            {
                if(j!=i)
                {
                    if(i/2==j/2)dis[i][j]=1;
                    else
                    {
                        dis[i][j]=abs(a[i/2][i%2]-a[j/2][j%2]);
                    }
                }
                else
                {
                    dis[i][j]=0;
                }
            }
        }
        ////////////////////////
        for(int i=0;i<6;i++)//求这个图的最短路
        {
            for(int j=0;j<6;j++)
            {
                for(int k=0;k<6;k++)
                {
                    dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
                }
            }
        }
        int ans=0;
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            int minn=abs(u-v);
            for(int i=0;i<6;i++)
            {
                for(int j=0;j<6;j++)//枚举所有这六个节点所形成的最短路、
                {
                    int uu=abs(u-a[i/2][i%2]);//从起点到最短路的起点的距离
                    int vv=abs(v-a[j/2][j%2]);//从终点到最短路的终点的距离
                    minn=min(minn,uu+vv+dis[i][j]);//最后加上最短路的距离、枚举之后一定能得到最短路
                }
            }
            ans+=((long long int )minn*(long long int )(i+1))%mod;
            if(ans>=mod)ans-=mod;
        }
        printf("%d\n",ans);
    }
}


根据原作 https://pan.quark.cn/s/0ed355622f0f 的源码改编 野火IM解决方案 野火IM是专业级即时通讯和实时音视频整体解决方案,由北京野火无限网络科技有限公司维护和支持。 主要特性有:私有部署安全可靠,性能强大,功能齐全,全平台支持,开源率高,部署运维简单,二次开发友好,方便与第三方系统对接或者嵌入现有系统中。 详细情况请参考在线文档。 主要包括一下项目: 野火IM Vue Electron Demo,演示如何将野火IM的能力集成到Vue Electron项目。 前置说明 本项目所使用的是需要付费的,价格请参考费用详情 支持试用,具体请看试用说明 本项目默认只能连接到官方服务,购买或申请试用之后,替换,即可连到自行部署的服务 分支说明 :基于开发,是未来的开发重心 :基于开发,进入维护模式,不再开发新功能,鉴于已经终止支持且不再维护,建议客户升级到版本 环境依赖 mac系统 最新版本的Xcode nodejs v18.19.0 npm v10.2.3 python 2.7.x git npm install -g node-gyp@8.3.0 windows系统 nodejs v18.19.0 python 2.7.x git npm 6.14.15 npm install --global --vs2019 --production windows-build-tools 本步安装windows开发环境的安装内容较多,如果网络情况不好可能需要等较长时间,选择早上网络较好时安装是个好的选择 或参考手动安装 windows-build-tools进行安装 npm install -g node-gyp@8.3.0 linux系统 nodej...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值