HDU 4109 Instrction Arrangement

版权声明:未经本人同意,禁止转载。 https://blog.csdn.net/qq_34022601/article/details/84628110

Instrction Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2821    Accepted Submission(s): 1123

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.

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.


题意大概是:有编号从0--N-1个指令,他们之间存在M个依赖关系,必须完成之前的指令才能运行现在的指令,但是为了保证运算结果的正确性,指令之间要存在时间差,x,y指令开始之间要间隔z ns。执行指令本身需要1 ns;指令可以并行处理.

解题思路:

明显的依赖关系,提示我们要用拓扑排序求路径, 并行处理,求完成任务的时间,是要求最长路径(关键路径).


#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
struct Node{
	vector <int> adj;
};
/*
任务执行本身要1ns
安全距离是指开始时间之差 
*/
int indegree[1005];		//入度数组 
int time[1002][1002];	//安全距离 
void Topsort(int n,Node *g)
{
	int dis[1005]={0};
	int ans=0;
	queue <int> q;
	for (int i=0;i<n;i++)
	{
		if (indegree[i]==0)
		{
			dis[i]=1; 
			q.push(i);		
		}
	}
	vector <int> :: iterator it;
	while (!q.empty())
	{
		int v=q.front();
		q.pop();
		for (it=g[v].adj.begin();it!=g[v].adj.end();it++)
		{
			indegree[*it]--;
			if (indegree[*it]==0)
				q.push(*it);
			dis[*it]=max(dis[*it],dis[v]+time[v][*it]+1);
			ans=max(ans,dis[*it]);
		}
	}
	printf ("%d\n",ans);
}
int main()
{
	int n,m,x,y,z;
	while (~scanf ("%d%d",&n,&m))
	{
		Node g[1005];
		memset(time,0,sizeof(time));
		memset(indegree,0,sizeof(indegree));
		for (int i=0;i<m;i++)
		{
			scanf ("%d%d%d",&x,&y,&z);
			if (time[x][y]==0)
			{
				indegree[y]++;
				time[x][y]=z-1;
				g[x].adj.push_back(y);
			}
			if (z-1<time[x][y])		//去重边 
				time[x][y]=z-1;
		}
		Topsort(n,g);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_34022601/article/details/84628110