7-25-Harry Potter's Exam-编程题
解题代码
#include<stdio.h>
#include<stdlib.h>
#define MAXN 100
#define Infinite 65534
typedef int Weight;
typedef struct GNode *PtrToGNode;
struct GNode {
int Nv;
int Ne;
Weight W[MAXN][MAXN];
};
typedef PtrToGNode MGraph;
typedef struct ENode *PtrToENode;
struct ENode {
int V1, V2;
Weight W;
};
typedef PtrToENode Edge;
void Initialization_W(MGraph G);
Edge InputEdge(void);
MGraph BuildGraph(void);
void InsertEdge(MGraph G, Edge E);
void FindAnimal(MGraph G);
void Floyd(MGraph G, Weight D[][MAXN]);
int GetLDL(Weight D[][MAXN], int i, int N);
int main()
{
MGraph G = BuildGraph();
FindAnimal(G);
return 0;
}
MGraph BuildGraph(void) {
MGraph G = (MGraph)malloc(sizeof(struct GNode));
scanf("%d %d", &G->Nv, &G->Ne);
Initialization_W(G);
Edge E;
for (int i = 0; i < G->Ne; i++) {
E = InputEdge();
InsertEdge(G, E);
}
return G;
}
void Initialization_W(MGraph G) {
int i, j;
for (i = 0; i < G->Nv; i++)
for (j = 0; j < G->Nv; j++)
G->W[i][j] = Infinite;
}
Edge InputEdge(void) {
Edge E = (Edge)malloc(sizeof(struct ENode));
scanf("%d %d %d", &E->V1, &E->V2, &E->W);
return E;
}
void InsertEdge(MGraph G, Edge E) {
G->W[E->V1 - 1][E->V2 - 1] = G->W[E->V2 - 1][E->V1 - 1] = E->W;
}
void FindAnimal(MGraph G) {
Weight D[MAXN][MAXN];
Floyd(G, D);
int SDL, LDL, index;
SDL = Infinite; LDL = 0;
for (int i = 0; i < G->Nv; i++) {
LDL = GetLDL(D, i, G->Nv);
if (LDL < SDL) {
SDL = LDL;
index = i;
}
}
if (SDL == Infinite) printf("0");
else printf("%d %d", index+1, SDL);
}
void Floyd(MGraph G, Weight D[][MAXN]) {
int i, j, k;
for (i = 0; i < G->Nv; i++)
for (j = 0; j < G->Nv; j++)
D[i][j] = G->W[i][j];
for (k = 0; k < G->Nv; k++)
for (i = 0; i < G->Nv; i++)
for (j = 0; j < G->Nv; j++)
if (D[i][k] + D[k][j] < D[i][j])
D[i][j] = D[i][k] + D[k][j];
}
int GetLDL(Weight D[][MAXN], int i, int N) {
int LDL = 0;
for (int j = 0; j < N; j++)
if (j != i && D[i][j] > LDL) LDL = D[i][j];
return LDL;
}
测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9c2caff8d9cd4abca9ffd0eef2907ded.png)
问题整理
1.基础题。