数据结构基础—关键活动

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

AC代码如下:

扫描二维码关注公众号,回复: 932592 查看本文章
/*-----------------------------------------------------------------
 Earliest[j] = max{Earliest[i] + C(i,j)};
 Latest[i] = min{Lastest[j] - C(i,j)}
 Key Activity = Latest[i] - Earliest[i]
 利用拓扑排序进行改造
 即使下一个邻接点的入度不为0,一样需要更新它的Earliest的值,只是不会入队,只有在入度为0的时候才可以入队
 -----------------------------------------------------------------*/
#include <iostream>
#include <stack>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#define Infinity 1 << 30
using namespace std;
typedef int vertex;
typedef int degree;
typedef int weightType;
struct GNode{
    vertex Nv;
    vertex Ne;
    int** map;
};
typedef struct GNode* ptrToGraph;
typedef ptrToGraph LGraph;
/*---------------------建立图----------------------*/
LGraph buildGraph(){
    LGraph graph = new GNode;
    cin >> graph->Nv >> graph->Ne;
    /* 建立二维数组 */
    graph->map = new vertex*[graph->Nv];
    for(vertex i = 0; i < graph->Nv; i++)
        graph->map[i] = new vertex[graph->Nv];
    
    /* 一开始初始化成正无穷大*/
    for(int i = 0; i < graph->Nv; i++){
        for(int j = 0; j < graph->Nv; j++)
            graph->map[i][j] = Infinity;
    }
    /* 数值初始化,有向图 */
    vertex V1,V2;
    weightType Weight;
    for(int i = 0; i < graph->Ne; i++){
        cin >> V1 >> V2 >> Weight;
        V1--;V2--;
        graph->map[V1][V2] = Weight;
    }
    return graph;
}
/*---------------------test----------------------*/
void test(int *Indegree, int N){
    for(int i = 0; i < N; i++)
        cout << *(Indegree + i) << " ";
    cout << endl;
}
/*---------------------拓扑排序----------------------*/
void TopSort(LGraph graph){
    vertex i,j;
    int cnt = 0;
    queue<vertex> qOfEarliest,qOfLatest;
    int Indegree[graph->Nv];
    int Earliest[graph->Nv];
    
    int Outdegree[graph->Nv];
    int Latest[graph->Nv];
    
    for(i = 0; i < graph->Nv; i++){
        Indegree[i] = 0;
        Earliest[i] = 0;
        Outdegree[i] = 0;
        Latest[i] = Infinity;
    }
    
    for(i = 0; i < graph->Nv; i++){
        for(j = 0; j < graph->Nv; j++){
            if(graph->map[i][j] != Infinity){
                Indegree[j] ++;
                Outdegree[i] ++;
            }
        }
    }
    /* 一开始把入度为0的全部入队列 */
    for(i = 0; i < graph->Nv; i++)
        if(Indegree[i] == 0)
            qOfEarliest.push(i);
    
    while (! qOfEarliest.empty()) {
        vertex temp = qOfEarliest.front();
        qOfEarliest.pop();
        cnt++;
        for(i = 0; i < graph->Nv; i++){
            if(graph->map[temp][i] != Infinity){
                Earliest[i] = max(Earliest[i], Earliest[temp] + graph->map[temp][i]);
                if((--Indegree[i]) == 0)
                /* 这个if一定要在上一个if的下面,我一开始的错就在放一起了,所以导致一个测试点一直过不了 */
                    qOfEarliest.push(i); /* 入度为0才入队,并不是直接临接点就入队 */
//                  test(Indegree, graph->Nv);
//                  test(Earliest, graph->Nv);
            }
        }
    }
    
    /* 以下解决了多个起点和多个终点第四个评测点 */
    int maxNumber = 0;
    for(i = 0; i < graph->Nv; i++)
        maxNumber = max(maxNumber,Earliest[i]);
    
    /* 一开始把出度为0的全部入队列,并且赋初值 */
    for(i = 0; i < graph->Nv; i++)
        if(Outdegree[i] == 0){
            qOfLatest.push(i);
            Latest[i] = maxNumber;
            /* 上面这一行,我一开始写成了latest[i] = earliest[i],调试了好久哈哈哈*/
        }
    
    
    while (! qOfLatest.empty()) {
        vertex temp = qOfLatest.front();
        qOfLatest.pop();
        for(i = 0; i < graph->Nv; i++){
            if(graph->map[i][temp] != Infinity){
                Latest[i] = min(Latest[i], Latest[temp] - graph->map[i][temp]);
                if((--Outdegree[i]) == 0)
                    qOfLatest.push(i);
            }
        }
    }
   
    /* 输出 */
    if (cnt != graph->Nv)
        cout << 0 << endl;
    else{
        cout << maxNumber << endl;
        /* 寻找关键边,对每一条边,key <i,j> = 顶点j最晚-顶点i最早-map(i,j) */
        for(i = 0; i < graph->Nv; i++){
            for(j = graph->Nv - 1; j >= 0; j--){
                if(graph->map[i][j] != Infinity){
                    if(Latest[j] - Earliest[i] - graph->map[i][j] == 0)
                        cout << i+1 << "->" << j+1 << endl;
                }
            }
        }
    }
    
}
/*---------------------------关键路径查找结束----------------------------------------*/
int main(int argc, const char * argv[]) {
    LGraph graph = buildGraph();
    TopSort(graph);
    delete graph;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_25175067/article/details/80109238