Instrction Arrangement HDU - 4109 (拓扑排序*)

本文探讨了在计算机组织与架构课程中遇到的指令依赖性问题,如WAR、WAW、RAW,并提出了通过调整指令顺序和增加气泡操作来最小化CPU运行时间的解决方案。使用邻接表和拓扑排序算法,确保在无限核心的强CPU上并行执行指令时,避免了因指令间距离不足而产生的危险。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Instrction Arrangement

 HDU - 4109 

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 2
1 2 1
3 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.

       
#include <bits/stdc++.H>
using namespace std;

queue<int> q;
int head[1111], cnt, indegree[1111], weight[1111];
struct Edge{
	int to, v, w;
}edge[11111];

void addedge(int u, int v, int w) {
	edge[cnt].to = head[u];
	edge[cnt].v = v;
	edge[cnt].w = w;
	head[u] = cnt++;
}

void init() {
	memset(head, -1, sizeof(head)); //邻接表 
	memset(indegree, 0, sizeof(indegree));// 入度 
	memset(weight, 0, sizeof(weight)); //最早开始时间 
	cnt = 0;
}

void tuopupaixu(int n) {
	
	for (int i = 0; i < n; ++i) {
		if (indegree[i] == 0) {
			//printf ("i: %d\n", i);
			q.push(i);
			indegree[i] = -1;
		}
	}
	while (!q.empty()) {
		
		int now = q.front();
		q.pop();
		//printf ("now: %d\n", now);
		for (int i = head[now]; ~i; i = edge[i].to) {
			int v = edge[i].v;
			--indegree[v];
			weight[v] = max(weight[now] + edge[i].w, weight[v]); //更新早开始时间 
			if (indegree[v] == 0)  {
				q.push(v);
				indegree[v] = -1;
			}
		}
		
	}
	/*for (int i = 0; i < n; ++i) {
		printf ("%d ", weight[i]);
	}
	puts("");*/
}

int main() {
	
	int n, m;
	while (~scanf ("%d %d", &n, &m)) {
		init();
		int u, v, w;
		for (int i = 1; i <= m; ++i) {
			scanf ("%d %d %d", &u, &v, &w);
			++indegree[v];
			w = max(w, 1); //每个事件的持续时间是1,所以即使安全时间小于1也要保证能完成事件 
			addedge(u, v, w);
		}
		for (int i = 0; i < n; ++i) {
			if (head[i] == -1) {
				addedge(i, n, 1); // 添加汇点方便求解,每个事件持续时间是1 
			}
		}
		tuopupaixu(n);
		printf ("%d\n", weight[n]); //汇点的最早开始时间=它的最晚开始时间 
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值