7-10 公路村村通

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

输入格式:

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

输出格式:

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

输入样例:

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>
//表示起来比较复杂,其实根据题目意思可以进行简单表示
/*邻接矩阵表示*/
/*最小生成树-Prim算法实现*/
/*MST用邻接表表示*/
#define MaxNum 1000
#define INFINITY 65533
typedef int Vertex;
typedef int Weight;
//邻接矩阵定义
struct GNode{
    int Nv;
    int Ne;
    Weight G[MaxNum][MaxNum];
};
typedef struct GNode *Graph;
struct ENode{
    Vertex V1;
    Vertex V2;
    Weight EW;
};
typedef struct ENode *Edge;
//邻接表定义
typedef struct AdjNode *PAdjNode;
struct AdjNode{
    Vertex adj;//下标
    Weight W;
    PAdjNode next;
};
struct HNode{
    PAdjNode firstNode;
};
typedef struct HNode HNODE[MaxNum];
struct LGNode{
    int V;
    int E;
    HNODE LG;
};
typedef struct LGNode *LGraph;
struct LENode{
    Vertex V1;
    Vertex V2;
    Weight LEW;
};
typedef struct LENode *LEdge;

Vertex FindMin(Graph G, int dist[])
{
    Vertex MinV;
    Weight Mindist = INFINITY;
    for (int i = 1; i <= G->Nv; i++)
        if (dist[i] != 0 && dist[i] < Mindist){
            Mindist = dist[i];
            MinV = i;
        }
    if (Mindist < INFINITY)
        return MinV;
    else
        return -1;
}

Weight Prim(Graph G)    
{
    Weight TotalWeight = 0;
    int cnt = 0;
    LGraph MST = (LGraph)malloc(sizeof(struct LGNode));
    Vertex S = 1;
    Vertex V;
    int dist[MaxNum];
    Vertex path[MaxNum];
    for (int i = 1; i <= G->Nv; i++){
        dist[i] = G->G[S][i];
        path[i] = S;
    }
    dist[S] = 0;
    cnt++;
    path[S] = -1;
    while (1){
        V = FindMin(G, dist);
        if (V == -1)
            break;
        TotalWeight += dist[V];
        dist[V] = 0;
        cnt++;
        for (int i = 1; i <= G->Nv; i++)
            if (dist[i] != 0 && G->G[V][i] < INFINITY)
                if (G->G[V][i] < dist[i]){
                    dist[i] = G->G[V][i];
                    path[i] = V;
                }
    }
    if (cnt < G->Nv)
        return -1;
    else
        return TotalWeight;
}

int main(int argc, char const *argv[])
{
    Graph G = (Graph)malloc(sizeof(struct GNode));
    Edge E = (Edge)malloc(sizeof(struct ENode));
    int N, M;
    scanf("%d %d\n", &N, &M);
    G->Nv = N;
    G->Ne = M;
    //图初始化
    for (int i = 1; i <= G->Nv; i++)
        for (int j = 1; j <= G->Nv; j++)
            G->G[i][j] = INFINITY;
    //插入边
    for (int i = 0; i < G->Ne; i++){
        scanf("%d %d %d", &E->V1, &E->V2, &E->EW);
        G->G[E->V1][E->V2] = E->EW;
        G->G[E->V2][E->V1] = E->EW;
    }
    int cost;
    cost = Prim(G);
    printf("%d\n", cost);
    system("pause");
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章