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. Go语言从入门到放弃(四)

    前言 有段时间没摸Go语言了,最近B站的Go语言泄露挺火的. 还差的很远呐 学无止境 本章主要介绍一些零碎的小知识点 变更记录 # 19.4.30  起笔 # 19.4.30  增加代码打包步骤 正文 ...

  2. JavaScript 获得当前日期+时间

    //直接从项目中copy出来的,亲测可用.function getTodayTime(){ var date = new Date(); var seperator1 = "-"; ...

  3. 安装SVN和汉化包及基本使用

    官网下载程序 和 下载汉化包. https://tortoisesvn.net/   注意SVN汉化包版本需要和SVN版本一致,否则是无效的. 一.下载 1.进入官网,点击downloads 2.点击 ...

  4. python学习笔记 | PyCharm创建文件时自动添加头文件

    File Settings Editor File and Code Templates Python Script 然后在右边的框中写入信息就可以啦: # -*- coding: utf-8 -*- ...

  5. 【Docker】Docker启动停止重启 Redirecting to /bin/systemctl start docker.service

    [root@liuawen local]# docker -v Docker version 1.13.1, build cccb291/1.13.1 [root@liuawen local]# 启动 ...

  6. SDUST数据结构 - chap9 排序

    判断题: 选择题: 编程题: 7-1 排序: 输入样例: 11 4 981 10 -17 0 -20 29 50 8 43 -5 输出样例: -20 -17 -5 0 4 8 10 29 43 50 ...

  7. Promise.all()使用实例

    一.什么是Promise.all()? 在说这个之前要先说清楚promise.promise就是一个对象,专门用来处理异步操作的. 而Promise.all方法用于将多个 Promise 实例,包装成 ...

  8. 前端知识(二)03-Webpack-谷粒学院

    目录 一.什么是Webpack 二.Webpack安装 1.全局安装 2.安装后查看版本号 三.创建项目 1.初始化项目 2.创建src文件夹 3.src下创建common.js 4.src下创建ut ...

  9. E1.获取Elixir/Erlang版本信息

    E1.获取Elixir/Erlang版本信息 获取Elixir版本 直接在shel中打开iex (interactive shell),就可以查到具体的版本信息: iex Erlang/OTP 22 ...

  10. Linux 三剑客之 grep 使用详解

    Linux 最重要的三个命令在业界被称为三剑客,它们是:awk.sed.grep.sed 已经在上篇中讲过,本文要讲的是 grep 命令. 我们在使用 Linux 系统中,grep 命令的使用尤为频繁 ...