Shortest Path(HDU-5636)

探讨了在路径图中,通过添加三条单位长度边来优化最短路径算法的方法。通过对新增边的枚举和Floyd算法的应用,实现了对任意两点间最短路径的有效计算,避免了直接使用大规模数组带来的内存溢出问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description

There is a path graph G=(V,E) with nn vertices. Vertices are numbered from 1 to n and there is an edge with unit length between i and i+1 (1≤i<n). To make the graph more interesting, someone adds three more edges to the graph. The length of each new edge is 1. 

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 integer T, indicating the number of test cases. For each test case: 

The first line contains two integer n and m (1≤n,m≤105) -- the number of vertices and the number of queries. The next line contains 6 integers a1,b1,a2,b2,a3,b3 (1≤a1,a2,a3,b1,b2,b3≤n), 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 (1≤si,ti≤n), 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=(∑mi⋅zi) 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

题意:t 组数据,每组数据给出两个数 n、m 代表一个图有 n 个点,且相邻两个点 i 到 i+1 的距离是 1,另外再加三条边,边权同样为 1,m 代表 m 组查询,每组查询给出两个数 x、y ,要求将每组的查询结果 mi 乘以组号 i 后相加模 1E9+7 后输出

思路:实质就是一条线,线上有 n 个点,相邻两点距离为一,然后有 3 组点的距离改为 1,然后查询任意两点的距离。

由于数据量的问题,直接 Floyd,开数组 dis[1000000][1000000] 会爆,故此题需要技巧

通过题意可以看出,在不加新的三条边前,任意两点 x、y 的路径长度为 |x-y|,由于加 3 条边权为 1 的边后要求两点最短路,因此增加的边要具有缩短边权的功能,否则就没有任何作用

因此,可以直接去枚举新增的三条边,通过 Floyd 看能否缩短路径,如果可以的话就更新最小值,然后查询时,先通过给出的两点 x、y 判断原距离 |x-y|,然后与 Floyd 后的最小距离比较,找出最小的,最后直接将每组查询结果与组号相乘再相加取模即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-6
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define N 1001
#define LL long long
using namespace std;
int n,m;
int a[N];
int dis[N][N];
void floyd(){
    for(int k=1;k<=6; k++)
        for(int i=1;i<=6;i++)
            for(int j=0;j<=6;j++)
                dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
int main (){
    int t;
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&m);
		for(int i=1;i<=6;i++) //六个点
			scanf("%d",&a[i]);

		for(int i=1;i<=6;i++)
            for(int j=1;j<=6;j++)
                dis[i][j]=abs(a[i]-a[j]);

		for(int i=1;i<=6;i+=2)//相邻距离为1
			dis[i][i+1]=dis[i+1][i]=1;

		floyd();

		LL res=0;
		for(int i=1;i<=m;i++){
            int x,y;
			scanf("%d%d",&x,&y);
			int len=abs(x-y);//原距离
			for(int j=1;j<=6;j++)
                for(int k=1;k<=6;k++)
                    len=min(len,abs(x-a[j])+abs(y-a[k])+dis[j][k]);

			res=(res+(LL)i*len%MOD)%MOD;
		}
		printf("%lld\n",res);
	}

    return 0;
}

 

GCN (Graph Convolutional Network) Shortest-Path-Master 是一种基于图卷积网络的最短路径算法。最短路径问题是图论中的经典问题,对于给定的图和起始点,找到到达目标点的最短路径。 GCN Shortest-Path-Master 通过应用图卷积神经网络的思想来解决最短路径问题。传统的最短路径算法(如Dijkstra算法或贝尔曼-福特算法)在计算过程中不考虑节点的特征信息,只利用图的拓扑结构。而GCN Shortest-Path-Master 利用了节点的特征信息,将节点的邻居节点信息通过图卷积操作进行聚合,得到节点的新特征表示。 GCN Shortest-Path-Master 的核心思想是,通过图卷积层不断更新节点的特征表示,使得节点的特征表示能够包含更多关于最短路径的信息。在每次迭代中,GCN Shortest-Path-Master 将节点的特征与邻居节点的特征进行聚合,得到节点的新特征表示。在网络的最后一层,通过对所有节点进行分类任务,可以得到每个节点到达目标点的最短路径预测。 相比传统的最短路径算法,GCN Shortest-Path-Master 提供了以下优势: 1. GCN Shortest-Path-Master 能够利用节点的特征,从而更好地表达节点之间的相互作用和联系。 2. GCN Shortest-Path-Master 可以自适应地学习节点的特征表示,而无需人工定义特征。 3. GCN Shortest-Path-Master 可以处理大规模的图结构,在计算效率上具有一定优势。 总之,GCN Shortest-Path-Master 是一种基于图卷积神经网络的最短路径算法,通过利用节点的特征信息,能够更好地解决最短路径问题。它在图结构数据中的应用具有很大潜力,在社交网络分析、推荐系统和物流路径规划等领域都有广泛的应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值