-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path补充题目:1003 Emergency.c
195 lines (170 loc) · 5.01 KB
/
补充题目:1003 Emergency.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define INFINITY 65535
#define ERROR -1
/* ——————无向图的邻接表定义开始—————— */
#define MaxVertexNum 500
typedef int Vertex;
typedef int WeightType;
typedef int DataType;
typedef struct ENode *PtrToENode;
struct ENode{
Vertex V1, V2;
WeightType RoadLength; // 路的长度
};
typedef PtrToENode Edge;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode{
Vertex AdjV;
WeightType RoadLength; // 路的长度
PtrToAdjVNode Next;
};
typedef struct Vnode{
PtrToAdjVNode FirstEdge;
DataType RescueNum; // 急救队数量
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode{
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
LGraph CreateGraph( int VertexNum );
void DestoryGraph( LGraph Graph );
void InsertEdge(LGraph Graph, Edge E);
/* ——————无向图的邻接表定义结束—————— */
Vertex source, destination;
bool collected[MaxVertexNum]; // 是否被收录
WeightType roadLength[MaxVertexNum]; // 路径长度
int shortestNum[MaxVertexNum]; // 最短路数量
DataType rescueNum[MaxVertexNum]; // 急救队数量
LGraph BuildGraph();
void init(LGraph Graph);
int FindMinDist(LGraph Graph);
void solve(LGraph Graph);
int main()
{
LGraph graph;
graph = BuildGraph();
solve(graph);
DestoryGraph(graph);
return 0;
}
LGraph CreateGraph( int VertexNum )
{
Vertex V;
LGraph Graph;
Graph = (LGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; ++V)
Graph->G[V].FirstEdge = NULL;
return Graph;
}
void DestoryGraph( LGraph Graph )
{
Vertex V;
PtrToAdjVNode Node;
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);
}
}
free(Graph);
}
void InsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V2; NewNode->RoadLength = E->RoadLength;
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V1; NewNode->RoadLength = E->RoadLength;
NewNode->Next = Graph->G[E->V2].FirstEdge;
Graph->G[E->V2].FirstEdge = NewNode;
}
LGraph BuildGraph()
{
LGraph graph;
Edge E;
Vertex V;
int Nv, i;
scanf("%d", &Nv);
graph = CreateGraph(Nv);
scanf("%d", &(graph->Ne));
scanf("%d %d", &source, &destination);
for (V = 0; V < Nv; ++V)
scanf("%d", &(graph->G[V].RescueNum));
if (graph->Ne != 0) {
E = (Edge)malloc(sizeof(struct ENode));
for (i = 0; i < graph->Ne; ++i) {
scanf("%d %d %d", &(E->V1), &(E->V2), &(E->RoadLength));
InsertEdge(graph, E);
}
free(E);
}
return graph;
}
void init(LGraph Graph)
{
Vertex V;
for (V = 0; V < Graph->Nv; ++V) {
collected[V] = false;
roadLength[V] = INFINITY;
shortestNum[V] = 0;
}
}
int FindMinDist(LGraph Graph)
{
Vertex MinV, V;
int MinDist = INFINITY;
for (V = 0; V < Graph->Nv; ++V) {
if (!collected[V] && roadLength[V] < MinDist) {
MinDist = roadLength[V];
MinV = V;
}
}
if (MinDist < INFINITY) return MinV;
else return ERROR;
}
void solve(LGraph Graph)
{
PtrToAdjVNode edge;
Vertex V;
init(Graph);
// 先将源点放入已取元素的集合中,然后更改其邻接点相关值
collected[source] = true; roadLength[source] = 0;
shortestNum[source] = 1; rescueNum[source] = Graph->G[source].RescueNum;
for (edge = Graph->G[source].FirstEdge; edge; edge = edge->Next) {
roadLength[edge->AdjV] = edge->RoadLength;
shortestNum[edge->AdjV] = shortestNum[source];
rescueNum[edge->AdjV] = rescueNum[source] + Graph->G[edge->AdjV].RescueNum;
}
// 然后依次从未取元素中找距离最小的元素放入集合中
while (true) {
V = FindMinDist(Graph);
if (V == ERROR || V == destination)
break;
collected[V] = true;
for (edge = Graph->G[V].FirstEdge; edge; edge = edge->Next) {
if (!collected[edge->AdjV]) {
if (roadLength[V] + edge->RoadLength < roadLength[edge->AdjV]) {
roadLength[edge->AdjV] = roadLength[V] + edge->RoadLength;
shortestNum[edge->AdjV] = shortestNum[V];
rescueNum[edge->AdjV] = rescueNum[V] + Graph->G[edge->AdjV].RescueNum;
}
else if (roadLength[V] + edge->RoadLength == roadLength[edge->AdjV]) {
shortestNum[edge->AdjV] += shortestNum[V];
if (rescueNum[edge->AdjV] < rescueNum[V] + Graph->G[edge->AdjV].RescueNum)
rescueNum[edge->AdjV] = rescueNum[V] + Graph->G[edge->AdjV].RescueNum;
}
}
}
}
printf("%d %d\n", shortestNum[destination], rescueNum[destination]);
}