858. Prim算法求最小生成树

858. Prim算法求最小生成树 - AcWing题库

//prim算法 每次放入不在连通块中 最近的一个点 连通块加上距离 然后更新迭代其余所有点
#include<iostream>
#include<cstring>
using namespace std;

const int N=510,INF=0x3f3f3f3f;
int g[N][N];//g[a][b] a->b的距离
int dist[N];//dist[i]:i到连通块中的最小距离
bool st[N];//是否在连通块中
int n,m;

int prim()
{
    memset(dist,0x3f,sizeof dist);
    int res=0;//连通块长度
    
    for(int i=0;i<n;i++)//n个点 循环n次 Dijkstra可循环n-1次(起点直接放入)
    {
        int t=-1;
        for(int j=1;j<=n;j++)
            if(!st[j] && (t==-1 || dist[t]>dist[j]))
                t=j;//最近的未被确定的点
                
        if(i && dist[t]==INF) return INF;//不是起点 但是到连通块中点的距离为INF
        if(i) res+=dist[t];//不是起点 距离加上
        //这步要放在更新其余点之前,因为如果有自环,g[t][t]可能会更新dist[t]
        for(int j=1;j<=n;j++) dist[j]=min(dist[j],g[t][j]);
        
        st[t]=true;//放入连通块
    }
    
    return res;
}

int main()
{
    scanf("%d%d",&n,&m);
    memset(g,0x3f,sizeof g);
    
    while(m--)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        g[a][b]=g[b][a]=min(g[a][b],c);
    }
    
    int t=prim();
    if(t==INF) printf("impossible\n");
    else printf("%d\n",t);
    
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的基于DOS菜单的应用程序,实现无向网的基本操作及应用: ```c #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAX_VERTEX_NUM 20 // 最大顶点数 #define INFINITY 65535 // 无穷大 typedef struct { int adjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int numVertices; // 顶点数 } Graph; typedef struct { int adjList[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接表 int numVertices; // 顶点数 } GraphList; // 创建无向网的邻接矩阵 void createGraph(Graph *graph) { int i, j, weight; printf("请输入顶点数: "); scanf("%d", &graph->numVertices); for (i = 0; i < graph->numVertices; ++i) { for (j = 0; j < graph->numVertices; ++j) { printf("请输入顶点%d到顶点%d的权值(没有连接关系则输入无穷大): ", i + 1, j + 1); scanf("%d", &weight); graph->adjMatrix[i][j] = weight; } } } // 创建无向网的邻接表 void createGraphList(GraphList *graphList) { int i, j, weight; printf("请输入顶点数: "); scanf("%d", &graphList->numVertices); for (i = 0; i < graphList->numVertices; ++i) { for (j = 0; j < graphList->numVertices; ++j) { printf("请输入顶点%d到顶点%d的权值(没有连接关系则输入无穷大): ", i + 1, j + 1); scanf("%d", &weight); graphList->adjList[i][j] = weight; } } } // Prim算法最小生成树 void prim(Graph *graph) { int i, j, k, minWeight; int lowCost[MAX_VERTEX_NUM]; int closest[MAX_VERTEX_NUM]; for (i = 0; i < graph->numVertices; ++i) { lowCost[i] = graph->adjMatrix[0][i]; closest[i] = 0; } for (i = 1; i < graph->numVertices; ++i) { minWeight = INFINITY; for (j = 1; j < graph->numVertices; ++j) { if (lowCost[j] != 0 && lowCost[j] < minWeight) { minWeight = lowCost[j]; k = j; } } printf("(%d, %d)\n", closest[k] + 1, k + 1); lowCost[k] = 0; for (j = 1; j < graph->numVertices; ++j) { if (lowCost[j] != 0 && graph->adjMatrix[k][j] < lowCost[j]) { lowCost[j] = graph->adjMatrix[k][j]; closest[j] = k; } } } } // Kruskal算法最小生成树 void kruskal(Graph *graph) { int i, j, k, m, n; int parent[MAX_VERTEX_NUM]; int minWeight, sumWeight = 0; for (i = 0; i < graph->numVertices; ++i) { parent[i] = 0; } for (i = 1; i < graph->numVertices; ++i) { minWeight = INFINITY; for (j = 0; j < graph->numVertices; ++j) { for (k = 0; k < graph->numVertices; ++k) { if (graph->adjMatrix[j][k] < minWeight && parent[j] != k) { minWeight = graph->adjMatrix[j][k]; m = j; n = k; } } } while (parent[m] != 0) { m = parent[m]; } while (parent[n] != 0) { n = parent[n]; } if (m != n) { printf("(%d, %d)\n", m + 1, n + 1); sumWeight += minWeight; parent[n] = m; } } } int main() { int choice; Graph graph; GraphList graphList; while (1) { system("cls"); printf("无向网的基本操作及应用\n"); printf("1. 创建无向网的邻接矩阵\n"); printf("2. 创建无向网的邻接表\n"); printf("3. Prim算法最小生成树(应用)\n"); printf("4. Kruskal算法最小生成树(应用)\n"); printf("0. 退出\n"); printf("请输入你的选择: "); scanf("%d", &choice); switch (choice) { case 1: createGraph(&graph); break; case 2: createGraphList(&graphList); break; case 3: prim(&graph); printf("按任意键继续...\n"); getch(); break; case 4: kruskal(&graph); printf("按任意键继续...\n"); getch(); break; case 0: exit(0); break; default: printf("输入错误,请重新输入...\n"); printf("按任意键继续...\n"); getch(); break; } } return 0; } ``` 运行程序后,选择相应的菜单选项即可执行对应的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值