HDU 5636 Shortest Path(Floyed,枚举)

2024-02-19 14:08

本文主要是介绍HDU 5636 Shortest Path(Floyed,枚举),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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

In the next mm lines, each contains two integers sisi and titi (1≤si,ti≤n)(1≤si,ti≤n), denoting a query. 

The sum of values of mm in all test cases doesn't exceed 106106.

Output

For each test cases, output an integer S=(∑i=1mi⋅zi) mod (109+7)S=(∑i=1mi⋅zi) mod (109+7), where zizi is the answer for ii-th query.

Sample Input

1
10 2
2 4 5 7 8 10
1 5
3 1

Sample Output

7

题意:

       给定n个点,两个点间的距离为1,再给定3对点(3条边,权值为1),有每次询问,将每次的结果相加对1e9++7求模。

分析:

       先通过一次flyod求出任意两点间的最短距离,然后以6个点为中心,再一次flyod找出输入的两点间的最小距离即可。

代码:(思路看懂了,懒的敲了,主要是没时间啊,继续补题-------)

#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);///因为权值都为1,序列号相减即为输入的两点间的距离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;
}

 

 

这篇关于HDU 5636 Shortest Path(Floyed,枚举)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/724902

相关文章

Kotlin 枚举类使用举例

《Kotlin枚举类使用举例》枚举类(EnumClasses)是Kotlin中用于定义固定集合值的特殊类,它表示一组命名的常量,每个枚举常量都是该类的单例实例,接下来通过本文给大家介绍Kotl... 目录一、编程枚举类核心概念二、基础语法与特性1. 基本定义2. 带参数的枚举3. 实现接口4. 内置属性三、

C#之枚举类型与随机数详解

《C#之枚举类型与随机数详解》文章讲解了枚举类型的定义与使用方法,包括在main外部声明枚举,用于表示游戏状态和周几状态,枚举值默认从0开始递增,也可手动设置初始值以生成随机数... 目录枚举类型1.定义枚举类型(main外)2.使用生成随机数总结枚举类型1.定义枚举类型(main外)enum 类型名字

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

C++11作用域枚举(Scoped Enums)的实现示例

《C++11作用域枚举(ScopedEnums)的实现示例》枚举类型是一种非常实用的工具,C++11标准引入了作用域枚举,也称为强类型枚举,本文主要介绍了C++11作用域枚举(ScopedEnums... 目录一、引言二、传统枚举类型的局限性2.1 命名空间污染2.2 整型提升问题2.3 类型转换问题三、C

Java 枚举的基本使用方法及实际使用场景

《Java枚举的基本使用方法及实际使用场景》枚举是Java中一种特殊的类,用于定义一组固定的常量,枚举类型提供了更好的类型安全性和可读性,适用于需要定义一组有限且固定的值的场景,本文给大家介绍Jav... 目录一、什么是枚举?二、枚举的基本使用方法定义枚举三、实际使用场景代替常量状态机四、更多用法1.实现接

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

解决jupyterLab打开后出现Config option `template_path`not recognized by `ExporterCollapsibleHeadings`问题

《解决jupyterLab打开后出现Configoption`template_path`notrecognizedby`ExporterCollapsibleHeadings`问题》在Ju... 目录jupyterLab打开后出现“templandroidate_path”相关问题这是 tensorflo

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置