HDU ACM 1102 / POJ 2421 Constructing Roads(MST)

Constructing Roads

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


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
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output
179
 
Source
 
Recommend
Eddy
 
 
#include<stdio.h>
#include<string.h>
#define MAXN_COST 1050
typedef struct{
    int w; 
    int is_bulid; // 初始值为0,路已铺好则为1 
}road;

road path[102][102];
int flag[102];
int closedge[102], cnt = 0, circus = 0;

int BuildCri(int x, int n)
{// 该函数实现的功能是将刚进入生成圈的节点相关的已铺好的路径找出来,如果有,将邻接的节点调入圈内,再调用此函数
 // 如果无出现则返回。回溯的时候保持短路径的“先进性” 
    int i, j;
    for(i=2; i<=n; ++i)
    {
        if(!flag[i] && path[x][i].is_bulid)  // i在圈内会发生什么情况,首先要知道x已进入圈内, i不会在圈内,因为是无向图 
        {
            flag[i] = 1;
            path[x][i].is_bulid = path[i][x].is_bulid = 0; 
            for(j=2; j<=n; ++j)
            if(!flag[j] && closedge[j] > path[i][j].w)
                {
                    closedge[j] = path[i][j].w;
                }
            circus++;
            BuildCri(i, n);
        }
    }
    return 0;
}

int CreatMST(int n)
{// 最小生成树函数描述 
    int i, j, min, x, y, t;
    cnt = 0;
    circus = 0;
    memset(closedge, 0, sizeof(closedge));
    for(i=1; i<=n; ++i)
    closedge[i] = path[1][i].w;
    
    memset(flag, 0, sizeof(flag));
    flag[1] = 1;
    
    BuildCri(1, n);
    
    for(t=2+circus; t<=n; t=t+circus+1)
    {
        circus = 0;   // BuildCri函数统计每次纳入一条为被铺好的路时牵涉进去的路的条数,以判断此for循环循环的次数 
        min = MAXN_COST;
        for(i=2; i<=n; ++i)
        {
            if(!flag[i] && closedge[i] < min)
                {y = i; min = closedge[i];}
        }
        cnt += min;
        flag[y] = 1;
        BuildCri(y, n);
        
        for(i=2; i<=n; ++i)
            {
                if(!flag[i] && closedge[i] > path[y][i].w)
                {
                    closedge[i] = path[y][i].w;
                }
            }
    }
    
    return cnt;
}

int main()
{
    int x, y, i, j, n, m, t, len, weight;
    while(scanf("%d", &n) != EOF)
    {
        memset(path, 0, sizeof(path));
        for(i=1; i<=n; ++i)
        for(j=1; j<=n; ++j)
        {
            scanf("%d", &weight);
            path[i][j].w = !weight ? MAXN_COST : weight;
        }
        scanf("%d", &m);
        for(i=0; i<m; ++i)
        {
            scanf("%d%d", &x, &y);
            path[x][y].is_bulid = path[y][x].is_bulid = 1;
        }
        printf("%d\n", CreatMST(n));
    }
    return 0;
}

解题报告:

关键是怎样将已铺建的路纳入最小生成树内而不记录路径的长度。

测试数据:

input:

7
0 1 3 4 0 0 0
1 0 2 0 8 7 0
3 2 0 0 0 0 0
4 0 0 0 5 0 9
0 8 0 5 0 0 0
0 7 0 0 0 0 6
0 0 0 9 0 6 0
4
1 3
4 5
4 7
6 7

output:

5

Kruskal算法实现

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define SIZE 101
#define MAXN 2000 

using namespace std;

int n, m, cnt;
int father[SIZE];
int road[SIZE][SIZE];

struct edge{
    int first, second;
    int value;
}query[SIZE*SIZE];

int find_father(int f)
{
    return father[f] = f == father[f] ? f : find_father(father[f]);
}

bool cmp(struct edge a, struct edge b)
{
    return a.value < b.value;
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
    cnt = 0;
    for(int i=0; i<n; ++i)
    for(int j=0; j<n; ++j)
    {
        scanf("%d", &road[i][j]);
        if(i<j)
        {
            query[cnt].first = i;
            query[cnt].second = j;
            query[cnt].value = road[i][j];
            cnt++;
        }    
    }
    int q;
    scanf("%d", &q);
    for(int i=0; i<n; ++i) father[i] = i;
    for(int i=0; i<q; ++i)
    {
        int first, second;
        scanf("%d%d", &first, &second);
        first = find_father(first-1);
        second = find_father(second-1);
        father[second] = first;
    }
    int cost = 0;
    sort(query, query+cnt, cmp);
    for(int i = 0; i < cnt; ++i)
    {
        struct edge &cur_edge = query[i];
        int first, second;
        first = find_father(cur_edge.first);
        second = find_father(cur_edge.second);
        if(first != second)
        {
            father[second] = first;
            cost += cur_edge.value;
        }
    }
    printf("%d\n", cost);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/liaoguifa/p/2819455.html