HDU 4109 Instrction Arrangement (拓扑or差分约束求关键路径)

来源:互联网 发布:arpu值算法 编辑:程序博客网 时间:2024/06/07 17:26




Instrction Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1871    Accepted Submission(s): 791


Problem Description
Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW.
If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance.
The definition of the distance between two instructions is the difference between their beginning times.
Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction.
Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.
 

Input
The input consists several testcases.
The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.
The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1.
 

Output
Print one integer, the minimum time the CPU needs to run.
 

Sample Input
5 21 2 13 4 1
 

Sample Output
2
Hint
In the 1st ns, instruction 0, 1 and 3 are executed;In the 2nd ns, instruction 2 and 4 are executed.So the answer should be 2.
 

Source
2011 Alibaba-Cup Campus Contest
 

Recommend
lcy
 


题目大意:有n条指令和m条指令之间的依赖关系,对于每一组关系u v w表示指令v必须在指令u之后w执行,且执行需要w个时间单位,问你要执行n条指令最少需要消耗的时间(多条无依赖关系的指令可同时执行)

思路:

1.裸的关键路径, 拓扑排序求, E数组代表最早的结束时间

2.差分约束, 建立超级源点, 只有一个约束, 求最长路, 答案就是到源点的最大值-到源点的最小值+1

拓扑排序代码:

#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#include <queue>using namespace std;const int maxn = 1e3 + 5;struct node{    int to, w;    node(){}    node(int tt, int ww) : to(tt), w(ww){}};vector<node> v[maxn];int e[maxn], deg[maxn], n, m, x, y, z;void TOP(){    queue<int> q;    for(int i = 0; i < n; i++)        if(!deg[i])            q.push(i), e[i] = 1;    while(!q.empty())    {        int u = q.front();        q.pop();        for(int i = 0; i < v[u].size(); i++)        {            int to = v[u][i].to, w = v[u][i].w;            if(e[to] < e[u]+w)                e[to] = e[u]+w;            if(--deg[to] == 0)                q.push(to);        }    }}int main(){    while(~scanf("%d%d", &n, &m))    {        memset(deg, 0, sizeof(deg));        memset(e, 0, sizeof(e));        for(int i = 0; i < maxn; i++)            v[i].clear();        for(int i = 1; i <= m; i++)        {            scanf("%d%d%d", &x, &y, &z);            v[x].push_back(node(y, z));            deg[y]++;        }        TOP();        int ans = 0;        for(int i = 0; i < n; i++)            ans = max(ans, e[i]);        printf("%d\n", ans);    }    return 0;}



Instrction Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1871    Accepted Submission(s): 791


Problem Description
Ali has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW.
If the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance.
The definition of the distance between two instructions is the difference between their beginning times.
Now we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction.
Your job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time.
 

Input
The input consists several testcases.
The first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.
The following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N - 1.
 

Output
Print one integer, the minimum time the CPU needs to run.
 

Sample Input
5 21 2 13 4 1
 

Sample Output
2
Hint
In the 1st ns, instruction 0, 1 and 3 are executed;In the 2nd ns, instruction 2 and 4 are executed.So the answer should be 2.
 

Source
2011 Alibaba-Cup Campus Contest
 

Recommend
lcy
 
阅读全文
0 0
原创粉丝点击