Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1494 Accepted Submission(s): 476
Problem Description
There is a path graph G=(V 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, where z 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
最近在学习最短路问题,floyd写法。
void floyd(){
for(int k = 1; k<=6; k++)
for(int i = 1; i<=6; i++)
for(int j = 1; j<=6; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
for(int k = 1; k<=6; k++)
for(int i = 1; i<=6; i++)
for(int j = 1; j<=6; j++)
d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
}
1 #include <iostream> 2 #include<cstdio> 3 #include<queue> 4 #include<cstring> 5 #include<algorithm> 6 #include<cmath> 7 using namespace std; 8 const int maxn = 105; 9 const int INF = 0x3f3f3f3f; 10 const int mod = 1e9+7; 11 int d[7][7]; 12 int a[7]; 13 void floyd(){ 14 for(int k = 1; k<=6; k++) 15 for(int i = 1; i<=6; i++) 16 for(int j = 1; j<=6; j++) 17 d[i][j] = min(d[i][j],d[i][k]+d[k][j]); 18 } 19 void solve(){ 20 int t,n,m; 21 scanf("%d",&t); 22 while(t--){ 23 scanf("%d%d",&n,&m); 24 for(int i = 1; i<=6; i++) scanf("%d",&a[i]); 25 for(int i = 1; i<=6; i++){ 26 for(int j = 1; j<=6; j++){ 27 d[i][j] = abs(a[i] - a[j]); 28 } 29 } 30 for(int i = 1; i<=6; i+=2) d[i][i+1] = d[i+1][i] = 1; 31 floyd(); 32 long long sum = 0; 33 34 for(int k = 1; k<=m; k++){ 35 int x,y; 36 scanf("%d%d",&x,&y); 37 int ans = abs(x-y); 38 for(int i = 1; i<=6; i++) 39 for(int j = 1; j<=6; j++) 40 ans = min(ans,(abs(x-a[i])+d[i][j]+abs(y-a[j]))); 41 sum = (sum + (long long)ans*k%mod)%mod; 42 } 43 printf("%I64d ",sum); 44 } 45 } 46 int main() 47 { 48 solve(); 49 return 0; 50 }