1 条题解
-
0
找工作(Job Hunt S) 题解
思路分析
按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。
参考代码
#include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); long long d; int p, c, f, s; if (!(cin >> d >> p >> c >> f >> s)) return 0; vector<vector<pair<int, long long>>> adj(c + 1); for (int i = 0; i < p; ++i) { int u, v; cin >> u >> v; adj[u].push_back({v, d}); } for (int i = 0; i < f; ++i) { int u, v; long long t; cin >> u >> v >> t; adj[u].push_back({v, d - t}); } const long long NEG_INF = -(1LL << 58); vector<long long> dp(c + 1, NEG_INF); vector<int> in_q(c + 1, 0), cnt(c + 1, 0); queue<int> q; dp[s] = d; in_q[s] = 1; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); in_q[u] = 0; for (auto& edge : adj[u]) { int v = edge.first; long long w = edge.second; if (dp[u] + w > dp[v]) { dp[v] = dp[u] + w; cnt[v] = cnt[u] + 1; if (cnt[v] >= c) { cout << -1 << "\n"; return 0; } if (!in_q[v]) { in_q[v] = 1; q.push(v); } } } } long long ans = d; for (int i = 1; i <= c; ++i) ans = max(ans, dp[i]); cout << ans << "\n"; return 0; }
- 1
信息
- ID
- 8791
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者