5-12 How Long Does It Take (25分)

5-12 How Long Does It Take   (25分)
Given the relations of all the activities of a project, you are supposed to find the earliest completion time of the project.


Input Specification:


Each input file contains one test case. Each case starts with a line containing two positive integers NN (\le 100≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N-1N−1), and MM, the number of activities. Then MM lines follow, each gives the description of an activity. For the i-th activity, three non-negative numbers are given: S[i], E[i], and L[i], where S[i] is the index of the starting check point, E[i] of the ending check point, and L[i] the lasting time of the activity. The numbers in a line are separated by a space.


Output Specification:


For each test case, if the scheduling is possible, print in a line its earliest completion time; or simply output "Impossible".


Sample Input 1:


9 12
0 1 6
0 2 4
0 3 5
1 4 1
2 4 1
3 5 2
5 4 0
4 6 9
4 7 7
5 7 4
6 8 2
7 8 4
Sample Output 1:


18
Sample Input 2:


4 5
0 1 1
0 2 2
2 1 3
1 3 4
3 2 5
Sample Output 2:


Impossible








思路:计算每个点完成的时间,并在有多条路径到这个点的情况选择花费时间最长的。

整个图里花费时间最长的点才是全部完成的时间。



#include<stdio.h>    
#include<stdlib.h>    
#include<algorithm>  
#define MAX 101  
using namespace std;    
  
  
struct ENode{  
    int v1,v2;  
    int weight;  
};  
typedef struct ENode *PtrToENode;  
  
struct AdjVNode{  
    int adjv;  
    int weight;    
    struct AdjVNode *next;  
};  
typedef struct AdjVNode *PtrToAdjVNode;  
  
typedef struct FirstVNode{  
    PtrToAdjVNode FirstEdge;  
}Adjlist[MAX];  
  
struct GNode{  
    int Nv;  
    int Ne;  
    Adjlist G;  
};  
typedef struct GNode *PtrToGNode;  
  
PtrToGNode create(int N)  
{  
    PtrToGNode Graph = (PtrToGNode)malloc(sizeof(struct GNode));  
    Graph->Nv = N;  
    Graph->Ne = 0;  
    for(int i = 0; i < N; i++)  
    {  
        Graph->G[i].FirstEdge = NULL;  
    }  
      
    return Graph;  
}  
  
void Insert(PtrToGNode Graph,PtrToENode E)  
{  
    PtrToAdjVNode NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));  
      
    NewNode->adjv = E->v2;  
    NewNode->weight = E->weight;    
    NewNode->next = Graph->G[E->v1].FirstEdge;  
    Graph->G[E->v1].FirstEdge = NewNode;  
}  
    int earliest[MAX];  
bool Topsort(PtrToGNode Graph,int N)  
{  
    int Indegree[N],cnt = 0;  
    PtrToAdjVNode W;  
    int queue[N];/*数组模拟队列*/ 
    int rear = -1,front = -1;  
    for(int i = 0; i < N; i++)  
    {  
        Indegree[i] = 0;  
    }  
      
    for(int V = 0; V < N; V++)  
    {  
        for(W = Graph->G[V].FirstEdge; W ; W = W->next)  
        {  
            Indegree[W->adjv]++;  
        }  
    }  
      
    for(int V = 0; V < N; V++)/*将起点的顶点初始化*/   
    {  
        if(Indegree[V] == 0)  
        {  
            queue[++rear] = V;  
            earliest[V] = 0;/*起点花费时间都为0*/  
        }  
    }  
    int V;  
    while(front < rear)  
    {  
        V = queue[++front];  
        cnt++;/*记录出队点的个数*/ 
        for(W = Graph->G[V].FirstEdge; W ; W = W->next)  
        {  
            if(W->weight + earliest[V] > earliest[W->adjv])/*更新最大花费时间*/ 
            {  
                earliest[W->adjv] = W->weight + earliest[V];  
            }  
          
            if(--Indegree[W->adjv] == 0)  
            {  
                queue[++rear] = W->adjv;  
            }  
          
        }  
      
    }  
    if(cnt != N)/*出队的点不等于顶点数,则说明图里有回路*/ 
    {  
        return false;  
    }  
    else  
    return true;  
}  
  
  
int main(void)  
{  
    int N,M;  
    scanf("%d%d",&N,&M);  
    PtrToGNode G = create(N);  
    PtrToENode E = (PtrToENode)malloc(sizeof(struct ENode));  
      
    for(int i = 0; i < M; i++)  
    {  
        scanf("%d%d%d",&E->v1,&E->v2,&E->weight);  
        Insert(G,E);  
    }  
      
    bool flag = Topsort(G,N);  
      
    if(flag == true)  
    {  
        int max = earliest[0];  
        for(int i = 0; i < N; i++)/*整个图里最大时间完成的点才是最终完成的时间*/    
        {  
            if(earliest[i] > max)  
            {  
                max = earliest[i];  
            }  
        }  
        printf("%d",max);  
    }  
    else  
    {  
        printf("Impossible");  
    }  
    return 0;  
}  



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值