acw 1146. A new beginning [Super source point application of minimum spanning tree]

Of course, to develop the mining industry, you must first have mines. Little FF spent one thousandth of the wealth obtained from the last expedition to hire people to dig n mines on the island, but he seemed to have forgotten to consider the power supply of the mines.

In order to ensure the supply of electricity, Little FF thought of two methods:

Build a power station on mine i at a cost of vi (the output power of the power station can be supplied to any number of mines).
To establish a power grid between mine i and another mine j that already has power supply , the cost is pi,j. Little FF hopes you can help him come up with a minimally cost solution to ensure the power supply to all mines.

Input format: The first line contains an integer n, representing the total number of mines.

The next n lines contain an integer in each line. The i-th number vi represents the cost of building a power station on the i-th mine.

Next is an n×n matrix P, where pi,j represents the cost of establishing a power grid between the i-th mine and the j-th mine.

The data guarantees that pi,j=pj,i, and pi,i=0.

Output format: Output an integer representing the minimum cost for all mines to obtain sufficient power.

Data range 1≤n≤300, 0≤vi,pi,j≤105

For this problem of finding the minimum overall cost, consider MST. Each point has a weight and contains edge weights. Think of super source points.
We assume that there is a super power station, and assume that each point has an edge with this super source point. The edge weight is the cost of building this point. Then I can directly run MST to get the minimum cost of electrifying each point. The meaning of this question is not the same
. Instead of just building one power station, there can be many connected blocks, so that every point can generate electricity.
For example:
Insert image description here

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+7;
struct node{
    
    
    int from,to,d;
}s[N];
int cnt=0;
int cmp(const node s1,const node s2){
    
    
    return s1.d<s2.d;
}
int father[N];
int find(int x){
    
    
    if(x==father[x]) return x;
    return father[x]=find(father[x]);
}

int main(){
    
    
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) father[i]=i;
    for(int i=1;i<=n;i++){
    
    
        int v;cin>>v;
        s[++cnt]={
    
    i,n+1,v};
    }
    for(int i=1;i<=n;i++){
    
    
        for(int j=1;j<=n;j++){
    
    
            int x;
            cin>>x;
            if(i>j) s[++cnt]={
    
    i,j,x};
        }
    }
    sort(s+1,s+1+cnt,cmp);
    int res=0;
    for(int i=1;i<=cnt;i++){
    
    
        int a=s[i].from,b=s[i].to,w=s[i].d;
        int dx=find(a),dy=find(b);
        if(dx!=dy){
    
    
            father[dx]=dy;
            res+=w;
        }
    }
    cout<<res<<endl;
    return 0;
}

Water question solution TAT

Guess you like

Origin blog.csdn.net/weixin_51461002/article/details/126755901