现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本。

输入格式:

输入数据包括城镇数目正整数N(≤1000)和候选道路数目M(≤3N);随后的M行对应M条道路,每行给出3个正整数,分别是该条道路直接连通的两个城镇的编号以及该道路改建的预算成本。为简单起见,城镇从1到N编号。

输出格式:

输出村村通需要的最低成本。如果输入数据不足以保证畅通,则输出−1,表示需要建设更多公路。

输入样例:

6 15
1 2 5
1 3 3
1 4 7
1 5 4
1 6 2
2 3 4
2 4 6
2 5 2
2 6 6
3 4 6
3 5 1
3 6 1
4 5 10
4 6 8
5 6 3

输出样例:

12

我的答案
 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #define ERROR -1
#define false 0
#define true 1
#define MaxVertexNum 10000
#define INFINITY 65535
typedef int Vertex;
typedef int WeightType;
typedef int bool; //边
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; //邻接点
typedef struct AdjVNode *PtrToAdjVNode;
struct AdjVNode {
Vertex AdjV;
WeightType Weight;
PtrToAdjVNode Next;
}; typedef struct VNode {
PtrToAdjVNode FirstEdge;
// DataType Data;
}AdjList[MaxVertexNum]; typedef struct LNode *PtrToLNode;
struct LNode {
int Nv;
int Ne;
AdjList G;
};
typedef PtrToLNode LGraph; //邻接矩阵
MGraph MGraphCreate(int VertexNum)
{
Vertex V, W;
MGraph Graph; Graph = (MGraph)malloc(sizeof(struct GNode));
Graph->Nv = VertexNum;
Graph->Ne = ; for(V=;V<Graph->Nv;V++) {
for(W=;W<Graph->Nv;W++) {
Graph->G[V][W] = INFINITY;
}
} return Graph;
} void MGraphInsertEdge(MGraph Graph, Edge E)
{
Graph->G[E->V1][E->V2] = E->Weight;
Graph->G[E->V2][E->V1] = E->Weight;
} MGraph MGraphBuild()
{
MGraph Graph;
Edge E;
int i, Nv; scanf("%d ", &Nv);
Graph = MGraphCreate(Nv); scanf("%d\n", &(Graph->Ne));
if(Graph->Ne != ) {
E = (Edge)malloc(sizeof(struct ENode));
for(i=;i<Graph->Ne;i++) {
scanf("%d %d %d\n", &E->V1, &E->V2, &E->Weight);
E->V1--;
E->V2--;
MGraphInsertEdge(Graph, E);
}
} return Graph;
} void MGraphPrint(MGraph Graph)
{
Vertex V, W;
printf("Graph:\n");
for(V=;V<Graph->Nv;V++) {
for(W=;W<Graph->Nv;W++)
printf("[%5d]\t" , Graph->G[V][W]);
printf("\n");
}
printf("-----------------------\n");
} //邻接表
LGraph LGraphCreate(int VertexNum)
{
Vertex V;
LGraph Graph; Graph = (LGraph)malloc(sizeof(struct LNode));
Graph->Nv = VertexNum;
Graph->Ne = ;
for(V=;V<Graph->Nv;V++)
Graph->G[V].FirstEdge = NULL; return Graph;
} void LGraphInsertEdge(LGraph Graph, Edge E)
{
PtrToAdjVNode NewNode; NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V2;
NewNode->Weight = E->Weight; NewNode->Next = Graph->G[E->V1].FirstEdge;
Graph->G[E->V1].FirstEdge = NewNode; NewNode = (PtrToAdjVNode)malloc(sizeof(struct AdjVNode));
NewNode->AdjV = E->V1;
NewNode->Weight = E->Weight; NewNode->Next = Graph->G[E->V2].FirstEdge;
Graph->G[E->V2].FirstEdge = NewNode;
} LGraph LGraphBuilGraph()
{
LGraph Graph;
Edge E;
// Vertex V;
int Nv, i; scanf("%d", &Nv);
Graph = LGraphCreate(Nv); scanf("%d", &(Graph->Ne));
if(Graph->Ne != ) {
E = (Edge)malloc(sizeof(struct ENode));
for(i=;i<Graph->Ne;i++) {
scanf("%d %d %d", &E->V1, &E->V2, &E->Weight);
LGraphInsertEdge(Graph, E);
}
} return Graph;
} Vertex FindMinDist(MGraph Graph, WeightType dist[])
{
Vertex MinV, V;
WeightType MinDist = INFINITY; for(V=;V<Graph->Nv;V++) {
if(dist[V] != && dist[V] < MinDist) {
MinDist = dist[V];
MinV = V;
}
}
if(MinDist < INFINITY)
return MinV;
else return ERROR;
} int Prim(MGraph Graph, LGraph MST)
{ /* 将最小生成树保存为邻接表存储的图MST,返回最小权重和 */
WeightType dist[MaxVertexNum], TotalWeight;
Vertex parent[MaxVertexNum], V, W;
int VCount;
Edge E; /* 初始化,默认初始点下标是0 */
for(V=;V<Graph->Nv;V++) {
/* 这里假设若V到W没有直接的边,则Graph->G[V][W]定义为INIFINITY */
dist[V] = Graph->G[][V];
parent[V] = ; /* 暂且定义所有顶点的父结点都是初始点0 */
}
TotalWeight = ; /* 初始化权重和 */
VCount = ; /* 初始化收录的顶点数 */
/* 创建包含所有顶点但没有边的图。注意用邻接表版本 */
MST = LGraphCreate(Graph->Nv);
E = (Edge)malloc(sizeof(struct ENode)); /* 将初始点0收录进MST */
dist[] = ;
VCount++;
parent[] = -; /* 当前树根是0 */ while() {
V = FindMinDist(Graph, dist);
/* V = 未被收录顶点中dist最小者 */
if(V == ERROR) /* 若这样的V不存在 */
break; /* 算法结束 */ /* 将V及相应的边<parent[V], V>收录进MST */
E->V1 = parent[V];
E->V2 = V;
E->Weight = dist[V];
LGraphInsertEdge(MST, E);
TotalWeight += dist[V];
dist[V] = ;
VCount++; for(W=;W<Graph->Nv;W++) { /* 对图中的每个顶点W */
if(dist[W]!= && Graph->G[V][W]<INFINITY) {
/* 若W是V的邻接点并且未被收录 */
if(Graph->G[V][W] < dist[W]) {
/* 若收录V使得dist[W]变小 */
dist[W] = Graph->G[V][W]; /* 更新dist[W] */
parent[W] = V; /* 更新树 */
}
}
}
}
if(VCount < Graph->Nv) /* MST中收的顶点不到|V|个 */
TotalWeight = ERROR;
return TotalWeight; /* 算法执行完毕,返回最小权重和或错误标记 */
} int main()
{
MGraph Graph;
LGraph Lraph;
WeightType minCost;
Graph = MGraphBuild();
// MGraphPrint(Graph);
minCost = Prim(Graph, Lraph);
printf("%d\n", minCost);
return ;
}

08-图7 公路村村通(30 分)Prim的更多相关文章

  1. pta08-图7 公路村村通 (30分)

    08-图7 公路村村通   (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N ...

  2. PTA 7-1 公路村村通 (30分)

    PTA 7-1 公路村村通 (30分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N ...

  3. PTA 08-图7 公路村村通 (30分)

    现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数NN(\le 1000≤1000)和候选道 ...

  4. 7-6 公路村村通(30 分) 【prime】

    7-6 公路村村通(30 分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤10 ...

  5. 7-10 公路村村通(30 分)(最小生成树Prim算法)

    7-10 公路村村通(30 分) 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本,求使每个村落都有公路连通所需要的最低成本. 输入格式: 输入数据包括城镇数目正整数N(≤1 ...

  6. pat06-图6. 公路村村通(30)

    06-图6. 公路村村通(30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的 ...

  7. ACM程序设计选修课——Problem E:(ds:图)公路村村通(Prim)

    问题 E: (ds:图)公路村村通 时间限制: 1 Sec  内存限制: 128 MB 提交: 9  解决: 5 题目描述 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的若干条道路的成本, ...

  8. PAT 7-14 公路村村通

    https://pintia.cn/problem-sets/1111189748004499456/problems/1111189831248850957 现有村落间道路的统计数据表中,列出了有可 ...

  9. ACM程序设计选修课——Problem E:(ds:图)公路村村通(优先队列或sort+克鲁斯卡尔+并查集优化)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

随机推荐

  1. 判断逻辑 先判断协议字段返回,再判断业务返回,最后判断交易状态 API密钥

    [微信支付]微信小程序支付开发者文档 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_1 协议规则 商户接入微信支付, ...

  2. 测开之路七十七:shell之if、case、for、while

    选择语句(if语句) 大于:-gt判断目录是否存在:-d if [ 判断条件 ]; then statement1 Statement2elif [ 判断条件 ]; then statement1 S ...

  3. rap安装mysql

    1.yum仓库下载MySQL: yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm ...

  4. Caffe深入分析(源码)

    Caffe的整体流程图: 程序入口:main() int main(int argc, char** argv) { ..... ]))(); .... } g_brew_map实现过程,首先通过 t ...

  5. Harbor - 私有企业级 Docker 镜像仓库

    GitHub 地址 容器镜像服务 Docker镜像的基本使用 Docker:企业级私有镜像仓库Harbor使用 Harbor 是基于 Docker Registry 的企业级镜像仓库,安装后的使用方法 ...

  6. Lambda拉姆达表达式

    拉姆达表达式常用于委托,也就是说拉姆达表达式是匿名函数,简单点就是函数. a => a.Equals("string"); //原形为: (a) => { return ...

  7. Reading query string values in JavaScript

    时间 2016-01-23 13:01:14  CrocoDillon’s Blog 原文  http://crocodillon.com/blog/reading-query-string-valu ...

  8. CentOS7 监控网络流量

    首先,以下介绍的流量监控工具安装之前均需要安装epel源, 安装epel源: [root@bogon ~]# yum -y install epel-release 安装 iftop 工具,查看各个连 ...

  9. Linux scp常用命令

    Linux scp命令用于Linux之间复制文件和目录. scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. 1.从本地复制到远程 命令格式: ...

  10. axios 如何获取下载文件的进度条

    exportFun(){         let _that = this         const instance = this.axios.create({           onDownl ...