51Nod - 1212 无向图最小生成树

N个点M条边的无向连通图,每条边有一个权值,求该图的最小生成树。

Input

第1行:2个数N,M中间用空格分隔,N为点的数量,M为边的数量。(2 <= N <= 1000, 1 <= M <= 50000)

第2 - M + 1行:每行3个数S E W,分别表示M条边的2个顶点及权值。(1 <= S, E <= N,1 <= W <= 10000)

Output

输出最小生成树的所有边的权值之和。

Sample Input

9 14
1 2 4
2 3 8
3 4 7
4 5 9
5 6 10
6 7 2
7 8 1
8 9 7
2 8 11
3 9 2
7 9 6
3 6 4
4 6 14
1 8 8

Sample Output

37

题解:

适合入门写的基础题。

代码:

克鲁斯卡尔版:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

const int MAXN = 1005;

int N,M;

int pre[MAXN];//记录点的祖先 

struct Edge{//边结构体 
	int from;
	int to;
	int value;
	Edge(){}
	Edge(int a,int b,int c):from(a),to(b),value(c){}
};

struct cmp{
	bool operator()(struct Edge a,struct Edge b){
		return a.value > b.value;
	}
};

priority_queue<struct Edge,vector<struct Edge>,cmp> Q;

int Find(int a){
	if(pre[a] == a)return a;
	return pre[a] = Find(pre[a]);
	while(!Q.empty())Q.pop();
}

bool Judge(int a,int b){
	int A = Find(a);
	int B = Find(b);
	if(A != B){
		pre[A] = B;
		return true;
	}
	return false;
}

int Num,Sum;//表示已经找到的边数;表示最小生成树的值。 

inline void init(){//初始化函数,有需要在每组输入之前进行处理的都可以加到这里。 
	Sum = Num = 0;
	for(int i=1 ; i<=N ; i++)pre[i] = i;//初始化祖先为自己。 
}

int main(){
	
	int a,b,c;
	while(scanf("%d %d",&N,&M)!=EOF){
		init();
		for(int i=0 ; i<M ; i++){
			scanf("%d %d %d",&a,&b,&c);
			Q.push(Edge(a,b,c));
		}
		while(!Q.empty() && Num<N-1){
			if(Judge(Q.top().from,Q.top().to)){
				Sum += Q.top().value;
				++Num;
			}
			Q.pop();
		}
		printf("%d\n",Sum);
	}
	
	return 0;
}

prim版:

#include <cstdio>
#include <cstring>

using namespace std;

const int MAXN = 1005;  
const int INF = 0x3f3f3f3f;  
  
int N,M;  
  
struct Edge{  
 
    int to,next,value;   
    
}E[MAXN*100];  
  
int head[MAXN],top;   
  
inline void Add(int from,int to,int value){//邻接表加入边函数 
    E[++top].next = head[from];  
    head[from] = top;  
    E[top].value = value;  
    E[top].to = to;  
}  
  
bool book[MAXN];  
int lenth[MAXN];  
  
int prim(){  
 
    memset(book,false,sizeof book);  
    memset(lenth,INF,sizeof lenth);  
    
    book[1] = true;  
    
    int ans = 0;  
    
    for(int i=head[1] ; i ; i=E[i].next){  
        lenth[E[i].to] = E[i].value;  
    }  
    
    int minL = INF;//与“树”相连且权值最小的边的权值。   
    int mint;//与“树”相连且边权值最小的点的下标。 
    
    for(int i=2 ; i<=N ; i++){//因为以第一个点为根,所以只需要再找N-1个点就够了。  
    
        for(int j=2 ; j<=N ; j++){ //第一个点已经确定,所以每次从第二个点开始找。 
            if(book[j] == false && lenth[j]<minL){  
                minL = lenth[j];  
                mint = j;  
            }  
        }  
        
        ans += minL; 
        minL = INF;  
        
        book[mint] = true;  
        
        for(int k=head[mint] ; k ; k=E[k].next){  
            if(book[E[k].to] == false && lenth[E[k].to] > E[k].value)lenth[E[k].to] = E[k].value;  
        }  
        
    }  
    
    return ans; 
	 
}  
 
inline void init(){
    memset(head,0,sizeof head);  
    top = 0;  
}    

int main(){
	
	while(scanf("%d %d",&N,&M) == 2){
		init();
		int a,b,v;
		for(int i=1 ; i<=M ; ++i){
			scanf("%d %d %d",&a,&b,&v);
			Add(a,b,v);
			Add(b,a,v);
		}
		printf("%d\n",prim());
	}
	
	return 0;
}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值