链接:http://acm.hdu.edu.cn/showproblem.php?pid=4109
题解:cpu能够并行处理一些指令,且每次处理的时间为1s,但是给出限制条件a,b,c 就是指令a和指令b之间必须隔c(s)才是安全的,同时a指令必须在b指令之前。
没有限制的指令就是入度为0的指令,用一个队列来维护完成某条指令最早的时间,因为一些指令不得不因为一些限制而延时。
类似于拓扑排序,入度为0的点入队。
//#include <bits/stdc++.h>
#include <queue>
#include <vector>
#include <cstdio>
using namespace std;
const int maxn = 1005;
int a, b;
struct edge {
int to;
int cost;
};
vector<edge> G[maxn];
int indeg[maxn];
int mint[maxn];
queue<int> que;
void topo() {
int time = 0;
for (int i = 0; i < a; i ++) {
if(indeg[i] == 0) {
que.push(i);
mint[i] = 1;
}
}
while(!que.empty()) {
int cur = que.front();
que.pop();
for (int i = 0, qs = G[cur].size(); i < qs; i ++) {
int to = G[cur][i].to;
int cost = G[cur][i].cost;
mint[to] = max(mint[to], mint[cur] + cost);
indeg[to] --;
if(indeg[to] == 0) {
que.push(to);
}
}
}
for (int i = 0; i < a; i ++) {
if(time < mint[i]) {
time = mint[i];
}
}
printf("%d\n", time);
}
int main() {
int x;
edge tmp;
// freopen("1.in", "r", stdin);
while (scanf("%d%d", &a, &b) != EOF) {
while (b --) {
scanf("%d%d%d", &x, &tmp.to, &tmp.cost);
G[x].push_back(tmp);
indeg[tmp.to] ++;
}
topo();
for (int i = 0; i < a; i ++) {
G[i].clear();
indeg[i] = 0;
mint[i] = 0;
}
}
return 0;
}
输入:
10 6 1 2 9 2 4 2 2 3 4 5 1 3 6 2 1 0 1 4 10 3 1 2 3 2 0 2 1 4 2 10 2 1 5 10 2 5 2 1 0 0 0 2 1 1 0 1 6 3 1 2 3 1 0 4 1 5 3 4 2 1 2 3 3 1 4 5 3 2 3 2 3 1 4 1 0 6 5 3 1 2 100 0 4 1 4 3 1 |
输出:
18 6 11 1 0 2 5 8 13 101 |