码迷,mamicode.com
首页 > 其他好文 > 详细

08-图8 How Long Does It Take

时间:2018-11-18 19:31:23      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:sam   node   com   ram   esc   efi   sizeof   sem   tom   

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 (≤), 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

 1 //拓扑排序 
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #define Max 105
 5 #define ERROR -1
 6 
 7 int N, M;
 8 int Graph[Max][Max];        //邻接矩阵储存图 
 9 int Indegree[Max] = {0};    //记录每个结点的入度 
10 int time[Max] = {0};        //记录每个结点需要花费的时间 
11 
12 void init(){
13     for(int i=0; i<N; i++){
14         for(int j=0; j<N; j++){
15             Graph[i][j] = ERROR;
16         }
17     }
18 }
19 
20 void BuildG(){
21     scanf("%d%d", &N, &M);
22     init();
23     for(int i=0; i<M; i++){
24         int S, E, time;
25         scanf("%d%d%d", &S, &E, &time);
26         Graph[S][E] = time;
27         Indegree[E]++;
28     }
29 }
30 
31 typedef struct QNode{
32     int *data;
33     int rear, front;
34     int maxsize;
35 }*Queue;
36 
37 Queue BuildQ(){
38     Queue Q = (Queue)malloc(sizeof(struct QNode));
39     Q->data = (int *)malloc(sizeof(int)*N);
40     Q->front = Q->rear = 0;
41     Q->maxsize = N;
42     return Q;
43 }
44 
45 void AddQ(Queue Q, int x){
46     Q->rear = (Q->rear + 1)%Q->maxsize;
47     Q->data[Q->rear] = x;
48 }
49 
50 bool IsEmpty(Queue Q){
51     return Q->front == Q->rear;
52 }
53 
54 int DeleteQ(Queue Q){
55     Q->front = (Q->front + 1)%Q->maxsize;
56     return Q->data[Q->front];
57 }
58 
59 void TopSort(){
60     Queue Q = BuildQ();          //用队列记录入度为0的结点 
61     for(int i=0; i<N; i++){
62         if(Indegree[i]==0){
63             AddQ(Q, i);
64         }
65     }
66     int v, cnt=0, Time;
67     while(!IsEmpty(Q)){
68         v = DeleteQ(Q);
69         cnt++;
70         for(int i=0; i<N; i++){
71             if(Graph[v][i]!=ERROR){
72                 Indegree[i]--;
73                 if(Indegree[i]==0){
74                     AddQ(Q, i);
75                 }
76                 if(time[v] + Graph[v][i] > time[i]){
77                     time[i] = time[v] + Graph[v][i];
78                 }
79                 if(time[i]>Time){
80                     Time = time[i];
81                 }
82             } 
83         }
84     }
85     if(cnt<N){      //如果有几个结点不能被出队,则一定存在闭环,输出impossible 
86         printf("Impossible\n");
87     }
88     else printf("%d\n", Time);
89 }
90 
91 int main(){
92     BuildG();
93     TopSort();
94     return 0;
95 }

 

08-图8 How Long Does It Take

标签:sam   node   com   ram   esc   efi   sizeof   sem   tom   

原文地址:https://www.cnblogs.com/shin0324/p/9978716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!