题解
typedef pair<int, int> PII;
const int N = 2010;
int dist[N];
bool st[N];
class Solution {
public:
int findMinimumTime(vector<vector<int>>& tasks) {
int n = tasks.size();
unordered_map<int, vector<PII>> graph;
int ub = 0;
for(int i = 0; i < n; i++){
int a = tasks[i][0], b = tasks[i][1], c = tasks[i][2];
// s[b]>=s[a-1]+c
graph[a - 1].push_back({b, c});
ub = max(ub, b);
}
for(int i = 1; i <= ub; i++) {
// s[i]>=s[i-1]
graph[i - 1].push_back({i, 0});
// s[i]-s[i-1]<=1 -> s[i-1]>=s[i]-1
graph[i].push_back({i - 1, -1});
}
spfa(graph);
return dist[ub];
}
private:
void spfa(unordered_map<int, vector<PII>>& graph) {
queue<int> q;
memset(dist, -0x3f, sizeof dist);
dist[0] = 0;
q.push(0);
st[0] = true;
while(!q.empty()) {
int t = q.front();
q.pop();
st[t] = false;
for(auto&tup: graph[t]) {
int j = tup.first, w = tup.second;
if(dist[j] < dist[t] + w) {
dist[j] = dist[t] + w;
if(!st[j]) {
q.push(j);
st[j] = true;
}
}
}
}
}
};