POJ 2421Constructing Roads

本文探讨了如何使用最小生成树算法解决乡村道路建设问题,确保所有村庄间都能实现连接,并且总道路长度达到最小。通过实例解析算法步骤及应用,旨在提供一种高效、经济的道路规划解决方案。

注意:prime算法求最小生成树算法要非常熟练

时间限制:
2000ms
内存限制:
65536kB
描述
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.
输入
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.
输出
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.
样例输入
3
0 990 692
990 0 179
692 179 0
1
1 2
样例输出
179
程序一:带注释

#include <iostream>  
#define MAXN 200  
#define inf 10000  
typedef int elem_t;  
using namespace std;  
  
elem_t prim(int n,elem_t mat[MAXN][MAXN],int* pre){//pre[i]为最小生成树中各点的前驱点,本题可以省略
    elem_t min[MAXN],ret=0;  
    int v[MAXN],i,j,k;  
    for (i=0;i<n;i++)  
        min[i]=inf,v[i]=0,pre[i]=-1;  //v[i]标识一个元素是否被用过,min[i]
    for (min[j=0]=0;j<n;j++){  //min[0]=0,从0节点开始,共需要连n-1条边
        for (k=-1,i=0;i<n;i++)//k等于-1的原因是下一个入树的点一开始无法确定  
            if (!v[i]&&(k==-1||min[i]<min[k]))  
                k=i;  
     for (v[k]=1,ret+=min[k],i=0;i<n;i++)  //节点K进入最小生成树
            if (!v[i]&&mat[k][i]<min[i])  //每次增加一个节点,v[i]为V-U中各节点到U的最小权值
                min[i]=mat[pre[i]=k][i];  
    }  
    return ret;  
}  
int main(){  
    int n,i,j,result,q,a,b;  
    int pre[MAXN];  
    int distance[MAXN][MAXN];  
    cin>>n;  
    for (i = 0;i < n;i++)  
        for (j = 0;j < n;j++)  //试试用new会节省内存吗
            {  
                cin>>distance[i][j];  
            }  
        cin>>q;  
        for (i = 0;i < q;i++)  
        {  
            cin>>a>>b;  
            distance[a-1][b-1] = 0;  
            distance[b-1][a-1] = 0;//如果某条路已经修好,赋其权值为0  
        }  
        result  = prim(n,distance,pre);  
        cout << result;  
        return 0;  
}  


程序二:动态内存分配

#include<stdio.h>

#include<stdlib.h>
#define max_distance 1000
int prime(int **distance,int n){
int i,j,k,cost=0;
int *min=(int *)malloc(n*sizeof(int));
int *v=(int *)malloc(n*sizeof(int));
for(i=0;i<n;i++){
min[i]=max_distance;
v[i]=0;
}
min[0]=0;//第一个点进入时,防止min[0]没有初始化。
for(j=0;j<n;j++){
k=-1;
for(i=0;i<n;i++)
if(!v[i]&&(k==-1||min[i]<min[k]))
k=i;
cost+=min[k];
v[k]=1;
for(i=0;i<n;i++)
if(!v[i]&&distance[k][i]<min[i])
min[i]=distance[k][i];
}
return cost;
};
int main(){
int n,i,j,t,a,b;
scanf("%d",&n);
int **distance=(int **)malloc(n*sizeof(int *));
for(i=0;i<n;i++)
distance[i] = (int *)malloc(n*sizeof(int));
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&distance[i][j]);
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d%d",&a,&b);
distance[a-1][b-1]=0;
distance[b-1][a-1]=0;
}
printf("%d\n",prime(distance,n));
free(distance);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值