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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Maxn 1005
#define INF 100005
typedef struct GNode* MGraph;
struct GNode {
    int Nv, Ne;
    int G[Maxn][Maxn];
};

typedef struct AdjNode* AdjList;
struct AdjNode {
    int V;
    AdjList Next;
};

typedef struct HNode {
    AdjList FirstEdge;
}List[Maxn];

typedef struct LGNode* LGraph;
struct LGNode {
    int Nv;
    List Head;
};

typedef struct ENode* Edge;
struct ENode {
    int v1, v2;
};

void PrintGraph(MGraph MG) {
    int v, w;
    for(v = 1; v <= MG->Nv; v++) {
        for(w = 1; w <= MG->Nv; w++)
            printf("%3d", MG->G[v][w]);
        printf("\n");
    }
}

LGraph CreateLGraph(int n) {
    int i;
    LGraph MST = (LGraph)malloc(sizeof(struct LGNode));
    MST->Nv = n;
    for(i = 1; i <= n; i++)
        MST->Head[i].FirstEdge = NULL;
    return MST;
}

MGraph CreateMGraph(int n, int m) {
    int v, w, v1, v2, W;
    MGraph MG = (MGraph)malloc(sizeof(struct GNode));
    MG->Ne = m;
    MG->Nv = n;
    for(v = 1; v <= n; v++)
        for(w = 1; w <= n; w++)
            MG->G[v][w] = INF;
    for(v = 1; v <= m; v++) {
        scanf("%d%d%d", &v1, &v2, &W);
        MG->G[v1][v2] = MG->G[v2][v1] = W;
    }
    return MG;
}

void InsertEdge(LGraph MST, Edge E) {
    AdjList NewNode = (AdjList)malloc(sizeof(struct AdjNode));
    NewNode->V = E->v1;
    NewNode->Next = MST->Head[E->v1].FirstEdge;
    MST->Head[E->v1].FirstEdge = NewNode;
    NewNode = (AdjList)malloc(sizeof(struct AdjNode));
    NewNode->V = E->v2;
    NewNode->Next = MST->Head[E->v2].FirstEdge;
    MST->Head[E->v2].FirstEdge = NewNode;
}

int FindMinDist(MGraph MG, int dist[]) {
    int i, Mindist, Minv;
    Mindist = INF;
    for(i = 1; i <= MG->Nv; i++) {
        if(dist[i] != 0 && dist[i] < Mindist) {
            Mindist = dist[i];
            Minv = i;
        }
    }
    if(Mindist != INF) return Minv;
    else return -1;
}

void Prim(MGraph MG, LGraph MST) {
    int dist[Maxn], parent[Maxn], Tweight, v, w;
    int vcnt;
    Edge E = (Edge)malloc(sizeof(struct ENode));
    //初始化各项数值,dist的初始值,若与源点邻接则为边权,否则为无穷,先假定每个节点父节点为源点
    for(v = 1; v <= MG->Nv; v++) {
        dist[v] = MG->G[1][v];
        parent[v] = 1;
    }
    Tweight = vcnt = 0;
    //将源点收入MST
    dist[1] = 0;
    vcnt++;
    parent[1] = -1;
    //进行循环取节点
    for( ; ; ) {
        //找出为收入节点中dist最小者
        v = FindMinDist(MG, dist);
        if(v == -1) break;
        //将v与边收入MST
        E->v1 = parent[v];
        E->v2 = v;
        InsertEdge(MST, E);
        Tweight += dist[v];
        dist[v] = 0;
        vcnt++;
        //更新dist
        for(w = 1; w <= MG->Nv; w++)
        if(dist[w] != 0 && MG->G[v][w] != INF) {
            if(MG->G[v][w] < dist[w]) {
                dist[w] = MG->G[v][w];
                parent[w] = v;
            }
        }
    }
    if(vcnt != MG->Nv) Tweight = -1;
    printf("%d\n", Tweight);
}

int main() {
    int N, M;
    MGraph MG;
    LGraph MST;
    scanf("%d%d", &N, &M);
    MG = CreateMGraph(N, M);//PrintGraph(MG);
    MST = CreateLGraph(N);
    Prim(MG, MST);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值