Shortest Path
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1586 Accepted Submission(s): 512
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
之前一直用的spfa,感觉又快又方便,没怎么注意过弗洛伊德,到头来还是太天真,这题弗洛伊德才能过
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<cmath>
#include<algorithm>
#define inf 0x3f3f3f3f
#define ll long long
#define mes(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = 1e6+10;
const int mod = 1e9+7;
ll ans;
int dis[10][10];
int cnt[10];
int main(){
int t, m, n, a, b;
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(int i = 0; i < 6; i++){
scanf("%d", &cnt[i]);
}
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
dis[i][j] = abs(cnt[i]-cnt[j]);
}
}
dis[0][1] = dis[1][0] = dis[2][3] = dis[3][2] = dis[4][5] = dis[5][4] = 1;
for(int k = 0; k < 6; k++){
for(int i = 0; i < 6; i++){
for(int j = 0; j < 6; j++){
dis[i][j] = min(dis[i][j], dis[i][k]+dis[k][j]);
}
}
}
ans = 0;
for(int i = 1; i <= m; i++){
scanf("%d%d", &a, &b);
int sum = abs(a-b);
for(int j = 0; j < 6; j++){
for(int k = 0; k < 6; k++){
sum = min(sum, abs(cnt[j]-a)+abs(cnt[k]-b)+dis[j][k]);
}
}
ans += (ll)sum * (ll)i;
}
printf("%lld\n", ans%mod);
}
return 0;
}