码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构学习第二十一天

时间:2019-09-07 01:32:05      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:排序   路径   printf   fine   main   时间   拓扑排序   std   acitivity   

23:34:47 2019-09-06

学校未开课 继续接着暑假学习

 

PTA第21题 Prim最小树生成

技术图片
  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     }
 52     return Graph;
 53 }
 54 
 55 int IsEdge(MGraph Graph, Vertex V, Vertex W)
 56 {
 57     return (Graph->G[V][W] < INIFITY) ? 1 : 0;
 58 }
 59 //利用Prim算法 解决最小生成树的问题
 60 
 61 int Dist[1001];  //表示某点到树的距离
 62 int Parent[1001];
 63 int TotalWeight;    //表示总的成本
 64 int VCount;  //表示收录的点数
 65 int FindMinDist(MGraph Graph)
 66 {
 67     int MinDist = INIFITY;
 68     int MinV;
 69     for (int i = 0; i < Graph->Nv; i++)
 70     {
 71         if (Dist[i]!=-1&& Dist[i] < MinDist)
 72         {
 73             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 }
View Code

 

拓扑排序

拓扑序:如果图中从V到W有一条有向路径,则V一定排在W之前.满足此条件的顶点序列称为一个拓扑序

获得一个拓扑序的过程就是拓扑排序

AOV(Acitivity On Vertex)网络 如果有合理的拓扑序,则必定是有向无环图(Directed Acyclic Graph,DAG)

 

关键路径问题(拓扑排序应用)

AOE(Acitivity On Edge)网络  (os:PTA上有道题看了我好长时间都不会 。。果然我不看教程就啥也不会)

  一般用于安排项目的工序

数据结构学习第二十一天

标签:排序   路径   printf   fine   main   时间   拓扑排序   std   acitivity   

原文地址:https://www.cnblogs.com/57one/p/11478894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!