Emergency(只有10分),儲存一下程式碼。求大神改正。
#include <stdio.h>
#include <stdlib.h>
typedef struct LinkedNode *List;
typedef int DistType;
typedef int Vertex;
typedef struct TableEntry *Table;
enum bool{false, true};
struct LinkedNode
{
Vertex Vertex;
DistType Dist;
};
struct TableEntry
{
List Header;
int Known;
DistType Dist;
int RescueTeam;
};
#define NotAVertex (-1)
#define Infinity 2147483647
Vertex InitTable(Table T, int NumVertex);
void Dijkstra(Table
int NumRescueTeam(Table T, int NumVertex, Vertex SaveLocation);
Vertex FindUnknownMinDistance(Table T, int NumVertex);
int main()
{
int NumVertex;
scanf("%d",&NumVertex);
Table T = malloc(
Vertex SaveLocation = InitTable(T, NumVertex);
Dijkstra(T, NumVertex, SaveLocation);
int RescueTeamCount = NumRescueTeam(T, NumVertex, SaveLocation);
printf("%d %d\n",T[SaveLocation].Dist, RescueTeamCount);
}
Vertex InitTable(Table T, int NumVertex)
{
int NumEdge,CurrentLocation,SaveLocation;
int V1,V2,Dist;
scanf("%d %d %d",&NumEdge,&CurrentLocation,&SaveLocation);
for (int i=0; i<NumVertex; i++) {
scanf("%d",&T[i].RescueTeam);
T[i].Known = false;
T[i].Dist = Infinity;
T[i].Header = malloc(sizeof(struct LinkedNode));
T[i].Header->Next = NULL;
}
T[CurrentLocation].Dist = 0;
for (int i=0; i<NumEdge; i++) {
scanf("%d %d %d",&V1,&V2,&Dist);
List P = malloc(sizeof(struct LinkedNode));
P->Vertex = V2;
P->Dist = Dist;
P->Next = T[V1].Header->Next;
T[V1].Header->Next = P;
}
return SaveLocation;
}
void Dijkstra(Table T, int NumVertex, Vertex SaveLocation)
{
Vertex V, W;
for (int i=0; i<NumVertex; i++) {
V = FindUnknownMinDistance(T, NumVertex);
if (V == NotAVertex) {
break;
}
T[V].Known = true;
List head = T[V].Header->Next;
while (head) {
W = head->Vertex;
if (!T[W].Known) {
if (T[V].Dist + head->Dist < T[W].Dist) {
T[W].Dist = T[V].Dist + head->Dist;
}
}
head = head->Next;
}
}
}
Vertex FindUnknownMinDistance(Table T, int NumVertex)
{
Vertex V = Infinity;
int MinDistance = Infinity;
for (int i=0; i<NumVertex; i++) {
if (T[i].Dist < MinDistance && T[i].Known == false) {
MinDistance = T[i].Dist;
V = i;
}
}
return V;
}
int NumRescueTeam(Table T, int NumVertex, Vertex SaveLocation)
{
int RescueTeamCount = T[SaveLocation].RescueTeam;
for (int i=0; i<NumVertex; i++) {
List Head = T[i].Header->Next;
while (Head) {
if (Head->Vertex == SaveLocation) {
RescueTeamCount += T[i].RescueTeam;
break;
}
Head = Head->Next;
}
}
return RescueTeamCount;
}