题目:
假定一个工程项目由一组子任务构成,子任务之间有的可以并行执行,有的必须在完成了其它一些子任务后才能执行。“任务调度”包括一组子任务、以及每个子任务可以执行所依赖的子任务集。
比如完成一个专业的所有课程学习和毕业设计可以看成一个本科生要完成的一项工程,各门课程可以看成是子任务。有些课程可以同时开设,比如英语和C程序设计,它们没有必须先修哪门的约束;有些课程则不可以同时开设,因为它们有先后的依赖关系,比如C程序设计和数据结构两门课,必须先学习前者。
但是需要注意的是,对一组子任务,并不是任意的任务调度都是一个可行的方案。比如方案中存在“子任务A依赖于子任务B,子任务B依赖于子任务C,子任务C又依赖于子任务A”,那么这三个任务哪个都不能先执行,这就是一个不可行的方案。
任务调度问题中,如果还给出了完成每个子任务需要的时间,则我们可以算出完成整个工程需要的最短时间。在这些子任务中,有些任务即使推迟几天完成,也不会影响全局的工期;但是有些任务必须准时完成,否则整个项目的工期就要因此延误,这种任务就叫“关键活动”。
请编写程序判定一个给定的工程项目的任务调度是否可行;如果该调度方案可行,则计算完成整个工程项目需要的最短时间,并输出所有的关键活动。
输入格式
输入第1行给出两个正整数N(≤100)和M,其中N是任务交接点(即衔接相互依赖的两个子任务的节点,例如:若任务2要在任务1完成后才开始,则两任务之间必有一个交接点)的数量。交接点按1N编号,M是子任务的数量,依次编号为1M。随后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
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#define INFINITY 65535
#define MaxVertexNum 100
int earliest[MaxVertexNum];//记录每个事件到达的最早时间
int latest[MaxVertexNum];//记录每个事件到达的最晚时间
int indegree[MaxVertexNum];//记录每个事件的入度
int outdegree[MaxVertexNum];//记录每个事件的出度
typedef struct GNode* PtrToGNode;
struct GNode {
int Nv;
int Ne;
int G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;
typedef struct ENode* PtrToENode;
struct ENode {
int V1, V2;
int Weight;
};
typedef PtrToENode Edge;
MGraph CreateGraph(int VertexNum) {
int V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 1; V <= Graph->Nv; V++)
for (W = 1; W <= Graph->Nv; W++)
Graph->G[V][W] = INFINITY;
return Graph;
}
void InsertEdge(MGraph Graph, Edge E) {
Graph->G[E->V1][E->V2] = E->Weight;
}
MGraph BuildGraph() {
MGraph Graph;
Edge E;
int V;
int Nv, i;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &(Graph->Ne));
if (Graph->Ne != 0) {
E = (Edge)malloc(sizeof(struct ENode));
for (i = 1; i <= Graph->Ne; i++) {
scanf("%d%d%d", &E->V1, &E->V2, &E->Weight);
outdegree[E->V1]++;
indegree[E->V2]++;
InsertEdge(Graph, E);
}
free(E);
}
return Graph;
}
int TopSort(MGraph Graph) {
int i;
int* Queue=(int*)malloc(sizeof(int)*(Graph->Nv+1));
int rear = 0, front = 0;
int count = 0;//记录当前收录到集合中的元素
for (i = 1; i <= Graph->Nv; i++) {
if (indegree[i] == 0) {
Queue[++rear] = i;
}
}
int V;
while (front != rear) {
V = Queue[++front];
count++;
for (i = 1; i <= Graph->Nv; i++ ) {
if (Graph->G[V][i] != INFINITY) {
if (--indegree[i] == 0)
Queue[++rear] = i;
if (earliest[i] < earliest[V] + Graph->G[V][i])
earliest[i] = earliest[V] + Graph->G[V][i];
}
}
}
free(Queue);
if (count == Graph->Nv) {
int MaxSchedule=0;
for (i = 1; i <= Graph->Nv; i++) {
if (earliest[i] > MaxSchedule)
MaxSchedule = earliest[i];
}
return MaxSchedule;
}
else
return -1;
}
void ReverseTopSort(MGraph Graph, int EarliestTime) {
int i, j;
int* Queue = (int*)malloc(sizeof(int) * (Graph->Nv+1));
int rear = 0, front = 0;
for (i = 1; i <= Graph->Nv; i++) {
latest[i] = EarliestTime;//这里latest要全部初始化为最大值
if (outdegree[i] == 0)
Queue[++rear] = i;
}
int V;
while (front != rear) {
V = Queue[++front];
for (i = 1; i <= Graph->Nv; i++) {
if (Graph->G[i][V] != INFINITY) {//注意这里的节点顺序为Graph->G[i][V]
if (--outdegree[i] == 0)
Queue[++rear] = i;
if (latest[i] > latest[V] - Graph->G[i][V])
latest[i] = latest[V] - Graph->G[i][V];
}
}
}
free(Queue);
}
int main() {
int i, j;
MGraph Graph=BuildGraph();
int EarliestTime = TopSort(Graph);
if (EarliestTime != -1) {
printf("%d\n", EarliestTime);
ReverseTopSort(Graph, EarliestTime);
for (i = 1; i <= Graph->Nv; i++)
//关键路径的判别条件是earliest[i]+Graph->G[i][j]=latest[j]
for (j = Graph->Nv; j >= 1; j--) {
if ((Graph->G[i][j] != -1) && (earliest[i] + Graph->G[i][j] == latest[j]))
printf("%d->%d\n", i, j);
}
}
else
printf("0");
system("pause");
return 0;
}