最小生成树1.2

· · 算法·理论

最小生成树1.2

咱们先复习一下吧 树是什么? 树是n个点,n - 1条边,无环的图,比如 不好意思,贴错了,是这个 但是最小生成树是什么呢?

比如这个图

他的最小生成树是什么呢? 对了,就是这个↓

注:感谢yr409892525提供的图片

那么他是怎么来的呢? 很简单。 我们根据定义和方法求解。 这就用到了以下概念

生成树是什么?

给出一个无向带权连通的图,删掉一些边后变成了树。 这个过程就是生成树。

生成树并不唯一,删掉的边不同,构成的树也不同。

最小生成树是什么

最小生成树就是生成树中,边权值和最小的树。

So

我们的哪一个答案并不是很严谨的,那只是其中一种

言归正传

言归正传,我们还需要知道最小生成树一定是生成树,而生成树不一定是最小生成树( 等于没说 )

我们一般定义函数名叫做

Kruskal(当然,我用的不是)

使用算法:并查集、贪心 将每一条边按边权从小到大排列。 每次选当前最小边权的那条边。 如果这条边两点不连接,则连接这条边。

真正的挑战

不开玩笑了,上例题

P3366 【模板】最小生成树

简简单单的模板题目,不会有人不会吧 话不多说,上代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=200005;
int power(int a, int b);
int gcd(int x,int y);
int lcm(int x,int y);
int n,m,sum,ans,fa[NNN];
struct node{
    int x,y,z;
}a[NNN];
bool cmp(node x,node y){
    return x.z < y.z;
} 
int find(int x){
    if(fa[x] == x){
        return x;
    }
    return fa[x] = find(fa[x]);
}
void dfs(){
    for(int i = 1; i <= m; i++){
        if(ans == n - 1){
            break;
        }
        int u = find(a[i].x), v = find(a[i].y);
        if(u != v){
            sum += a[i].z;
            ans ++;
            fa[u] = v;
        }
    }
    return ;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }
    for(int i = 1; i <= m; i++){
        cin >> a[i].x >> a[i].y >> a[i].z;
    }
    sort(a + 1, a + m + 1, cmp);
    dfs();
    if(ans == n - 1){
        cout << sum;
    }else{
        cout << "orz";
    }
    return 0;
}
int power(int a, int b){int ans = 1, aa = a;while(b){if(b & 1){ans *= aa;}aa *= aa;b >>= 1;}return ans;}
int gcd(int x,int y){if(y == 0){return x;} return gcd(y, x % y);}
int lcm(int x,int y){return x * y / gcd(x,y);}

P1195 口袋的天空

我们知道树 n 个点只有n −1n−1 条边。 那么每连接一条边就会减少一个连通快个数。 所以用 sum 记录当前连通块个数,初始为 n 。 使用最小生成树,每建立一条边,连通块个数减一。 当连通块个数为 k 时,当前边权和为答案。

话不多说,上代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=200005;
int power(int a, int b);
int gcd(int x,int y);
int lcm(int x,int y);
int n,m,sum,ans,fa[NNN],k;
struct node{
    int x,y,z;
}a[NNN];
bool cmp(node x,node y){
    return x.z < y.z;
} 
int find(int x){
    if(fa[x] == x){
        return x;
    }
    return fa[x] = find(fa[x]);
}
void dfs(){
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }
    for(int i = 1; i <= m; i++){
        if(find(a[i].x) != find(a[i].y)){
            int u = find(a[i].x), v = find(a[i].y);
            if(u != v){
                fa[u] = v;
            }
            sum --;
            ans += a[i].z;
            if(sum == k){
                return ;
            }
        }   
    }
    return ;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++){
        cin >> a[i].x >> a[i].y >> a[i].z;
    }
    sort(a + 1, a + 1 + m, cmp);
    sum = n;
    dfs();
    if(sum == k){
        cout << ans;
    }else{
        cout << "No Answer\n";
    }
    return 0;
}
int power(int a, int b){int ans = 1, aa = a;while(b){if(b & 1){ans *= aa;}aa *= aa;b >>= 1;}return ans;}
int gcd(int x,int y){if(y == 0){return x;} return gcd(y, x % y);}
int lcm(int x,int y){return x * y / gcd(x,y);}

P1194 买礼物

按照 K i j建边,如果 K i j等于0,就建立 i 到 j 有边权为 A 的边。

再使用最小生成树。

统计边权和,再加上 A ,因为要从一个物品出发。 话不多说,上代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=200005;
int power(int a, int b);
int gcd(int x,int y);
int lcm(int x,int y);
int n,m,k,sum,ans,fa[NNN],k;
struct node{
    int x,y,z;
}a[NNN];
bool cmp(node x,node y){
    return x.z < y.z;
} 
int find(int x){
    if(fa[x] == x){
        return x;
    }
    return fa[x] = find(fa[x]);
}
void dfs(){
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }
    for(int i = 1; i <= k; i++){
        if(find(a[i].x) != find(a[i].y)){
            sum += a[i].z;
            int u = find(a[i].x), v = find(a[i].y);
            if(u != v){
                fa[u] = v;
            }
        }   
    }
    return ;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> m >> n;
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            int x;
            cin >> x;
            if(x > 0){
                a[++k] = {i, j, x};
            }
        }
    }
    for(int i = 1; i <= n; i++){
        a[++k] = {0, i, m};
    }
    sort(a + 1, a + 1 + k, cmp);
    dfs();
    cout << sum;
    return 0;
}
int power(int a, int b){int ans = 1, aa = a;while(b){if(b & 1){ans *= aa;}aa *= aa;b >>= 1;}return ans;}
int gcd(int x,int y){if(y == 0){return x;} return gcd(y, x % y);}
int lcm(int x,int y){return x * y / gcd(x,y);}

P2330 [SCOI2005] 繁忙的都市

题目里构成的树的名字叫做“瓶颈生成树”,可以证明最小生成树一定是瓶颈生成树,但反之不一定。

#include<bits/stdc++.h>
#define double long double
#define int long long
#define mem(x,n) memset(x, n, sizeof(x));
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=1000005;
const int im=INT_MAX;
const int MOD=1e9;
int power(int a, int b);
int gcd(int x,int y);
int lcm(int x,int y);
int fa[200005];
struct node{
    int x,y,z;
}a[200005];
int n,m; 
int ans,sum;
bool cmp(node x,node y){
    return x.z < y.z;
}
int find(int x){
    if(x == fa[x]){
        return x;
    }
    return fa[x] = find(fa[x]);
}
void dfs(){
    for(int i = 1; i <= m; i++){
        if(ans == n - 1){
            break;
        }
        if(find(a[i].x) != find(a[i].y)){
            x = find(a[i].x);
            y = find(a[i].y);
            if(x != y){
                fa[x] = y;
            }
            sum = a[i].z;
            ans++;
        }
    }
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }for(int i=1;i<=m;i++){
        cin >> a[i].x >> a[i].y >> a[i].z;
    }
    sort(a + 1,a + m + 1,cmp);
    dfs();
    cout << ans << " " << sum;
    return 0;
}
int power(int a, int b){int ans = 1, aa = a;while(b){if(b & 1){ans *= aa;ans %= MOD;}aa *= aa;aa %= MOD;b >>= 1;}return ans;}
int gcd(int x,int y){if(y == 0){return x;} return gcd(y, x % y);}
int lcm(int x,int y){return x * y / gcd(x,y);}

P2916 [USACO08NOV] Cheering up the Cow G

由于这题要加上对点权的处理,比较麻烦。

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=200005;
const int MOD = 1e9;
int power(int a, int b);
int gcd(int x,int y);
int lcm(int x,int y);
int n, m, d[NNN], ans, minn = 1e18;
int sum,fa[NNN];
struct node{
    int x,y,z;
}a[NNN];
bool cmp(node x,node y){
    return x.z < y.z;
} 
int find(int x){
    if(fa[x] == x){
        return x;
    }
    return fa[x] = find(fa[x]);
}
void dfs(){
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }
    for(int i = 1; i <= m; i++){
        if(ans == n - 1){
            break;
        }
        int u = find(a[i].x), v = find(a[i].y);
        if(u != v){
            fa[u] = v;
            sum += a[i].z;
            ans ++;
        }
    }
    return ;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        cin >> d[i];
        minn = min(minn, d[i]);
    }
    for(int i = 1; i <= m; i++){
        int x, y, z;
        cin >> x >> y >> z;
        a[i] = {x, y, z * 2 + d[x] + d[y]};
    }
    sort(a + 1, a + 1 + m, cmp);
    dfs();
    cout << sum + minn;
    return 0;
}

[ABC328E] Modulo MST

由于n不是一般的小,咱们直接上DFS

#include<bits/stdc++.h>
#define int long long
using namespace std;
typedef long long ll;
const int N=105;
const int NN=3005;
const int NNN=200005;
const int MOD = 1e9;
int n, m, k, ans = 1e18;
int fa[NNN];
int v[NNN];
struct node{
    int x,y,z;
}a[NNN];
int find(int x){
    if(fa[x] == x){
        return x;
    }
    return fa[x] = find(fa[x]);
}
int check(){
    int sum = 0;
    for(int i = 1; i <= n; i++){
        fa[i] = i;
    }
    for(int i = 1; i < n; i++){
        int u = find(a[v[i]].x), vv = find(a[v[i]].y);
        if(u != vv){
            fa[u] = vv;
            sum += a[v[i]].z;
            sum %= k;
        }else{
            return 2e18;
        }
    }
    return sum % k;
}
void dfs(int st){
    if(st == n){
        ans = min(ans, check());
        return ;
    }
    for(int i = v[st - 1] + 1; i <= m; i++){
        v[st] = i;
        dfs(st + 1);
    }
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    for(int i = 1; i <= m; i++){
        cin >> a[i].x >> a[i].y >> a[i].z;
    }
    dfs(1);
    cout << ans;
    return 0;
}


作业

P2121 拆地毯

P1547 [USACO05MAR] Out of Hay S

P2212 [USACO14MAR] Watering the Fields S

P1669 [USACO04DEC] Bad Cowtractors S