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  
#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         min[i]=inf,v[i]=0,pre[i]=-1;  //v[i]标识一个元素是否被用过,min[i]
    for (min[j=0]=0;j         for (k=-1,i=0;i             if (!v[i]&&(k==-1||min[i]                 k=i;  
     for (v[k]=1,ret+=min[k],i=0;i             if (!v[i]&&mat[k][i]                 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

#include
#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 min[i]=max_distance;
v[i]=0;
}
min[0]=0;//第一个点进入时,防止min[0]没有初始化。
for(j=0;j k=-1;
for(i=0;i if(!v[i]&&(k==-1||min[i] k=i;
cost+=min[k];
v[k]=1;
for(i=0;i if(!v[i]&&distance[k][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 distance[i] = (int *)malloc(n*sizeof(int));
for(i=0;i for(j=0;j scanf("%d",&distance[i][j]);
scanf("%d",&t);
for(i=0;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;
}

你可能感兴趣的:(POJ 2421Constructing Roads)