08-图9 关键活动(C)

 这道题,最优方法是邻接矩阵,为什么,因为他的一个优点:方便找任一顶点的所有“邻接点”(有边直接相连的顶点), 方便计算任一顶点的“度”(从该点发出的边数为“出度”,指向该点的边数为“入度”).而这道题便是运用了这一特点。

测试点提示内存(KB)用时(ms)结果得分
0sample 4条简单路径选11762

答案正确

15 / 15
1单起点和单终点,2条关键路径3601

答案正确

3 / 3
2多起点和多终点3562

答案正确

3 / 3
3不可行3762

答案正确

1 / 1
4最大N,简单回路不可行3802

答案正确

4 / 4
5最大N,随机,可行3682

答案正确

4 / 4

 

假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。

比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同时开设,因为它们有先后的依赖关系,比如C程序设计和数据结构两门课,必须先学习前者。

但是需要注意的是,对一组子任务,并不是任意的任务调度都是一个可行的方案。比如方案中存在“子任务A依赖于子任务B,子任务B依赖于子任务C,子任务C又依赖于子任务A”,那么这三个任务哪个都不能先执行,这就是一个不可行的方案。

任务调度问题中,如果还给出了完成每个子任务需要的时间,则我们可以算出完成整个工程需要的最短时间。在这些子任务中,有些任务即使推迟几天完成,也不会影响全局的工期;但是有些任务必须准时完成,否则整个项目的工期就要因此延误,这种任务就叫“关键活动”。

请编写程序判定一个给定的工程项目的任务调度是否可行;如果该调度方案可行,则计算完成整个工程项目需要的最短时间,并输出所有的关键活动。

输入格式:

输入第1行给出两个正整数N(≤100)和M,其中N是任务交接点(即衔接相互依赖的两个子任务的节点,例如:若任务2要在任务1完成后才开始,则两任务之间必有一个交接点)的数量。交接点按1~N编号,M是子任务的数量,依次编号为1~M。随后M行,每行给出了3个正整数,分别是该任务开始和完成涉及的交接点编号以及该任务所需的时间,整数间用空格分隔。

输出格式:

如果任务调度不可行,则输出0;否则第1行输出完成整个工程项目需要的时间,第2行开始输出所有关键活动,每个关键活动占一行,按格式“V->W”输出,其中V和W为该任务开始和完成涉及的交接点编号。关键活动输出的顺序规则是:任务开始的交接点编号小者优先,起点编号相同时,与输入时任务的顺序相反。

输入样例:

7 8
1 2 4
1 3 3
2 4 5
3 4 3
4 5 1
4 6 6
5 7 5
6 7 2

输出样例:

17
1->2
2->4
4->6
6->7

 注意要求:起点编号相同时,与输入时任务的顺序相反。这一个可能很难理解,但是请观察

输入条件,M是子任务的数量,依次编号为1~M。随后M行,每行给出了3个正整数。

也可以理解为从第一个任务输入到最后一个任务,所以注定为有序输入,莫要多想。

我的AC:

 

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define INFITY 100000
#define MaxVertex 105
typedef struct ENode *Edge;
struct ENode{
	int V1, V2;
	int Weight;
};

typedef struct GNode *MGraph;
struct GNode{
	int Nv;
	int Ne;
	int G[MaxVertex][MaxVertex];
};

typedef struct TaskNode *Vertex;
struct TaskNode{
	int InDegree, OutDegree;
	int EarlyTime, LastTime;
};

typedef struct Node *QNode;
struct Node{
	int data;
	QNode Next;
};

typedef struct QNode *Queue;
struct QNode{
	QNode Front;
	QNode Rear;
};

void Build_Graph(MGraph M, Vertex Task);
MGraph Init_Graph();
void Insert_Graph(MGraph M, Edge E);
Vertex Init_TaskNode(int N);
Queue Init_Queue();
void Add_Q(Queue Q, int data);
int Delete_Q(Queue Q);
bool IsEmpty_Q(Queue Q);
bool TopSort_EarlyTime(MGraph M, Vertex Task);
void TopSort_LastTime(MGraph M, Vertex Task);
int Max_time(Vertex Task, int N);
void Print_path(MGraph M, Vertex Task);

int main()
{
	MGraph M;
	Vertex Task;
	M = Init_Graph();
	Task = Init_TaskNode(M ->Nv);
	Build_Graph(M, Task);
	if(TopSort_EarlyTime(M, Task)){
		printf("%d\n", Max_time(Task, M ->Nv));
		TopSort_LastTime(M, Task);
//		for(int i = 0; i <M ->Nv; i++){
//			printf("Task[%d].EarlyTime = %d, Task[%d].LastTime = %d\n", i, Task[i].EarlyTime,i, Task[i].LastTime);
//		}
		Print_path(M, Task);
	}else{
		printf("0\n");
	}
	return 0;
}
void Print_path(MGraph M, Vertex Task)
{
	for(int i = 0; i < M ->Nv; i++){
		if(Task[i].EarlyTime == Task[i].LastTime){
			for(int j = M->Nv - 1; j >= 0; j--){
				if(Task[j].EarlyTime == Task[j].LastTime && M ->G[i][j] == Task[j].LastTime - Task[i].EarlyTime){
					printf("%d->%d\n", i + 1, j + 1);
				}
			}
		}
	}
	return ;
}
bool TopSort_EarlyTime(MGraph M, Vertex Task)
{
	Queue Q;
	int V = 0;
	int num = 0;
	Q = Init_Queue();
	for(V = 0; V < M ->Nv; V++){
		if(!Task[V].InDegree){
			Add_Q(Q, V);
		}
	}
	while(!IsEmpty_Q(Q)){
		V = Delete_Q(Q);
		num++;
		for(int j = 0; j < M ->Nv; j++)
			if(M ->G[V][j] != INFITY){
				if(--Task[j].InDegree == 0){
					Add_Q(Q, j);
				}
				if(Task[j].EarlyTime < Task[V].EarlyTime + M ->G[V][j]){
					Task[j].EarlyTime = Task[V].EarlyTime + M ->G[V][j];
				}
			}	
	}
	if(num != M ->Nv){
		return false;
	}else{
		return true;
	}
}
void TopSort_LastTime(MGraph M, Vertex Task)
{
	Queue Q;
	int V = 0;
	Q = Init_Queue();
	for(V = 0; V < M ->Nv; V++){
		if(!Task[V].OutDegree){
			Add_Q(Q, V);
		}
	}
	while(!IsEmpty_Q(Q)){
		V = Delete_Q(Q);
		for(int i = 0; i < M ->Nv; i++){	
			if(M ->G[i][V] != INFITY){
				if(--Task[i].OutDegree == 0){
					Add_Q(Q, i);
				}
				if(Task[i].LastTime > Task[V].LastTime - M ->G[i][V]){
					Task[i].LastTime = Task[V].LastTime - M ->G[i][V];
				}
			}
		}	
	}
}
int Max_time(Vertex Task, int N)
{
	int time = 0, index;
	for(int i = 0; i < N; i++){
		if(Task[i].EarlyTime > time){
			time = Task[i].EarlyTime;
			index = i;
		}
	}
	Task[index].LastTime = time;
	return time;
}
void Build_Graph(MGraph M, Vertex Task)
{
	Edge E;
	int i;
	E = (Edge)malloc(sizeof(struct ENode));
	for(i = 0; i < M ->Ne; i++){
		scanf("%d %d %d", &E ->V1, &E ->V2, &E ->Weight);
		E ->V1--; E ->V2--;
		Insert_Graph(M, E);
		Task[E ->V2].InDegree++;
		Task[E ->V1].OutDegree++;
	}
}
MGraph Init_Graph()
{
	MGraph M;
	M = (MGraph)malloc(sizeof(struct GNode));
	scanf("%d %d", &M ->Nv, &M ->Ne);
	for(int i = 0; i < M ->Nv; i++){
		for(int j = 0; j < M ->Nv; j++){
			M ->G[i][j] = INFITY;
		}
	}
	return M;
}
Vertex Init_TaskNode(int N)
{
	Vertex Task;
	Task = (Vertex)malloc(sizeof(struct TaskNode) * N);
	for(int i = 0; i < N; i++){
		Task[i].InDegree = 0;
		Task[i].OutDegree = 0;
		Task[i].EarlyTime = 0;
		Task[i].LastTime = INFITY;
	}
	return Task;
}
void Insert_Graph(MGraph M, Edge E)
{
	M ->G[E ->V1][E ->V2] = E ->Weight;
}
Queue Init_Queue()
{
	Queue Q;
	Q = (Queue)malloc(sizeof(struct QNode));
	Q ->Front = NULL;
	Q ->Rear  = NULL;
	return Q;
}
void Add_Q(Queue Q, int Vertex)
{
	QNode Node;
	Node = (QNode)malloc(sizeof(struct Node));
	Node ->data = Vertex;
	Node->Next = NULL;
	if(!(Q ->Front) && !(Q ->Rear)){
		Q ->Front = Node;
		Q ->Rear = Node;
	}else{
		Q ->Rear ->Next = Node;
		Q ->Rear = Node;
	}
}
int Delete_Q(Queue Q)
{
	if(IsEmpty_Q(Q)){
		printf("很遗憾,是空的!\n");
		return -1;
	}else{
		QNode Temp;
		int Vertex;
		Temp = Q ->Front;
		Q ->Front = Temp ->Next;
		Vertex = Temp ->data;
		if(IsEmpty_Q(Q)){
			Q ->Rear = NULL;
		}else{
			free(Temp);
		}
		return Vertex;
	}
}
bool IsEmpty_Q(Queue Q)
{
	return (Q ->Front == NULL);
}

  • 42
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值