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
/* How Long Does It Take*/
/*判断是否为拓扑排序,即是否存在回路*/
/*邻接表,有向图*/
/*Earliest[0]= 0
Earliest[w] = max{Earliest[v]+weight<v,W>}*/
/*最后输出最大的Eariest即为最短时间*/
#include<stdio.h>
#include<stdlib.h>
/*边的定义*/
typedef struct Enode* Edge;
struct Enode {
int V1, V2;
int weight;
};
/*邻接点的定义*/
typedef struct Adjvnode* prttoadjvnode;
struct Adjvnode {
int adjv;
int weight;
prttoadjvnode next;
};
/*顶点定义*/
typedef struct Vnode {
prttoadjvnode firstedge;
}adjlist[102];
/*图节点定义*/
struct Gnode {
int Nv;/*顶点数*/
int Ne;/*边数*/
adjlist G; /*邻接表*/
};
typedef struct Gnode* Lgraph;
/*队列定义*/
typedef struct Qnode* ptrtoQnode;
struct Qnode {
int* Data;/*data数组,*/
int front, rear;/*队列头尾指针*/
int maxsize;/*容量*/
};
typedef ptrtoQnode Queue;
int AddQ(Queue Q, int x);
Queue CreateQueue(int maxsize);
int isempty(Queue Q);
int DeleteQ(Queue Q);
Lgraph buildgraph(int N, int M);
void insertedge(Lgraph graph, Edge E);
Lgraph creatgraph(int size);
int Topsort(Lgraph graph);
int main() {
int N,M;
scanf("%d %d", &N, &M);
Lgraph graph = buildgraph(N, M);
int flag = Topsort(graph);
if (flag == -1) printf("Impossible");
else printf("%d", flag);
return 0;
}
Lgraph creatgraph(int size) {
Lgraph graph = (Lgraph)malloc(sizeof(struct Gnode));
graph->Nv = size;
graph->Ne = 0;
for (int i = 0; i < size; i++) {
graph->G[i].firstedge = NULL;
}
return graph;
}
void insertedge(Lgraph graph, Edge E) { /*有向图*/
/*v1,v2*/
prttoadjvnode newnode = (prttoadjvnode)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;
}
Lgraph buildgraph(int N, int M) {
Lgraph graph;
graph = creatgraph(N);
graph->Ne = M;
if (graph->Ne != 0) {
Edge E = (Edge)malloc(sizeof(struct Enode));
for (int i = 0; i < graph->Ne; i++) {
scanf("%d %d %d", &E->V1, &E->V2, &E->weight);
insertedge(graph, E);
}
}
return graph;
}
int Topsort(Lgraph graph) {
int* indegree = (int*)malloc(graph->Nv * sizeof(int));
int* earliest = (int*)malloc(graph->Nv * sizeof(int));
int v;
prttoadjvnode w,j;
Queue Q = CreateQueue(graph->Nv);
for (v = 0; v < graph->Nv; v++) { /*初始化为0*/
earliest[v] = 0;
}
for (v = 0; v < graph->Nv; v++) { /*初始化为0*/
indegree[v] = 0;
}
for (v = 0; v < graph->Nv; v++) { /*遍历图获得入度*/
for (w = graph->G[v].firstedge; w; w = w->next) {
indegree[w->adjv]++;
}
}
//printf("\n");
for (v = 0; v < graph->Nv; v++) { /*入度为0 的入队*/
if (indegree[v] == 0) {
AddQ(Q, v);
}
}
int cnt = 0;
while (isempty(Q) == 0) {
int temp_weight = 0;
v = DeleteQ(Q);
cnt++;
for (int i = 0; i < graph->Nv; i++) { /*遍历图找到 指向V的邻接点*/
for (j = graph->G[i].firstedge; j; j = j->next) {
if (j->adjv == v) {
if (earliest[i] + j->weight > temp_weight) {
temp_weight = earliest[i] + j->weight;
}
}
}
}
earliest[v] = temp_weight;
for (w = graph->G[v].firstedge; w; w = w->next) {/*对v的每个邻接点*/
if (--indegree[w->adjv] == 0) {
AddQ(Q, w->adjv);
}
}
}/*while结束以后v指向最后一个顶点*/
// printf("cnt is %d num is %d\n", cnt, graph->Nv);
int min = 0;
for (int i = 0; i < graph->Nv; i++) {
if (earliest[i] > min) {
min = earliest[i];
}
}
if (cnt != graph->Nv) return -1;
else return min;
}
Queue CreateQueue(int maxsize) {
Queue Q = (Queue)malloc(sizeof(struct Qnode));
Q->Data = (int*)malloc(maxsize * sizeof(int));
Q->front = Q->rear = 0;
Q->maxsize = maxsize;
return Q;
}
int AddQ(Queue Q, int x) {
Q->rear = (Q->rear + 1) % (Q->maxsize); /*若1%1,则值为0*/
Q->Data[Q->rear] = x;
return 1;
}
int isempty(Queue Q) {
return(Q->rear == Q->front);
}
int DeleteQ(Queue Q) {
Q->front = (Q->front + 1) % Q->maxsize;
return Q->Data[Q->front];
}