HDU1102 最小生成树(已构造好部分边)Prim与Kruskal

0)

        题意:已经在部分城市之间建造了道路,求再花费多少钱建造道路使得所有城市连通。

  

       求连接所有点的最小花费,这是最小生成路问题,变形在于已经修建好了部分道路。之前的思路是,在所有未连接的点图中,如何构建出所有需要的道路,而在构建之前如何将已经修好的路放进去,事实上这样的思想不明确也不简洁。好的做法是将修好的道路的修建花费直接赋值为0。然后就可以直接上最小生成树的模板,Prim或者Kruskal都可以。这么来看的话,这个题也是比较裸的。


       (Prim从点出发更适合稠密图,Kruskal从边出发更适合稀疏图)

1)

Prim

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;
const int maxn=110;
const int INF=0x3f3f3f3f;
int mat[maxn][maxn];
int dis[maxn];
int used[maxn];
int sum=0;
void Prim(int n){
    int cur=1;
    used[cur]=1;
    for(int i=1;i<=n;i++){
        dis[i]=mat[cur][i];
    }
    for(int i=1;i<n;i++){
        int cur_min=INF;
        int temp=-1;
        for(int j=1;j<=n;j++){
            //if(cur==j) continue;之前因为cur写成了i所以wa,实际上这一句话不用写
            if(used[j]==0&&cur_min>dis[j]){
                    cur_min=dis[j];
                    temp=j;
            }
        }
        if(temp==-1){
            break;
        }
        sum+=cur_min;
        cur=temp;
        used[cur]=1;
        for(int j=1;j<=n;j++){
            if(used[j]==0)
                dis[j]=min(dis[j],mat[cur][j]);//更新确定集合,到j点的最小距离
        }
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n)){
        memset(mat,0,sizeof(mat));
        memset(used,0,sizeof(used));
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&mat[i][j]);
            }
        }
        int q;
        //int coun=0;
        scanf("%d",&q);
        while(q--){
            int l,r;
            scanf("%d%d",&l,&r);
            mat[l][r]=0;//精妙所在
            mat[r][l]=0;
        }
        sum=0;
        memset(dis,INF,sizeof(dis));
        Prim(n);
        cout<<sum<<endl;
    }
    return 0;
}


Kruskal

......


2)

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
 


你可能感兴趣的:(最小生成树,Prim,kruskal)