-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path补充题目:1018 Public Bike Management.c
337 lines (296 loc) · 8.27 KB
/
补充题目:1018 Public Bike Management.c
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define INF 65535
#define ERROR -1
/* ——————无向图的邻接表定义开始—————— */
#define MaxVertexNum 501
typedef int Vertex;
typedef int WeightType;
typedef int DataType;
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1, V2;
WeightType time;
};
typedef PtrToENode Edge;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
WeightType time;
PtrToAdjVNode Next;
};
typedef struct LastVNode *PtrToLastVNode;
struct LastVNode {
Vertex LastV;
PtrToLastVNode Next;
};
typedef struct Vnode{
DataType BikeNum;
PtrToAdjVNode FirstEdge;
PtrToLastVNode FirstLastVertex; // 用于在Dijkstra算法中记录到该结点路径长度相同的所有上一跳结点编号
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph CreateGraph(int Nv);
void DestoryGraph( LGraph Graph );
void InsertEdge(LGraph Graph, Edge E);
void InsertLastVertex(LGraph Graph, Vertex V, Vertex Last);
void ClearLastVertex(LGraph Graph, Vertex V); // 清空传入的结点站当前记录的所有上一跳结点编号
/* ——————无向图的邻接表定义结束—————— */
/* ——————栈定义开始—————— */
typedef Vertex ElementType;
typedef int Position;
struct SNode {
ElementType *Data; /* 存储元素的数组 */
Position Top; /* 栈顶指针 */
int MaxSize; /* 堆栈最大容量 */
};
typedef struct SNode *Stack;
Stack CreateStack( int MaxSize );
void DestoryStack(Stack stack);
bool IsFull( Stack S );
bool Push( Stack S, ElementType X );
bool IsEmpty( Stack S );
ElementType Pop( Stack S );
int CopyData(Stack stack, ElementType *data); // 返回拷贝的data长度
/* ——————栈定义结束—————— */
Vertex problemStation;
DataType perfectNum;
bool collected[MaxVertexNum];
WeightType distTime[MaxVertexNum];
DataType minBackSum, minSendSum;
int steps;
Vertex ansPath[MaxVertexNum];
LGraph buildGraph();
Vertex findMinDist(LGraph graph);
void dijkstra(LGraph graph);
void dfs(LGraph graph, Stack stack, Vertex V, DataType backSum, DataType sendSum);
void printAns();
void solve(LGraph graph);
int main()
{
LGraph graph;
graph = buildGraph();
solve(graph);
DestoryGraph(graph);
return 0;
}
/* ——————相关数据结构函数实现开始—————— */
LGraph CreateGraph(int Nv)
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = Nv + 1;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; ++V) {
Graph->G[V].FirstEdge = NULL;
Graph->G[V].FirstLastVertex = NULL;
}
return Graph;
}
void DestoryGraph( LGraph Graph )
{
Vertex V;
PtrToAdjVNode Node;
PtrToLastVNode LNode;
if (!Graph) return;
for (V = 0; V < Graph->Nv; ++V) {
while (Graph->G[V].FirstEdge) {
Node = Graph->G[V].FirstEdge;
Graph->G[V].FirstEdge = Node->Next;
free(Node);
}
while (Graph->G[V].FirstLastVertex) {
LNode = Graph->G[V].FirstLastVertex;
Graph->G[V].FirstLastVertex = LNode->Next;
free(LNode);
}
}
free(Graph);
}
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V2; NewNode->time = E->time;
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V1; NewNode->time = E->time;
NewNode->Next = Graph->G[E->V2].FirstEdge;
Graph->G[E->V2].FirstEdge = NewNode;
}
void InsertLastVertex(LGraph Graph, Vertex V, Vertex Last)
{
PtrToLastVNode NewVertexNode;
NewVertexNode = (PtrToLastVNode)malloc(sizeof(struct LastVNode));
NewVertexNode->LastV = Last;
NewVertexNode->Next = Graph->G[V].FirstLastVertex;
Graph->G[V].FirstLastVertex = NewVertexNode;
}
void ClearLastVertex(LGraph Graph, Vertex V)
{
PtrToLastVNode Node;
while (Graph->G[V].FirstLastVertex) {
Node = Graph->G[V].FirstLastVertex;
Graph->G[V].FirstLastVertex = Node->Next;
free(Node);
}
Graph->G[V].FirstLastVertex = NULL;
}
Stack CreateStack( int MaxSize )
{
Stack S = (Stack)malloc(sizeof(struct SNode));
S->Data = (ElementType *)malloc(MaxSize * sizeof(ElementType));
S->Top = -1;
S->MaxSize = MaxSize;
return S;
}
void DestoryStack(Stack stack)
{
if (!stack) return;
if (stack->Data) free(stack->Data);
free(stack);
}
bool IsFull( Stack S )
{
return (S->Top == S->MaxSize-1);
}
bool Push( Stack S, ElementType X )
{
if ( IsFull(S) ) return false;
else {
S->Data[++(S->Top)] = X;
return true;
}
}
bool IsEmpty( Stack S )
{
return (S->Top == -1);
}
ElementType Pop( Stack S )
{
if ( IsEmpty(S) ) return ERROR;
else return ( S->Data[(S->Top)--] );
}
int CopyData(Stack stack, ElementType *data)
{
Position i;
for (i = 0; i <= stack->Top; ++i)
data[i] = stack->Data[i];
return i;
}
/* ——————相关数据结构函数实现结束—————— */
LGraph buildGraph()
{
LGraph graph;
Edge edge;
DataType maxCapacity;
int Nv, i; Vertex V;
scanf("%d %d", &maxCapacity, &Nv);
perfectNum = maxCapacity / 2;
graph = CreateGraph(Nv);
scanf("%d %d", &problemStation, &(graph->Ne));
for (V = 1; V < graph->Nv; ++V)
scanf("%d", &(graph->G[V].BikeNum));
edge = (Edge)malloc(sizeof(struct ENode));
for (i = 0; i < graph->Ne; ++i) {
scanf("%d %d %d", &(edge->V1), &(edge->V2), &(edge->time));
InsertEdge(graph, edge);
}
free(edge);
return graph;
}
Vertex findMinDist(LGraph graph)
{
Vertex minV, V;
WeightType minDist = INF;
for (V = 0; V < graph->Nv; ++V) {
if (!collected[V] && distTime[V] < minDist) {
minDist = distTime[V];
minV = V;
}
}
if (minDist < INF) return minV;
else return ERROR;
}
void dijkstra(LGraph graph)
{
Vertex V;
PtrToAdjVNode edge;
for (V = 0; V < graph->Nv; ++V) {
collected[V] = false;
distTime[V] = INF;
}
// 从问题站点向PBMC点反向遍历
distTime[problemStation] = 0;
while (true) {
V = findMinDist(graph);
if (V == ERROR || V == 0) break;
collected[V] = true;
for (edge = graph->G[V].FirstEdge; edge; edge = edge->Next) {
if (collected[edge->AdjV]) continue;
if (distTime[V] + edge->time < distTime[edge->AdjV]) {
distTime[edge->AdjV] = distTime[V] + edge->time;
ClearLastVertex(graph, edge->AdjV);
InsertLastVertex(graph, edge->AdjV, V);
}
else if (distTime[V] + edge->time == distTime[edge->AdjV])
InsertLastVertex(graph, edge->AdjV, V);
}
}
}
void dfs(LGraph graph, Stack stack, Vertex V, DataType backSum, DataType sendSum)
{
PtrToLastVNode last;
DataType minus;
if (V == problemStation) { // 只有当到达最终问题站时,才能知道这条路径发送的总车数及需要返回的车数
if (sendSum < minSendSum) {
minSendSum = sendSum;
minBackSum = backSum;
steps = CopyData(stack, ansPath);
}
else if (sendSum == minSendSum && backSum < minBackSum) {
minSendSum = sendSum;
minBackSum = backSum;
steps = CopyData(stack, ansPath);
}
return;
}
for (last = graph->G[V].FirstLastVertex; last; last = last->Next) {
Push(stack, last->LastV);
minus = graph->G[last->LastV].BikeNum - perfectNum;
if (minus < 0 && backSum + minus < 0) { // 如果该站车数少于半满状态,且该路径经过的所有前面站收集的总车数不足以使该站达到perfect
dfs(graph, stack, last->LastV, 0, sendSum - (backSum + minus));
}
else {
dfs(graph, stack, last->LastV, backSum + minus, sendSum);
}
Pop(stack);
}
}
void printAns()
{
Vertex V;
printf("%d 0", minSendSum);
for (V = 0; V < steps; ++V)
printf("->%d", ansPath[V]);
printf(" %d", minBackSum);
}
void solve(LGraph graph)
{
Stack stack;
dijkstra(graph); // 寻找最小路径
stack = CreateStack(graph->Nv);
minBackSum = minSendSum = INF;
dfs(graph, stack, 0, 0, 0); // 寻找最小路径相等时最优的路径
printAns();
DestoryStack(stack);
}