链接
https://www.luogu.com.cn/problem/P1119
题目
知识点
floyd算法思路
看题解,讲的差不多,本篇就是记录下写过的题。 唯一要注意的就是当遍历k(本代码用cnt代替)时,ij都要从0取到n-1。代码
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<string.h>
#include<limits.h>
#include<string>
#include<vector>
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
#define int long long
const int INF = 1e9;
const int N = 210;
int times[N];
int dp[N][N];signed main()
{IOS;int n, m;cin >> n >> m;for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)dp[i][j] = INF;for (int i = 0; i < n; i++)cin >> times[i];for (int i = 0; i < m; i++){int a, b, v;cin >> a >> b >> v;dp[a][b] = dp[b][a] = v;}int q; cin >> q;int cnt = 0;while (q--){int x, y, t; cin >> x >> y >> t;while (cnt<n and times[cnt] <= t){//这里很关键for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)dp[i][j] = min(dp[i][j], dp[i][cnt] + dp[cnt][j]);cnt++;}if (dp[x][y] == INF or times[x]>t or times[y]>t)cout << -1;else cout << dp[x][y];cout << '\n';}return 0;
}