-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkey act
301 lines (297 loc) · 5.69 KB
/
key act
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// main.c
// 20200404
//
// Created by 梅佳伟 on 2020/4/4.
// Copyright © 2020 梅佳伟. All rights reserved.
//
#include<stdio.h>
#include<stdlib.h>
#define MaxVerNum 150
#define N 150
#define Infinity 65535
typedef int Vertex;
typedef int WeightType;
struct ENode
{
Vertex V1,V2;
WeightType Weight;
};
typedef struct ENode *PtrToENode;
typedef PtrToENode Edge;
typedef struct AdjacentVNode *PtrToAdjVNode;
struct AdjacentVNode
{
Vertex V;
WeightType Weight;
PtrToAdjVNode Next;
};
struct VNode
{
PtrToAdjVNode FirstEdge;
};
typedef struct GNode *LGraph;
struct GNode
{
int Nv,Ne;
struct VNode AdjList[N];
LGraph G2;
};
LGraph CreateGraph(int VerNum)
{
LGraph G;
G=malloc(sizeof(struct GNode));
G->Nv=VerNum;
G->Ne=0;
Vertex V;
for(V=1;V<=G->Nv;V++)
{
G->AdjList[V].FirstEdge=NULL;
}
return G;
}
void InsertEdge(LGraph G, Edge E)
{
PtrToAdjVNode NewNode;
NewNode = malloc(sizeof(struct AdjacentVNode));
NewNode->V=E->V2;
NewNode->Weight=E->Weight;
NewNode->Next=G->AdjList[E->V1].FirstEdge;
G->AdjList[E->V1].FirstEdge=NewNode;
}
LGraph BuildGraph()
{
LGraph G[2];
int n,m;
scanf("%d",&n);
G[0]=CreateGraph(n);
G[1]=CreateGraph(n);
scanf("%d",&m);
G[0]->Ne=m;
G[1]->Ne=m;
Edge E;
E=malloc(sizeof(struct ENode));
Vertex V;
for(V=1;V<=G[0]->Ne;V++)
{
scanf("%d %d %d",&E->V1,&E->V2,&E->Weight);
InsertEdge(G[0],E);
Vertex tmp;
tmp=E->V1;
E->V1=E->V2;
E->V2=tmp;
InsertEdge(G[1],E);
}
G[0]->G2=G[1];
return G[0];
}
struct MGNode
{
int Nv,Ne;
WeightType G[MaxVerNum][MaxVerNum];
};
typedef struct MGNode *PtrToMGNode;
typedef PtrToMGNode MGraph;
struct act
{
Vertex Num;
int Earliest;
int Lastest;
};
typedef enum {False,True} Bool;
typedef struct Node *PtrToNode;
struct Node
{
struct act Data;
PtrToNode Next;
};
typedef PtrToNode Position;
struct QNode
{
Position Front,Rear;
int MaxSize;
};
typedef struct QNode *Queue;
Queue CreateQueue(int MaxSize)
{
Queue Q;
Q=malloc(sizeof(struct QNode));
Q->MaxSize=MaxSize;
Q->Front=NULL;
Q->Rear=NULL;
return Q;
}
Bool IsEmpty(Queue Q)
{
if(Q->Front==NULL)
{
return True;
}
else
return False;
}
void Enqueue(Queue Q,struct act V)
{
PtrToNode NewNode;
NewNode=malloc(sizeof(struct Node));
NewNode->Data=V;
NewNode->Next=NULL;
if(!IsEmpty(Q))
{
Q->Rear->Next=NewNode;
Q->Rear=NewNode;
}
else
{
Q->Front=NewNode;
Q->Rear=NewNode;
}
}
struct act Dequeue(Queue Q)
{
Position FrontCell;
FrontCell=Q->Front;
if(Q->Front==Q->Rear)
{
Q->Front=NULL;
Q->Rear=NULL;
}
else
{
Q->Front=Q->Front->Next;
}
struct act FrontElem;
FrontElem=FrontCell->Data;
free(FrontCell);
return FrontElem;
}
void FindKeyAct(struct act V[],LGraph G,int max);
Bool TopSort(LGraph G,Vertex TopOrder[])
{
Queue Q = CreateQueue(G->Nv);
struct act V[N],V1; int Indegree[MaxVerNum]; PtrToAdjVNode W;
int i;
for(i=1;i<=G->Nv;i++)
{
V[i].Num=i;
Indegree[V[i].Num]=0;
}
for(i=1;i<=G->Nv;i++)
{
for(W=G->AdjList[V[i].Num].FirstEdge;W!=NULL;W=W->Next)
{
Indegree[W->V]++;
}
}
for(i=1;i<=G->Nv;i++)
{
V[i].Earliest=0;
if(Indegree[V[i].Num]==0)
{
Enqueue(Q,V[i]);
}
}
int cnt=0;
while(!IsEmpty(Q))
{
V1=Dequeue(Q);
TopOrder[cnt++]=V1.Num;
for(W=G->AdjList[V1.Num].FirstEdge;W!=NULL;W=W->Next)
{
if(V1.Earliest+W->Weight>V[W->V].Earliest)
{
V[W->V].Earliest=V1.Earliest+W->Weight;
}
if(--Indegree[W->V]==0)
{
Enqueue(Q,V[W->V]);
}
}
}
if(cnt!=G->Nv)
{
printf("0");
return False;
}
else
{
int i,mark,max=V[cnt].Earliest;
for(i=1;i<=G->Nv;i++)
{
if(V[i].Earliest>max)
{
mark=i;
max=V[i].Earliest;
}
}
printf("%d\n",max);
FindKeyAct(V,G,max);
return True;
}
}
void FindKeyAct(struct act V[],LGraph G,int max)
{
int i,Indegree[N];
PtrToAdjVNode W;
Queue Q = CreateQueue(G->Nv);
for(i=1;i<=G->Nv;i++)
{
Indegree[V[i].Num]=0;
}
for(i=1;i<=G->Nv;i++)
{
for(W=G->AdjList[V[i].Num].FirstEdge;W!=NULL;W=W->Next)
{
Indegree[V[i].Num]++;
}
}
for(i=1;i<=G->Nv;i++)
{
if(Indegree[V[i].Num]==0)
{
V[i].Lastest=max;
}
else
{
V[i].Lastest=Infinity;
}
if(Indegree[V[i].Num]==0)
{
Enqueue(Q,V[i]);
}
}
struct act V1;
while(!IsEmpty(Q))
{
V1=Dequeue(Q);
for(W=G->G2->AdjList[V1.Num].FirstEdge;W!=NULL;W=W->Next)
{
if(V1.Lastest-W->Weight<V[W->V].Lastest)
{
V[W->V].Lastest=V1.Lastest-W->Weight;
}
if(--Indegree[W->V]==0)
{
Enqueue(Q,V[W->V]);
}
}
}
for(i=1;i<=G->Nv;i++)
{
for(W=G->AdjList[V[i].Num].FirstEdge;W!=NULL;W=W->Next)
{
if(V[W->V].Lastest-V[i].Earliest==W->Weight)
{
printf("%d->%d\n",V[i].Num,W->V);
}
}
}
}
int main (int argc, char *argv[])
{
LGraph G;
G=BuildGraph();
Vertex TopOrder[N];
TopSort(G, TopOrder);
return 0;
}