forked from Yuri0314/PTA-mooc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-图6 旅游规划.c
167 lines (146 loc) · 3.71 KB
/
07-图6 旅游规划.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
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 500
#define INFINITY 65535
/* 无向图的邻接表定义——开始 */
typedef int Vertex;
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
Vertex AdjV;
int length, cost;
PtrToAdjVNode Next;
};
typedef struct Vnode {
PtrToAdjVNode FirstEdge;
} AdjList[MaxVertexNum];
typedef struct GNode *PtrToGNode;
struct GNode {
int Nv;
int Ne;
AdjList G;
};
typedef PtrToGNode LGraph;
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1, V2;
int length, cost;
};
typedef PtrToENode Edge;
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->length = E->length; NewNode->cost = E->cost;
NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode;
}
/* 无向图的邻接表定义——结束 */
#define ERROR -1
int source, destination;
int collected[MaxVertexNum], dist[MaxVertexNum], cost[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 BuildGraph()
{
int Nv, i; Edge E;
Vertex V; LGraph Graph;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &(Graph->Ne));
scanf("%d %d", &source, &destination);
if (Graph->Ne != 0) {
E = (Edge)malloc(sizeof(struct ENode));
for (i = 0; i < Graph->Ne; ++i) {
scanf("%d %d %d %d", &E->V1, &E->V2, &E->length, &E->cost);
InsertEdge(Graph, E);
V = E->V1; E->V1 = E->V2; E->V2 = V;
InsertEdge(Graph, E);
}
free(E);
}
return Graph;
}
void init(LGraph Graph)
{
Vertex V;
PtrToAdjVNode edge;
for (V = 0; V < Graph->Nv; ++V) {
collected[V] = 0;
dist[V] = INFINITY;
cost[V] = INFINITY;
}
for (edge = Graph->G[source].FirstEdge; edge; edge = edge->Next) {
dist[edge->AdjV] = edge->length;
cost[edge->AdjV] = edge->cost;
}
}
int FindMinDist(LGraph Graph)
{
Vertex MinV, V;
int MinDist = INFINITY;
for (V = 0; V < Graph->Nv; ++V) {
if (collected[V] == 0 && dist[V] < MinDist) {
MinDist = dist[V];
MinV = V;
}
}
if (MinDist < INFINITY)
return MinV;
else return ERROR;
}
void solve(LGraph Graph)
{
PtrToAdjVNode edge;
Vertex V;
init(Graph);
collected[source] = 1; dist[source] = 0; cost[source] = 0;
while (1) {
V = FindMinDist(Graph);
if (V == ERROR)
break;
collected[V] = 1;
for (edge = Graph->G[V].FirstEdge; edge; edge = edge->Next) {
if (collected[edge->AdjV] == 0 &&
(dist[V] + edge->length < dist[edge->AdjV] ||
(dist[V] + edge->length == dist[edge->AdjV] && cost[V] + edge->cost < cost[edge->AdjV]))) {
dist[edge->AdjV] = dist[V] + edge->length;
cost[edge->AdjV] = cost[V] + edge->cost;
}
}
}
printf("%d %d\n", dist[destination], cost[destination]);
}