79分求调
  • 板块P1194 买礼物
  • 楼主Yxy7952
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/9/3 08:33
  • 上次更新2024/9/3 17:39:38
查看原帖
79分求调
936717
Yxy7952楼主2024/9/3 08:33
#include<bits/stdc++.h>
using namespace std;
int a,b,tot=0,v[1005],h[1005],sum=0;
struct edge{
	int nxt,to,w;
}lb[1005];
void cr(int u,int v,int w){
	tot++;
	lb[tot].to=v;
	lb[tot].w=w;
	lb[tot].nxt=h[u];
	h[u]=tot;
}
typedef pair<int,int>Pair;
priority_queue<Pair>q;
void prim(){
    q.push(make_pair(0,1));
    while(!q.empty()){
        int d=-q.top().first,u=q.top().second;
        q.pop();
        if(!v[u]){
	        sum+=d;
	        v[u]=1;
	        for(int i=h[u];i!=0;i=lb[i].nxt){
	        	q.push(make_pair(-lb[i].w,lb[i].to));
			}	
		}
    }
}
int main(){
	cin>>a>>b;
	for(int i=1;i<=b;i++){
		for(int j=1;j<=b;j++){
			int s;
			cin>>s;
			if(s!=0) cr(i,j,s),cr(j,i,s);
		}
		cr(0,i,a),cr(i,0,a);
	}
	prim();
	cout<<sum;
	return 0;
}
2024/9/3 08:33
加载中...