classSolution { public: int INF = 0x3f3f3f3f; intbf(vector<vector<int>>& g, int src, int dst, int k){ int n = g.size(); vector<int> dist(g.size(), INF); dist[src] = 0; while (k--) { vector<int> tmp(dist); for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { dist[j] = min(dist[j], tmp[i] + g[i][j]); } } } return dist[dst]; } intfindCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k){ vector<vector<int>> g(n, vector<int>(n, INF)); for (int i = 0; i < n; ++i) g[i][i] = 0; for (auto& e : flights) g[e[0]][e[1]] = e[2]; int ret = bf(g, src, dst, k+1); return ret == INF ? -1 : ret; } };
intbf(vector<vector<int>>& edg, int src, int dst, int k){ int n = edg.size(); vector<int> dist(edg.size(), INF); dist[src] = 0; while (k--) { vector<int> tmp(dist); for (int i = 0; i < n; ++i) { for (auto& e : edg) { int x = e[0], y = e[1], w = e[2]; dist[y] = min(dist[y], tmp[x] + w); } } } return dist[dst]; }
intfindCheapestPrice(int n, vector<vector<int>>& flights, int src, int dst, int k){ int ret = bf(flights, src, dst, k+1); return ret == INF ? -1 : ret; } };