**
**
Description
Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each city has a risk of kidnapping or robbery.
Firdaws’s home locates in the city uu, and Fatinah’s home locates in the city vv.Now you are asked to find the shortest path from the city uu to the city vv that does not pass through any other city with the risk of kidnapping or robbery higher than ww, a threshold given by Firdaws.
Input Format
The input contains several test cases, and the first line is a positive integer TT indicating the number of test cases which is up to 5050.
For each test case, the first line contains two integers n~(1\le n\le 200)n (1≤n≤200) which is the number of cities, and q~(1\le q\le 2\times 10^4)q (1≤q≤2×10 4 ) which is the number of queries that will be given.The second line contains nn integers r_1, r_2, \cdots, r_nr 1 ,r 2 ,⋯,r n indicating the risk of kidnapping or robbery in the city 11 to nn respectively.Each of the following nn lines contains nn integers, the jj-th one in the ii-th line of which, denoted by d_{i,j}d i,j , is the distance from the city ii to the city jj.Each of the following qq lines gives an independent query with three integers u, vu,v and ww, which are described as above.We guarantee that 1\le r_i \le 10^51≤r i ≤10 5 , 1\le d_{i,j}\le 10^5~(i \neq j)1≤d i,j ≤10 5 (i =j), d_{i,i}=0d i,i =0 and d_{i,j}=d_{j,i}d i,j =d j,i .Besides, each query satisfies 1\le u,v\le n1≤u,v≤n and 1\le w\le 10^51≤w≤10 5 .
Output Format
For each test case, output a line containing Case #x: at first, where xx is the test case number starting from 11.Each of the following qq lines contains an integer indicating the length of the shortest path of the corresponding query.
样例输入
1
3 6
1 2 3
0 1 3
1 0 1
3 1 0
1 1 1
1 2 1
1 3 1
1 1 2
1 2 2
1 3 2
样例输出
Case #1:
0
1
3
0
1
2
一开始用的dijastra算法,但是每次询问都要调用一次会严重超时,所以后面想了想还是用floyd算法,将各城市的危险值从小到大排序,dp[i][j][k]表示从城市i到城市j借助排序后的前k个城市可以取到的最小距离,所以状态转换方程为dp[i][j][k] = min( dp[i][j][k-1] , dp[i][name[k]][k-1] + dp[name[k]][j][k-1]) .
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 300
int f[maxn][maxn][maxn];
int danger[maxn];
int name[maxn];
bool cmp(int a,int b){
return danger[a]<danger[b];
}
int main(){
int i, j, k, n, m, T, q;
int cur = 0;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&q);
for(i = 1; i <= n; i++){
name[i] = i;
scanf("%d",&danger[i]);
}
sort(name+1, name+1+n, cmp);
for(i = 1; i <= n; i++){
for(j = 1; j <= n; j++){
scanf("%d",&f[i][j][0]);
}
}
for(k = 1; k <= n; k++){
for(i = 1; i <= n; i++){
for(j = 1; j <= n; j++){
f[i][j][k] = min(f[i][j][k-1], f[i][name[k]][k-1]+f[name[k]][j][k-1]);
}
}
}
printf("Case #%d:\n",++cur);
while(q--){
int u, v ,w;
scanf("%d%d%d",&u,&v,&w);
int ans = 0;
for(i = 1; i <= n; i++){
if(danger[name[i]]<=w) ans = i;
}
printf("%d\n",f[u][v][ans]);
}
}
return 0;
}