POJ 2421Constructing Roads

来源:互联网 发布:线性代数 矩阵 编辑:程序博客网 时间:2024/06/10 01:23

注意: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.
样例输入
30 990 692990 0 179692 179 011 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;
}
原创粉丝点击