-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy path07-图4 哈利·波特的考试.c
140 lines (114 loc) · 2.79 KB
/
07-图4 哈利·波特的考试.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
#include <stdio.h>
#include <stdlib.h>
#define MaxVertexNum 100
#define INFINITY 65535
typedef int Vertex;
typedef int WeightType;
typedef struct ENode *PtrToENode;
struct ENode {
Vertex V1, V2;
WeightType Weight;
};
typedef PtrToENode Edge;
typedef struct GNode *PtrToGNode;
struct GNode {
int Nv;
int Ne;
WeightType G[MaxVertexNum][MaxVertexNum];
};
typedef PtrToGNode MGraph;
MGraph CreateGraph(int);
void InsertEdge(MGraph, Edge);
MGraph BuildGraph();
void DestoryGraph(MGraph);
void Floyd(MGraph, WeightType D[][MaxVertexNum]);
WeightType FindMaxDist(WeightType D[][MaxVertexNum], Vertex, int);
void FindAnimal(MGraph);
int main()
{
MGraph G = BuildGraph();
FindAnimal(G);
return 0;
}
MGraph CreateGraph(int VertexNum)
{
Vertex V, W;
MGraph Graph;
Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = 0;
for (V = 0; V < Graph->Nv; ++V)
for (W = 0; W < Graph->Nv; ++W)
Graph->G[V][W] = INFINITY;
return Graph;
}
void InsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->Weight;
Graph->G[E->V2][E->V1] = E->Weight;
}
MGraph BuildGraph()
{
MGraph Graph;
Edge E;
int Nv, i;
scanf("%d", &Nv);
Graph = CreateGraph(Nv);
scanf("%d", &(Graph->Ne));
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->Weight);
--E->V1; --E->V2;
InsertEdge(Graph, E);
}
free(E);
}
return Graph;
}
void DestoryGraph(MGraph Graph)
{
if (Graph) free(Graph);
}
void Floyd(MGraph Graph, WeightType D[][MaxVertexNum])
{
Vertex i, j, k;
for (i = 0; i < Graph->Nv; ++i)
for (j = 0; j < Graph->Nv; ++j)
D[i][j] = Graph->G[i][j];
for (k = 0; k < Graph->Nv; ++k)
for (i = 0; i < Graph->Nv; ++i)
for (j = 0; j < Graph->Nv; ++j) {
if (D[i][k] + D[k][j] < D[i][j])
D[i][j] = D[i][k] + D[k][j];
}
}
WeightType FindMaxDist(WeightType D[][MaxVertexNum],
Vertex i, int N)
{
WeightType MaxDist;
Vertex j;
MaxDist = 0;
for (j = 0; j < N; ++j)
if (i != j && D[i][j] > MaxDist)
MaxDist = D[i][j];
return MaxDist;
}
void FindAnimal(MGraph Graph)
{
WeightType D[MaxVertexNum][MaxVertexNum], MaxDist, MinDist;
Vertex Animal, i;
Floyd(Graph, D);
MinDist = INFINITY;
for (i = 0; i < Graph->Nv; ++i) {
MaxDist = FindMaxDist(D, i, Graph->Nv);
if (MaxDist == INFINITY) {
printf("0\n");
return;
}
if (MinDist > MaxDist) {
MinDist = MaxDist; Animal = i + 1;
}
}
printf("%d %d\n", Animal, MinDist);
}