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<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=(∑i=1mi⋅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

题意:给你一条n个点组成的链,相邻两点的距离为1,再给你三条边,这三条边的端点都是链上的点,且每一条的距离为1。有m个询问,问你对于每两个点,从一个端点到另一个端点的最近距离是多少。

思路:可以先初始化3条边中6个点两两之间的最短距离,这个可以用floyd做,那么对于每一个询问,两个点x1,x2的最短距离为不经过任何点,或者经过3条边中的某些边,又因为我们已经初始化出3条边中任意两个点的最短距离,所以我们只要枚举a,b,即x1到a,a到b,再b到x2的最近距离。这一题floyd初始化时关键,如果每次直接8个点floyd时间复杂度就爆了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 1000000007
#define pi acos(-1.0)
#define MOD 1000000007
int dist[10][10];
void floyd()
{
int i,j,k;
for(k=1;k<=6;k++){
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
if(dist[i][j]>dist[i][k]+dist[k][j]){
dist[i][j]=dist[i][k]+dist[k][j];
} } } }
} int main()
{
int n,m,i,j,T,k;
int x[10];
int a1,b1,a2,b2,a3,b3;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
scanf("%d%d%d%d%d%d",&x[1],&x[2],&x[3],&x[4],&x[5],&x[6]);
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
dist[i][j]=abs(x[i]-x[j]);
}
}
dist[1][2]=dist[2][1]=min(dist[1][2],1);
dist[3][4]=dist[4][3]=min(dist[3][4],1);
dist[5][6]=dist[6][5]=min(dist[5][6],1);
floyd(); ll sum=0;
for(k=1;k<=m;k++){
scanf("%d%d",&x[7],&x[8]);
int ans=abs(x[7]-x[8]);
for(i=1;i<=6;i++){
for(j=1;j<=6;j++){
ans=min(ans,abs(x[7]-x[i] )+abs(x[8]-x[j])+dist[i][j] );
ans=min(ans,abs(x[7]-x[j] )+abs(x[8]-x[i])+dist[i][j] ); }
} sum=(sum+(ll)ans*(ll)k)%MOD;
//printf("%d\n",floyd()); }
printf("%lld\n",sum); }
return 0; }

hdu5365Shortest Path (floyd)的更多相关文章

  1. HDU3631:Shortest Path(Floyd)

    Problem Description When YY was a boy and LMY was a girl, they trained for NOI (National Olympiad in ...

  2. HDU - 3631 Shortest Path(Floyd最短路)

    Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...

  3. [ZOJ2760]How Many Shortest Path(floyd+最大流)

    题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 题意:给你一个一个n*n(n<=100)的有向图,问你从s到 ...

  4. sdut1282Find the Path (floyd变形)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1282 感觉这题就比较有意思了 ,虽说是看了别人 ...

  5. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  6. 【转】最短路&差分约束题集

    转自:http://blog.csdn.net/shahdza/article/details/7779273 最短路 [HDU] 1548 A strange lift基础最短路(或bfs)★254 ...

  7. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  8. [matlab] 22.matlab图论实例 最短路问题与最小生成树 (转载)

    最短路问题之 Floyd 某公司在六个城市 c1c1,c2c2,….,c6c6 中有分公司,从 cici 到 cjcj 的直接航程票价记在下述矩阵的 (ii,jj) 位置上. (∞∞表示无直接航路), ...

  9. 【HDOJ图论题集】【转】

    =============================以下是最小生成树+并查集====================================== [HDU] How Many Table ...

随机推荐

  1. C#数组的 Length 和 Count()

    C#数组的 Length 和 Count() C# 数组中 Length 表示数组项的个数,是个属性.而 Count() 也是表示项的个数,是个方法,它的值和 Length 一样.但实际上严格地说, ...

  2. 爬虫系列 | 6、详解爬虫中BeautifulSoup4的用法

    bs4,全称BeautifulSoup 4 , 它是Python独有的一种解析方式.也就是说只有Python语言才可以通过这种方式去解析数据. BeautifulSoup 3 只支持Python2,所 ...

  3. 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限

    目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...

  4. 1.搭建Hadoop实验平台

    节点功能规划 操作系统:CentOS7.2(1511) Java JDK版本:jdk-8u65-linux-x64.tar.gz Hadoop版本:hadoop-2.8.3.tar.gz 下载地址: ...

  5. Angular学习资料大全和常用语法汇总(让后端程序员轻松上手)

    前言: 首先为什么要写这样的一篇文章呢?主要是因为前段时间写过一些关于Angualr的相关实战文章,有些爱学习的小伙伴对这方面比较感兴趣,但是又不知道该怎么入手(因为认识我的大多数小伙伴都是后端的同学 ...

  6. 判断最长回文串——暴力、延展、Manacher

    1. 暴力 时间复杂度O(n^3). 2. 延展 以某一字符为中心,设置left, right两个变量同时向外扩,判断他们指向字符是否相同.注意分奇偶讨论.时间复杂度O(n^2). 3. Manach ...

  7. Docker 建站小记

    一,前言 Docker 建站小记,我使用了四个镜像来搭建:nginx,certbot,mysql,gradle.欢迎访问:https://www.zzk0.top 这个网页是从 github 上找的个 ...

  8. HTML基础复习2

    6.表格 6.1建立表格: 表格由<table></table>标签来定义 每行由<tr></tr>来定义,每行被分割为若干单元格,由<td> ...

  9. java基础-01代理类

    简单的代理类实现案例主实现类:ProxyTestimport java.lang.reflect.InvocationHandler;import java.lang.reflect.Proxy;im ...

  10. jackson学习之三:常用API操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...