Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1598 Accepted Submission(s): 520
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.
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 .
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
Source
Recommend
wange2014 | We have carefully selected several similar problems for you: 5867 5866 5865 5864 5863
题目大意:给你n个节点,这些节点的编号为1~n,每两个编号相邻的节点的距离为1,然后题中给出3对点,这三对点直接相连,距离变为1,给你m个查询,每次查询从s到t的最短路,最后求每次查询的最短路*查询的编号的和。
解题思路:普通的暴力不好过,那么考虑通过这六个新添加的点来找到最短路,将六个点之间的最短路打表,然后每次查询,最短路要么是abs(s-e),要么是通过六个点中的某两个点获得最短路,即ans=min(abs(s-e),s-i-j-e||s-j-i-e,e-i-j-s||e-j-i-s)。记得最后取模。
代码如下:
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define MOD 1000000007
long long map[7][7];
int x[7];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=6;i++)
{
scanf("%d",&x[i]);
}
for(int i=1;i<=6;i++)//离散化
{
for(int j=1;j<=6;j++)
{
map[i][j]=abs(x[i]-x[j]);//两个点没有直接连接的话距离就是是绝对值之差
}
}
map[1][2]=map[2][1]=1;//这三对点直接相连
map[3][4]=map[4][3]=1;
map[5][6]=map[6][5]=1;
for(int k=1;k<=6;k++)//求6个点之间的最短路径
{
for(int i=1;i<=6;i++)
{
for(int j=1;j<=6;j++)
{
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
}
}
}
long long ans=0;
for(int i=1;i<=m;i++)
{
int s,e;
scanf("%d%d",&s,&e);
long long tmp=abs(s-e);//这么初始化,tmp记得用long long型的,错了好几次
for(int j=1;j<=6;j++)
{
for(int k=1;k<=6;k++)
{
tmp=min(tmp,abs(s-x[j])+map[j][k]+abs(e-x[k]));//要么是s-j-k-e||s-k-j-e
tmp=min(tmp,abs(e-x[j])+map[j][k]+abs(s-x[k]));//要么是e-j-k-s||e-k-j-s
}
}
tmp=(tmp*i)%MOD;//题意
ans=(ans+tmp)%MOD;
}
ans=ans%MOD;
printf("%lld\n",ans);
}
return 0;
}