Instrction Arrangement (HDU 4109)

Topic Link

Title 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 Format

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 Format

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

SAMPLE INPUT

5 2
1 2 1
3 4 1

Sample Output

2

analysis

Subject to the effect of a given instruction n, m rule, each instruction execution time of 1 second, each rule indicates that the safety distance (safety time difference) between the two instructions, the minimum time required to complete all instructions required.
Seeking longest path after the topological sort critical path problem template, FIG converted into AOE FIG AOV, with t i array store instruction completion time. Note Enter a safe distance if the case should be assigned to ≤0 1, but not too much water may be entitled to this card.

Source

#include <bits/stdc++.h>
#define MAXN 10005
using namespace std;
struct Edge{
	int v,w,next;
	Edge(){};
	Edge(int _v,int _w,int _next){
		v=_v,w=_w,next=_next;
	};
}edge[MAXN];
int EdgeCount,head[MAXN];
int n,m,in[MAXN],t[MAXN];
void addEdge(int u,int v,int w)
{
	edge[++EdgeCount]=Edge(v,w,head[u]);
	head[u]=EdgeCount;
}
void TopSort()
{
	queue<int> q;
	for(int i=0;i<n;i++)
		if(!in[i]){
			t[i]=1;
			q.push(i);
		}
	while(!q.empty()){
		int u=q.front();
		q.pop();
		for(int i=head[u];i;i=edge[i].next){
			int v=edge[i].v,w=edge[i].w;
			in[v]--;
			t[v]=max(t[v],t[u]+w);
			if(!in[v])q.push(v);
		}
	}
}
int main()
{
	while(scanf("%d%d",&n,&m)!=EOF){
		EdgeCount=0;
		for(int i=0;i<MAXN;i++)in[i]=head[i]=t[i]=0;
		for(int i=1;i<=m;i++){
			int u,v,w;
			scanf("%d%d%d",&u,&v,&w);
			if(w<=0)w=1;
			addEdge(u,v,w);
			in[v]++;
		}
		TopSort();
		int ans=-9999999;
		for(int i=0;i<n;i++)
			ans=max(ans,t[i]);
		printf("%d\n",ans);
	}
} 
Published 19 original articles · won praise 0 · Views 127

Guess you like

Origin blog.csdn.net/weixin_43960284/article/details/105253488