topic
https://nanti.jisuanke.com/t/41288
The meaning of problems
N cities given level of risk and a adjacency matrix describing road information. After the interrogation of m, Q (u, v) does not pass through the shortest in the case where w is greater than the degree of danger between urban cities.
answer
Bare Floyd, do not drop that kind of dimension. edge [k] [i] [j] indicates that the update with the first k cities. Then the degree of danger according to the first city to row order from small to large, then the edge through such arrays sequentially updated, to update the top-k in FIG cities must not exceed comprises a k-th degree of danger City. Finally, ask for while looking for the greatest city in the k-1 ~ w range just fine.
In fact, this question is a naked Floyd, but to be honest it is not enough understanding of Floyd's deep, just stay in the board. . . .
#include <iostream> #include <cstring> #include <string> #include <algorithm> #include <cmath> #include <cstdio> #include <queue> #include <stack> #include <map> #include <bitset> #define ull unsigned long long #define met(a, b) memset(a, b, sizeof(a)) #define lowbit(x) (x&(-x)) #define MID (l + r) / 2 #define ll long long using namespace std; const int inf = 0x3f3f3f3f; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e6 + 3; const int maxn = 1e5 + 7; const int N = 110; struct node { int data, id; bool operator < (const node &a) const { return data < a.data; } }arr[220]; int edge[220][220][220]; int main() { int T, sp = 0; scanf("%d", &T); while(T--) { int n, m; scanf("%d%d", &n, &m); arr[0].data = 0; for(int i = 1; i <= n; i++) { scanf("%d", &arr[i].data); arr[i].id = i; } sort(arr+1, arr+1+n); for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d", &edge[0][i][j]); } } for(int k = 1; k <= n; k++) { int t = arr[k].id; for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { edge[k][i][j] = min(edge[k-1][i][j], edge[k-1][i][t] + edge[k-1][t][j]); } } } printf("Case #%d:\n", ++sp); for(int i = 1; i <= m; i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); int k = 0; for(int i = 1; i <= n; i++) if(arr[i].data <= w) k = i; printf("%d\n", edge[k][u][v]); } } return 0; }