hdu1102 - Constructing Roads (求最小生成树) (Prim & Kruskal)

来源:互联网 发布:黄政民在韩国地位知乎 编辑:程序博客网 时间:2024/04/30 18:49

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14056    Accepted Submission(s): 5351


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
 

Sample Input
30 990 692990 0 179692 179 011 2
 

Sample Output
179
 

Source
kicc
 
          先将已有的道路 存入集合,再进行Kruskal
/*************************************   acm:   hdu-1102**title: Constructing Roads**time : 2014.8.13************************************///考察 最小生成树//本题运用 Kruskal算法/*    题意:    邻接矩阵存储n个村庄,现已有m条道路,    在这添加一些道路使其所有村庄联通,求添加的道路最短距离。*/#include <stdio.h>#include <stdlib.h>#include <string.h>#define INFINITY 65535#define MAXVEX 101#define MAXEDGE 5152typedef struct MGraph{    int arc[MAXVEX][MAXVEX];    int numVertexes;    int numEdges;} MGraph;typedef struct Edge{    int begin;    int end;    int weight;} Edge;void CreateMGraph(MGraph *G, int n){    int i;    int j;    G->numVertexes = n;    for (i = 1; i <= G->numVertexes; i++)    {        for (j = 1; j <= G->numVertexes; j++)        {            scanf("%d", &G->arc[i][j]);        }    }}//交换权值  以及头和尾void Swapn(Edge *edges, int i, int j){    int temp;    temp = edges[i].begin;    edges[i].begin = edges[j].begin;    edges[j].begin = temp;    temp = edges[i].end;    edges[i].end = edges[j].end;    edges[j].end = temp;    temp = edges[i].weight;    edges[i].weight = edges[j].weight;    edges[j].weight = temp;}void sort(Edge edges[], MGraph *G){    int i, j;    int k;    for (i = 0; i < G->numEdges -1; i++)    {        k = i;        for (j = i + 1; j < G->numEdges; j++)        {            if (edges[k].weight > edges[j].weight)            {                k = j;            }        }        if (k != i)        {            Swapn(edges, k, i);        }    }}int Find(int *parent, int f){    int k = f;    int temp;    while (parent[f] > 0)    {        f = parent[f];    }    while (k != f)    {        temp = parent[k];        parent[k] = f;        k = temp;    }    return f;}int MiniSpanTree_Kruskal(MGraph *G){    int i, j, n, m;    int k = 0;    int parent[MAXVEX];   //用一数组来判断边与边是否形成环路(并查集)    Edge edges[MAXEDGE];  //定义边集数组    int temp;    int num = 0;    //构建边集数组    for (i = 1; i < G->numVertexes; i++)    {        for (j = i + 1; j <= G->numVertexes; j++)        {            edges[k].begin = i;            edges[k].end = j;            edges[k].weight = G->arc[i][j];            k++;        }    }    G->numEdges = k;    sort(edges, G);    /**************************************************/    memset(parent, -1, sizeof(parent));    scanf("%d", &j);    for (i = 0; i < j; i++)  //此循环wa了n次    {        scanf("%d%d", &n, &m);        n = Find(parent, n);        m = Find(parent, m);        if (n != m)        {            temp = parent[n] + parent[m];            if (parent[n] > parent[m])            {                parent[n] = m;                parent[m] = temp;            }            else            {                parent[m] = n;                parent[n] = temp;            }        }    }    for (i = 0; i < G->numEdges; i++)    {        n = Find(parent, edges[i].begin);        m = Find(parent, edges[i].end);        if (n != m)        {            temp = parent[n] + parent[m];            if (parent[n] > parent[m])            {                parent[n] = m;                parent[m] = temp;            }            else            {                parent[m] = n;                parent[n] = temp;            }            num += edges[i].weight;        }    }    return num;}int main(){    int n;    MGraph G;    while (~scanf("%d", &n))    {       CreateMGraph(&G, n);/*  静态数据        G.arc[1][1] = 0;        G.arc[1][2] =990;        G.arc[1][3] = 692;        G.arc[2][1] = 990;        G.arc[2][2] = 0;        G.arc[2][3] = 179;        G.arc[3][1] = 692;        G.arc[3][2] = 179;        G.arc[3][3] = 0;        G.numVertexes= 3;*/        n = MiniSpanTree_Kruskal(&G);        printf("%d\n", n);    }    return 0;}

   Prim算法:  将现有的道路距离改为0,再进行Prim算法 。注意:标记数组 lowcost[] = -1  不能为 0
/*************************************   acm:   hdu-1102**title: Constructing Roads**time : 2014.8.13************************************///考察 最小生成树//本题运用 Prim算法/*    题意:    邻接矩阵存储n个村庄,现已有m条道路,    在这添加一些道路使其所有村庄联通,求添加的道路最短距离。*/#include <stdio.h>#include <stdlib.h>#define INFINITY 65535#define MAXVEX 101typedef struct MGraph{    int arc[MAXVEX][MAXVEX];    int numVertexes;    int numEdges;} MGraph;void CreateMGraph(MGraph *G, int n){    int i;    int j;    G->numVertexes = n;    for (i = 1; i <= G->numVertexes; i++)    {        for (j = 1; j <= G->numVertexes; j++)        {            scanf("%d", &G->arc[i][j]);        }    }}int MiniSpanTree_Prim(MGraph *G){    int min, i, j, k;    int num = 0;    int lowcost[MAXVEX];   //记录最小权值    int adjvex[MAXVEX];   //记录相关顶点下标    lowcost[1] = -1;  //记录该结点,v1加入生成树    adjvex[1] = 1;    for (i = 2; i <= G->numVertexes; i++)    {        lowcost[i] = G->arc[1][i];        adjvex[i] = 1;    }    k = 1;    for (i = 1; i < G->numVertexes; i++)    {        min  = INFINITY;        j = 2;        while (j <= G->numVertexes)        {            if (lowcost[j]!=-1 && lowcost[j] < min)            {                min = lowcost[j];                k = j;            }            j++;        }        num += min;        lowcost[k] = -1;        for (j = 1; j <= G->numVertexes; j++)        {            if (lowcost[j]!=-1 && G->arc[k][j] < lowcost[j])            {                lowcost[j] = G->arc[k][j];                adjvex[j] = k;            }        }    }    return num;}int main(){    MGraph G;    int n;int a, b;int i;    while (~scanf("%d", &n))    {        CreateMGraph(&G, n);scanf("%d", &n);for (i = 0; i < n; i++){scanf("%d%d", &a, &b);G.arc[a][b] = 0;G.arc[b][a] = 0;}        n = MiniSpanTree_Prim(&G);        printf("%d\n", n);    }    return 0;}

0 0
原创粉丝点击