Instrction Arrangement
来源:互联网 发布:js 截取字符串后几位 编辑:程序博客网 时间:2024/05/18 06:31
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.
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.
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.
5 21 2 13 4 1
2
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.
题意:cpu每执行一个命令需1ns,给出n个命令,m个命令之间的间隔,求cpu执行完所有的命令的最短时间。
思路:拓扑排序, 然后dp求解最短时间
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;const int maxn=1005;int n, m;int dp[maxn];int du[maxn];int cost[maxn][maxn];int main(){ while(~scanf("%d%d", &n, &m)) { memset(dp, 0, sizeof(dp)); memset(du, 0, sizeof(du)); memset(cost, 0, sizeof(cost)); queue<int> q; int x, y, z; for(int i=0; i<m; i++) //建图 { scanf("%d%d%d", &x, &y, &z); cost[y][x]=z; du[y]++; } for(int i=0; i<n; i++) //将入度为0的操作设为1 { if(du[i]==0) { q.push(i); dp[i]=1; } } while(!q.empty()) { int x=q.front(); q.pop(); for(int i=0; i<n; i++) { if(cost[i][x]!=0) { dp[i]=max(dp[i], dp[x]+cost[i][x]); du[i]--; cost[i][x]=0; if(du[i]==0) q.push(i); } } } int ans=0; for(int i=0; i<n; i++) ans=max(ans, dp[i]); printf("%d\n", ans); } return 0;}
阅读全文
0 0
- Instrction Arrangement
- HDU 4109 Instrction Arrangement
- HDU4109 Instrction Arrangement @Z
- Hdu 4109 Instrction Arrangement
- HDU 4109 Instrction Arrangement
- HDU 4109 Instrction Arrangement
- 【hdoj 4109】Instrction Arrangement
- HDU 4109 Instrction Arrangement
- hdu4109拓扑Instrction Arrangement
- HDU_4109 Instrction Arrangement(拓扑)
- hdoj 4109--Instrction Arrangement(topsort)
- HDU4109 Instrction Arrangement 拓扑排序 关键路径
- hdu 4109 Instrction Arrangement(关键路径)
- HDU4109——Instrction Arrangement(关键路径)
- (关键路径)HDU 4109 Instrction Arrangement
- HDU 4109 Instrction Arrangement(差分约束)
- hdu 4109 Instrction Arrangement (差分约束)
- hdu 4109 Instrction Arrangement 拓扑排序 关键路径
- “bc: command not found ” 解决
- 51nod1264 线段相交
- Java对Samba进行上传与下载文件-jcifs
- spring 常用注解
- 学习笔记:日照day6数论总(meng)结(bi)
- Instrction Arrangement
- 深度优先搜索练习之神奇的矩环
- Java异常介绍
- hadoop学习笔记--10.hive安装与配置
- 常用地区手机号正则表达式
- 使用在hdfs上的文件导入到hive中
- 第一天总结
- 【DSP】TMS320F28035 GPIO例程
- 某公司要开发”天天灌水论坛”,请使用面向对象的思想,设计注册用户信息