每日一题:最小生成树

板子:

最小生成树【模板】最小生成树 - 洛谷

代码实现:

稠密图

#include<bits/stdc++.h>
using namespace std;
const int N=510,INF=0x3f3f3f3f;
int n,m;
int g[N][N],dis[N];
bool st[N];
int prim(){
    memset(dis,0x3f,sizeof dis);
    int res=0;
    for(int i=0;i<n;i++){
        int t=-1;
        for(int j=1;j<=n;j++){
            if(!st[j]&&(t==-1||dis[t]>dis[j]))
               t=j;
        }
        if(i&&dis[t]==INF)return INF;
        if(i)res+=dis[t];
        for(int j=1;j<=n;j++)dis[j]=min(dis[j],g[t][j]);//这个点到连边的距离
        st[t]=true;
    }
    return res;
}
signed main(){
    scanf("%d%d",&n,&m);
    memset(g,0x3f,sizeof g);
    while(m--){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        g[a][b]=g[b][a]=min(g[a][b],c);
    }
    int t=prim();
    if(t==INF)puts("impossible");
    else printf("%d\n",t);
}

例题:

新的开始 新的开始 - LibreOJ 10066 - Virtual Judge

题目描述

发展采矿业当然首先得有矿井,小 FF 花了上次探险获得的千分之一的财富请人在岛上挖了 n 口矿井,但他似乎忘记考虑的矿井供电问题……

为了保证电力的供应,小 FF 想到了两种办法:

  1. 在这一口矿井上建立一个发电站,费用为 v(发电站的输出功率可以供给任意多个矿井)。
  2. 将这口矿井与另外的已经有电力供应的矿井之间建立电网,费用为 p。

小 FF 希望身为「NewBe_One」计划首席工程师的你帮他想出一个保证所有矿井电力供应的最小花费。

输入格式

第一行一个整数 n,表示矿井总数。

第 2∼n+1 行,每行一个整数,第 i 个数 vi​ 表示在第 i 口矿井上建立发电站的费用。

接下来为一个 n×n 的矩阵 p,其中 pi,j​ 表示在第 i 口矿井和第 j 口矿井之间建立电网的费用(数据保证有 pi,j​=pj,i​,且 pi,i​=0)。

输出格式

输出仅一个整数,表示让所有矿井获得充足电能的最小花费。

样例

InputcopyOutputcopy
4
5
4
4
3
0 2 2 2
2 0 3 3
2 3 0 4
2 3 4 0
9

小 FF 可以选择在 44 号矿井建立发电站然后把所有矿井都不其建立电网,总花费是 3+2+2+2=93+2+2+2=9。

数据范围与提示

对于 30%30% 的数据:1≤n≤50;
对于 100%100% 的数据:1≤n≤300,0≤vi​,pi,j​≤10^5。

代码实现:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <vector>
#include <numeric>
#include <unordered_map>
#include <queue>
#include <set>
// #include <bits/stdc++.h>
#define endl '\n'
#define x first
#define y second
#define falg flag
// #define int long long
#define all(x) x.begin(),x.end()
#define dbug(x) cout << #x << '=' << x << endl;
#define Dbug(x,y) cout << #x << '=' << x << ',' << #y << '=' << y << endl;
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=((int)2e5)+10;
const int M=((int)5e2)+10;
const int P=1331;
const int INF=0x3f3f3f3f;
const int mod=1e9+7;
const double eps=1e-6;
const double PI=acos(-1);

int n;
int ans;
int p[M];
struct node{
    int w,a,b;
};
vector<node> v;

bool cmp(node A,node B){
    return A.w<B.w;
}

int find(int x){
    if(p[x]!=x)p[x]=find(p[x]);
    return p[x];
}

void solve(){
    for(int i=0;i<M;i++)p[i]=i;
    cin >> n;
    for(int i=1;i<=n;i++){
        int k;cin >> k;
        v.push_back({k,i,0});
    }
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            int k;cin >> k;
            v.push_back({k,i,j});
        }
    sort(all(v),cmp);
    for(auto it:v){
        int a=find(it.a);int b=find(it.b);
        if(a!=b){
            p[a]=b;
            ans+=it.w;
        }
    }
    cout << ans << endl;
}

signed main(){
       solve();
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值