洛谷P1119 灾后重建
floyd的一道比较好的题目
#include <iostream> #include <cstdio> #include <cstring> using namespace std; //Mystery_Sky // #define maxn 300 #define maxm 1000000 #define INF 0x3f3f3f3f int road[maxn][maxn]; //int map[maxn][maxn]; int fix[maxm]; int n, m, w, u, v, q, t; int now; inline void floyd() { for(int i = 0; i < n; i++) for(int j = 0; j < n; j++) road[i][j] = min(road[i][j], road[i][now] + road[now][j]); now++; } inline void print() { if(fix[u] > t || fix[v] > t || road[u][v] == INF) printf("-1\n"); else printf("%d\n", road[u][v]); } int main() { scanf("%d%d", &n, &m); for(int i = 0; i < n; i++) scanf("%d", &fix[i]); memset(road, INF, sizeof(road)); for(int i = 1; i <= n; i++) road[i][i] = 0; for(int i = 1; i <= m; i++) { scanf("%d%d%d", &u, &v, &w); road[u][v] = road[v][u] = w; } scanf("%d", &q); for(int i = 1; i <= q; i++) { scanf("%d%d%d", &u, &v, &t); while (fix[now] <= t && now < n) floyd(); print(); } return 0; }
唯愿,青春不辜负梦想,未来星辰闪耀