08-图9 关键活动 (30分)

我的思路,正向以最短完成时间和入度为参数做一遍拓扑排序;逆向以最长完成时间和出度为参数完成一边拓扑排序。出现了几个问题:判定说多起点多种点判定有误,同时也有段错误;自己验证的时候,在完成第一遍拓扑排序之后,发现以邻接表存储的图,其表头的最后一个丢失了(比如有1~12个表头,做完一次拓扑排序之后,第12个就读不出来了…),一直未解决。


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

typedef struct _node{
	int name;
	int time;
	struct _node *next;
}Node;

typedef struct _head{
	int numberofin;
	int numberofout;
	struct _node *in;
	struct _node *out;
}Head;


typedef struct _que{
	int loction;
	struct _que *next;
}Que;

typedef struct _table{
	int know;
	int stime;
	int ltime;
}Table;

Table *Sheet;
Head *ArrayList;
int Check;
int Act;
Node *End;
int *Recordout;

void add_array(Node *Beginer,int name,int time);
void print_array(Node *Beginer);
void insert_que(Que *root,int i);
int  dele_que(Que *root);
void topshot();
void antitopshot();
void print_table();
void print_que(Que *root);
void print_keycheck();

int main(){
	scanf("%d %d",&Check,&Act);
	ArrayList=(Head*)malloc((Check+1)*sizeof(Head));
	Sheet=(Table*)malloc((Check+1)*sizeof(Table));
	Recordout=(int*)malloc((Check+1)*sizeof(int));
	int i,j,t,k;
	End=(Node*)malloc(sizeof(Node));
	for(k=0;k<Check+1;k++){
		ArrayList[k].numberofin=0;
		ArrayList[k].in=(Node*)malloc(sizeof(Node));
		ArrayList[k].in->next=End;
		
		ArrayList[k].numberofout=0;
		ArrayList[k].out=(Node*)malloc(sizeof(Node));
		ArrayList[k].out->next=End;
		
		Sheet[k].know=0;
		Sheet[k].stime=0;
		Sheet[k].ltime=10000;
		
		Recordout[k]=0;
	}
	
	for(k=0;k<Act;k++){
		scanf("%d %d %d",&i,&j,&t);
		add_array(ArrayList[i].out,j,t);  //储存的顺序是堆栈储存~ 
		add_array(ArrayList[j].in,i,t);   //堆栈储存 
		ArrayList[i].numberofout++;
		Recordout[i]++;
		ArrayList[j].numberofin++;	
	}	
	
//	for(k=0;k<Check+1;k++){
//		printf("%d: in=%d ",k,ArrayList[k].numberofin);
//		print_array(ArrayList[k].in);
//		printf("**");
//		printf(" out=%d ",ArrayList[k].numberofout);
//		print_array(ArrayList[k].out);
//		printf("\n");
//	}
//	
//	for(k=1;k<Check+1;k++){
//		printf("%d\n",Recordout[k]);
//	}
	
	topshot();
//	printf("AAA");
//	print_table();  //基本完工,最后想想怎么打印好了 
	//打印想法是,顺序从搜索sheet的每一个节点,如果stime等于ltime,从该点的入度里面找 入度节点的ltime+入度节点的time 等于 该节点ltime 的输出。 
	// 写不动了…睡觉了= - 
	
	return 0;
}

void topshot(){
	Que *root=(Que*)malloc(sizeof(Que));
	root->next=NULL;
	int k;
	int cnt=0;
	for(k=1;k<Check+1;k++){
		if(ArrayList[k].numberofin==0){
			insert_que(root,k);
			cnt++;		
		}
	}

	int result;
	Node *subhead=(Node*)malloc(sizeof(Node));
	while(1){
		result=dele_que(root);
		Sheet[result].know=1;
		if(result==-1){
			break;
		}
		subhead=ArrayList[result].out->next;
		while(subhead!=End){
			if(Sheet[subhead->name].know==0){
				--ArrayList[subhead->name].numberofin;
				if(ArrayList[subhead->name].numberofin==0){
					insert_que(root,subhead->name);
					cnt++;
				}
				if(Sheet[result].stime+subhead->time > Sheet[subhead->name].stime){
					Sheet[subhead->name].stime=Sheet[result].stime+subhead->time;
				}	
			}
			subhead=subhead->next;
		}
		
	}
	if(cnt!= Check){
		printf("0");
	}else{
		int biggest=0;
		int location=0;
		for(k=1;k<Check+1;k++){
			if(Sheet[k].stime>biggest){
				biggest=Sheet[k].stime;
				location=k;
			}
		}
		printf("%d\n",biggest);
		antitopshot();
//		print_table();
		print_keycheck(); 
		
	}
	
}

void antitopshot(){
	Que *root=(Que*)malloc(sizeof(Que));
	root->next=NULL;
	int k;
	for(k=1;k<Check+1;k++){
		Sheet[k].know=0;
	}
	for(k=1;k<Check+1;k++){
		if(ArrayList[k].numberofout==0){
			insert_que(root,k);
			Sheet[k].ltime=Sheet[k].stime;		
		}
	}  
	
	int result;
	Node *subhead=(Node*)malloc(sizeof(Node));
	while(1){
		result=dele_que(root);
		if(result==-1){
			break;
		}
		Sheet[result].know=1;
		
		subhead=ArrayList[result].in->next;
		while(subhead!=End){
			if(Sheet[subhead->name].know==0){
				--ArrayList[subhead->name].numberofout;
				if(ArrayList[subhead->name].numberofout==0){
					insert_que(root,subhead->name);
				}
				if(Sheet[result].ltime-subhead->time < Sheet[subhead->name].ltime){
					Sheet[subhead->name].ltime=Sheet[result].ltime-subhead->time;
				}	
			}
			subhead=subhead->next;
		}
		
	}
	
}

void add_array(Node *Beginer,int name,int time){
	Node *p=Beginer;
	Node *q=(Node*)malloc(sizeof(Node));
	q->name=name;
	q->time=time;
	q->next=Beginer->next;
	Beginer->next=q;
}

void print_array(Node *Beginer){
	Node *p=Beginer->next;
	while(p!=End){
		printf("name=%d time=%d% ",p->name,p->time);
		p=p->next;
	}
	
}

void insert_que(Que *root,int i){
	Que *p=root;
	while(p->next){
		p=p->next;
	}
	Que *q=(Que*)malloc(sizeof(Que));
	q->loction=i;
	p->next=q;
	q->next=NULL;
}

int  dele_que(Que *root){
	Que *p=root->next;
	int result=-1;
	if(p){
		result=p->loction;
		root->next=p->next;
		free(p);
	}
	return result;
}

void print_table(){
	int k=0;
	for(k=1;k<Check+1;k++){
		printf("%d: ",k);
		printf("know=%d stime=%d ltime=%d",Sheet[k].know,Sheet[k].stime,Sheet[k].ltime);
		printf("\n");
	}
}

void print_que(Que *root){
	Que *p=root->next;
	while(p){
		printf("%d ",p->loction);
		p=p->next;
	}
	printf("\n");
}

void print_keycheck(){

	int i;
	Node *result;
	for(i=1;i<Check+1;i++){
		
		if(Recordout[i]!=0){
			if(Sheet[i].stime==Sheet[i].ltime){
				if(ArrayList[i].out->next){
					result=ArrayList[i].out->next;
					while(result!=End){
						if(Sheet[result->name].stime == Sheet[result->name].ltime){
							printf("%d->%d\n",i,result->name);
						}
						result=result->next;
					}
				}
			}
			
		}
	}
}



08-图9 关键活动   (30分)

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

比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值