# Shortest Path

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1396 Accepted Submission(s): 442

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 (1i<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 (1n,m105) -- the number of vertices and the number of queries. The next line contains 6 integers a1,b1,a2,b2,a3,b3 (1a1,a2,a3,b1,b2,b3n) , 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 (1si,tin) , 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=1mizi) 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



#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x[6]={0},dis[6][6]={0};
scanf("%d%d",&n,&m);
for(int i=0;i<6;++i)//三条边，六个点
{
scanf("%d",&x[i]);
}
for(int i=0;i<6;++i)//新加的三条边的原始距离
{
for(int j=0;j<i;++j)
{
dis[i][j]=dis[j][i]=abs(x[i]-x[j]);
}
}
for(int i=0;i<6;i+=2)//新加的边
{
dis[i][i+1]=dis[i+1][i]=1;
}
for(int i=0;i<6;++i)//三个点跑floyd最短路算法
{
for(int j=0;j<6;++j)
{
for(int k=0;k<6;++k)
{
dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
}
}
}
long long ans=0;
for(int i=1;i<=m;++i)
{
int a,b;
scanf("%d%d",&a,&b);
int len=abs(a-b),mod=1e9+7;
for(int j=0;j<6;++j)
{
for(int k=0;k<6;++k)
{
int tp=abs(a-x[j])+abs(b-x[k])+dis[j][k];//暴力枚举
len=min(tp,len);
}
}
ans=(ans+(long long)i*len%mod)%mod;
}
printf("%lld\n",ans);
}
return 0;
}

每一个你不满意的现在，都有一个你没有努力的曾经。
一个历史类的公众号，欢迎关注