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; }

最新文章

  1. Mui沉浸模式以及状态栏颜色改变
  2. 数据结构图文解析之:AVL树详解及C++模板实现
  3. Phalapi 中Union和Union All的用法
  4. Blue tooth
  5. Mysql错误信息汇总
  6. LeetCode:Word Break(DP)
  7. win7下如何安装JDK
  8. Android EditText的常用技巧
  9. Jenkins安装入门
  10. grub2详解(翻译和整理官方手册)
  11. Java面试06|项目相关介绍
  12. 屏蔽eslint代码格式报错
  13. sublim 插件
  14. UVA10559&amp;POJ1390 Blocks 区间DP
  15. pandas dataframe的合并(append, merge, concat)
  16. [学习日志]2018-11-18 主要: idea更改java版本
  17. SpringBoot2静态资料访问
  18. python 操作 hbase
  19. Python开发【Django】:图片验证码、KindEditor
  20. 代码审计之Catfish CMS v4.5.7后台作者权限越权两枚+存储型XSS一枚

热门文章

  1. 关于使用jq跨域请求的实现
  2. 【Linux】centos7中 root家目录中perl5文件夹无法删除问题解决
  3. Pandas的数据分组-aggregate聚合
  4. 迈凯伦765LT/600LT/720S/650S/570S维修手册电路图Mclaren车间手册接线图
  5. 图解 | 原来这就是TCP
  6. argparse的简单使用
  7. 每月一更的《HelloGitHub》第 58 期,来啦!
  8. C#高级编程第11版 - 第九章 索引
  9. Go RPC 框架 KiteX 性能优化实践 原创 基础架构团队 字节跳动技术团队 2021-01-18
  10. 深度漫谈数据系统架构——Lambda architecture