23:34:47 2019-09-06
Next summer school not yet begun, continue to learn
PTA question 21 Prim minimum spanning tree generation
1 #define _CRT_SECURE_NO_WARNINGS 2 #include<stdio.h> 3 #include<malloc.h> 4 #define INIFITY 65635 5 6 typedef int Vertex; 7 typedef struct ENode* Edge; 8 struct ENode 9 { 10 Vertex V1, V2; 11 int Weight; 12 }; 13 14 typedef struct Graph* MGraph; 15 struct Graph 16 { 17 int Nv; 18 int Ne; 19 int G[1001][1001]; 20 }; 21 22 MGraph CreateGraph(int VertexNum) 23 { 24 MGraph Graph = (MGraph)malloc(sizeof(struct Graph)); 25 Graph->Nv = VertexNum; 26 Graph->Ne = 0; 27 for (int i = 0; i < Graph->Nv; i++) 28 for (int j = 0; j < Graph->Nv; j++) 29 Graph->G[i][j] = INIFITY; 30 return Graph; 31 } 32 33 void Insert(MGraph Graph, Edge E) 34 { 35 Graph->G[E->V1][E->V2] = E->Weight; 36 Graph->G[E->V2][E->V1] = E->Weight; 37 } 38 39 MGraph BuildGraph() 40 { 41 int Nv; 42 Edge E; 43 scanf("%d", &Nv); 44 MGraph Graph = CreateGraph(Nv); 45 scanf("%d\n", &(Graph->Ne)); 46 for (int i = 0; i < Graph->Ne; i++) 47 { 48 E = (Edge)malloc(sizeof(struct ENode)); 49 scanf("%d %d %d\n", &(E->V1), &(E->V2), &(E->Weight)); 50 Insert(Graph, E); 51 is } 52 is return Graph; 53 is } 54 is 55 int IsEdge (Graph MGraph, Vertex V, W is Vertex) 56 is { 57 is return (Graph-> G [V] [W is] <INIFITY)? . 1 : 0 ; 58 } 59 // Prim using the minimum spanning tree algorithm to solve the problem 60 61 is int Dist [ 1001 ]; // represents the distance from a point to the tree 62 is int the Parent [ 1001 ]; 63 is int TotalWeight; // represents the total cost of 64 intVCount; // number of dots included 65 int FindMinDist (MGraph Graph) 66 { 67 int MinDist = INIFITY; 68 int MINV; 69 for ( int I = 0 ; I <Graph-> Nv; I ++ ) 70 { 71 is IF (Dist [I] = -! . 1 && Dist [I] < MinDist) 72 { 73 is MinDist = Dist [I]; 74 MINV = I; 75 } 76 } 77 if (MinDist < INIFITY) 78 return MinV; 79 else 80 return 0; 81 } 82 int Prim(MGraph Graph, Vertex S) 83 { 84 for (int i = 0; i < Graph->Nv; i++) 85 { 86 Dist[i] = Graph->G[S][i]; 87 if (Dist[i] < INIFITY) 88 Parent[i] = S; 89 else 90 Parent[i] = -1; 91 } 92 Dist[S] = -1; 93 Parent[S] = -1; 94 VCount++; 95 while (1) 96 { 97 int V = FindMinDist(Graph); 98 if (!V) 99 break; 100 TotalWeight += Dist[V]; 101 Dist[V] = -1; 102 VCount++; 103 for (int i = 0; i < Graph->Nv; i++) 104 { 105 if (Dist[i]!=-1&& IsEdge(Graph, V, i)) 106 { 107 if (Graph->G[V][i] < Dist[i]) 108 { 109 Dist[i] = Graph->G[V][i]; 110 Parent[i] = V; 111 } 112 } 113 } 114 } 115 if (VCount <Graph->Nv) 116 return -1; 117 else 118 return TotalWeight; 119 } 120 int main() 121 { 122 MGraph Graph = BuildGraph(); 123 printf("%d", Prim(Graph, 0)); 124 return 0; 125 }
Topological sorting
Order Topology: If the figures from V to W there is a directed path, ahead of certain V W satisfy this condition vertex sequence sequence called a topological
Get a topological order process is topological sorting
AOV (Acitivity On Vertex) network topology if there is a reasonable sequence, it must be a directed acyclic graph (Directed Acyclic Graph, DAG)
Critical path problem (topological sorting applications)
AOE (Acitivity On Edge) Network (os: Ethics questions gave me a good long time on the PTA will not .. I do not really see a tutorial on what does not)
General work arrangements for projects