Instrction Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1955 Accepted Submission(s): 821
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.
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.
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 2 1 2 1 3 4 1
Sample Output
2HintIn 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
Recommend
赤裸裸的求关键路径,用拓扑排序实现。
第一次用c++写出来,发个博文纪念一下。
#include <iostream>
#include <ctime>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1005;
struct node{
int to,val;
};
vector<node>G[maxn];
int i,j,k,m,n;
int in[maxn],t[maxn];
void init(){
int u,v,w;
for (i=0; i<n; i++) {
in[i] = t[i] = 0;
G[i].clear();
}
for (i=1; i<=m; i++) {
cin >> u >> v >> w;
G[u].push_back(node{v,w});
in[v]++;
}
}
void tuopu(){
queue<int>que;
int u,v,w;
for (i=0; i<n; i++) if (in[i]==0) {
que.push(i);
t[i]++;
}
while (!que.empty()) {
u = que.front();
que.pop();
for (i=0; i<G[u].size(); i++) {
v = G[u][i].to;
w = G[u][i].val;
t[v] = max(t[v],t[u]+w);
if (--in[v]==0) que.push(v);
}
}
}
void output(){
int ans = 0;
for (i=0; i<n; i++) ans = max(ans,t[i]);
cout << ans << endl;
}
int main(){
std::ios::sync_with_stdio(false);
while (cin >> n >> m) {
init();
tuopu();
output();
}
return 0;
}
关键路径与拓扑排序

本文介绍了一种利用拓扑排序解决关键路径问题的方法,并通过一个具体的编程实例展示了如何通过增加虚拟操作来确保指令间的安全距离,以避免依赖关系导致的危害。
5628

被折叠的 条评论
为什么被折叠?



