hdu 4109 Instrction Arrangement (topo+关键路径) (附简单的测试数据)

链接: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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值