首页 > ACM题库 > HDU-杭电 > HDU 4109-Instrction Arrangement-拓扑排序-[解题报告]HOJ
2015
04-16

HDU 4109-Instrction Arrangement-拓扑排序-[解题报告]HOJ

Instrction Arrangement

问题描述 :

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.

输入:

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 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.

样例输入:

5 2
1 2 1
3 4 1

样例输出:

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<stdio.h>
#include<string.h>
#include<stdlib.h>
#define val 1001
struct link
{
	int v;
	int cost;
	struct link *next;
};
struct node
{
	struct link *next;
}vode[val];
int into[val],ee[val],n,m;//into表示入度,ee表示从最早完成该任务的时间,ee初始化为1
void topsort();//拓扑排序,边拓扑边求最长路径
void create_link();
int main()
{
	int i,j,ans;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		memset(into,0,sizeof(into));
		for(i=0;i<n;i++)
			ee[i]=1;
		create_link();
		ans=0;
		topsort();
		for(i=0;i<n;i++)
			if(ee[i]>ans) ans=ee[i];
		printf("%d\n",ans);
	}
	return 0;
}
void create_link()
{
	int i,j,a,b,c;
	struct link *s;
	for(i=0;i<n;i++)
		vode[i].next=NULL;
	for(i=0;i<m;i++)
	{
		scanf("%d %d %d",&a,&b,&c);
		into[b]++;
		s=(struct link*)malloc(sizeof(struct link));
		s->v=b;
		s->cost=c;
		s->next=vode[a].next;
		vode[a].next=s;
	}
}
void topsort()
{
	int i,j,v;
	struct link *p;
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
			if(into[j]==0) 
			{
				v=j;
				break;
			}
		into[v]=-1;
		p=vode[v].next;
		while(p!=NULL)
		{
			if(ee[p->v]<ee[v]+p->cost)//如果权值和更大,则替换
				ee[p->v]=ee[v]+p->cost;
			into[p->v]--;
			p=p->next;
		}
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/yobobobo/article/details/7835970


  1. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的

  2. [email protected]