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

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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章