Description
Give you a graph G of n points, numbered 1-n. We define the Geosetic set of two points as Geodetic(x,y), representing the set of points on all the shortest path from point x to point y.
Each point has a weight wi, we define the sum of the points in Geodetic (x, y) corresponding to wi as U(x,y)
We guarantee that there is no self-loop and there is at most one edge between two points.
For example
Geodetic(2,5)={2,3,4,5}
Geodetic(1,5)={1,3,5}
U(2,5)=6+2+7+4=19
U(1,5)=1+2+4=7
Input
The first line contains two integers n and
m (3<=n<=200,1<=m<=n*(n-1)/2)—the number of points and the number
of edges. The second line contains n integers, (w_1, w_2, w_3,…,w_n(1<=w_i<=1E6)).
The next m lines, two integers x and y per line, indicate that there is an
undirected edge between x and y with a length of 1. The next line is a integer
q((1<=q<=1E5))-the number of questions, the next q lines, two integers x
and y per line, indicate a question.
Output
q lines, one integer per line.
Sample Input
5 6
1 6 2 7 4
1 2
1 3
2 3
2 4
3 5
4 5
3
2 5
5 1
2 4
Sample Output
19
7
13
Resume
求两点间所有最短路径所包含的点的集合。
Analysis
- 暴力思路
Dijkstra 加 手写集合去重。每次松弛成功就替换被松弛的点的目标集合,若恰好相等则取并集。
注意:STL的set太慢了(tcl). - Floyd 算法
首先求出两点间最短路(算法任意)。
那么只要符合 (dis[i][j] = dis[i][k] + dis[k][j]) 那么点(k)就在点(i) 和点(j)的最短路上。
Code
//////////////////////////////////////////////////////////////////////
//Target: 2018校赛 I - Geodetic
//@Author: Pisceskkk
//Date: 2019-5-7
//////////////////////////////////////////////////////////////////////
#include<bits/stdc++.h>
#define N 210
#define Min(a,b) (a<b?a:b)
using namespace std;
int n,m,w[N],q,ans[N][N],gra[N][N],x,y;
int main(){
memset(gra,0x3f,sizeof(gra));
memset(ans,0,sizeof(ans));
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
gra[i][i] = 0;
}
for(int i=1;i<=m;i++){
scanf("%d %d",&x,&y);
gra[x][y] = gra[y][x] = 1;
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
gra[i][j] = Min(gra[i][j],gra[i][k]+gra[k][j]);
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
for(int k=1;k<=n;k++){
if(gra[i][k] + gra[k][j] == gra[i][j]){
ans[i][j] += w[k];
}
}
}
}
scanf("%d",&q);
while(q--){
scanf("%d %d",&x,&y);
printf("%d
",ans[x][y]);
}
return 0;
}