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 N (≤100), the number of activity check points (hence it is assumed that the check points are numbered from 0 to N−1), and M, the number of activities. Then M 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
Pseudocode
void TopSort()
{ for(图中的每个顶点V)
if(Indegree[V] == 0)
Enqueue(V, Q); Earlist[V] = 0;
while(!IsEmpty(Q)){
V=Dequeue(Q); cnt++;
for(V的每个邻接点W){
更新Indegree[W];
if(--[Indegree[W]==0) Enqueue(Q);
}
}
if(cnt!=V) Impossible;
else printf(Max Earlist[V]);
}
**Code **
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 100
#define INFINITY 35565
typedef int Vertex;
typedef int WeightType;
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
WeightType Weight[MaxVertexNum][MaxVertexNum];
int Earlist[MaxVertexNum];
int InDegree[MaxVertexNum];
};
typedef PtrToGNode MGraph;
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1, V2;
WeightType W;
};
typedef PtrToENode Edge;
MGraph CreateGraph(int N){
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = N;
Vertex V, W;
for(V=0; V<Graph->Nv; V++){
Graph->Earlist[V] = -1;
Graph->InDegree[W] = 0;
}
for(V=0; V<Graph->Nv; V++){
for(W=0; W<Graph->Nv; W++){
Graph->Weight[V][W] = -1;
}
}
return Graph;
}
void InSert( Edge E, MGraph Graph){
Graph->Weight[E->V1][E->V2] = E->W;
Graph->InDegree[E->V2]++;
}
MGraph BuildGraph(){
MGraph Graph;
int N;
scanf("%d", &N);
Graph = CreateGraph(N);
scanf("%d", &(Graph->Ne));
int i;
Edge E;
E = (Edge)malloc(sizeof(struct ENode));
for(i=0; i<Graph->Ne; i++){
scanf("%d %d %d", &(E->V1), &(E->V2), &(E->W));
InSert(E, Graph);
}
return Graph;
}
void TopSort(MGraph Graph){
int Queue[Graph->Nv]; int ptr1=0; int ptr2=0;
Vertex V, W;
int cnt=0; int EarlistTime=0;
for(V=0; V<Graph->Nv; V++){
if(Graph->InDegree[V] == 0){
Queue[ptr2++] = V;
Graph->Earlist[V] = 0;
}
}
while(ptr1 != ptr2){
V = Queue[ptr1++]; cnt++;
for(W=0; W<Graph->Nv; W++){
if( Graph->Weight[V][W]!=-1 && (Graph->Earlist[V] + Graph->Weight[V][W])>Graph->Earlist[W]){
Graph->Earlist[W] = Graph->Earlist[V] + Graph->Weight[V][W];
}
if(Graph->Weight[V][W]!=-1 && --(Graph->InDegree[W])==0 ){
Queue[ptr2++] = W;
}
}
}
if(cnt != Graph->Nv){
printf("Impossible");
} else {
for(V=0; V<Graph->Nv; V++){
if(Graph->Earlist[V] > EarlistTime){
EarlistTime = Graph->Earlist[V];
}
}
printf("%d", EarlistTime);
}
}
int main(){
MGraph Graph;
Graph = BuildGraph();
TopSort(Graph);
return 0;
}