解题思路:
对于每次输入的时间c判断
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N = 210; int n, m; int k; int times[N]; int dp[N][N]; bool flag[N]; int main() { cin >> n >> m; for (int i = 0; i < n; i ++ ) scanf("%d", ×[i]); memset(dp, 0x3f, sizeof dp);//floyd算法初始化 for (int i = 0; i < n; i ++ ) for (int j = 0; j < n; j ++ ) if (i == j) dp[i][j] = 0; while (m -- ) { int a, b, c; scanf("%d %d %d", &a, &b, &c); dp[a][b] = dp[b][a] = min(c, dp[a][b]); } int q; cin >> q; while (q -- ) { int c, a, b; scanf("%d %d %d", &a, &b, &c); if (times[a] > c || times[b] > c) //两个村庄任意一个超出时间说明一定不可到达 { puts("-1"); continue; } while (times[k] <= c && k < n)//题目给的times是依次递增的,所以我们不需要每次都将k置为0 { for (int i = 0; i < n; i ++ ) for (int j = 0; j < n; j ++ ) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); k ++ ; } if (dp[a][b] == 0x3f3f3f3f) puts("-1"); else printf("%d\n", dp[a][b]); } return 0; }
P1119 灾后重建
最新推荐文章于 2023-06-25 03:13:54 发布