Pocket sky

Keywords: C++

Background of topic

Xiaoshan sits in the classroom and looks at the pocket like sky through the pocket like window.

There are a lot of clouds floating there. They look very beautiful. Xiaoshan wants to pick some clouds that are so beautiful and make them into marshmallows.

Title Description

Give you the number of clouds N, and then give you M relationships, indicating which clouds can be connected together.

Now Xiaoshan wants to connect all the clouds into K marshmallows. At least one cloud needs to be used for a marshmallow. Xiaoshan wants to know how to connect, and the cost is the least.

I / O format

Input format:

 

Of each group of test data

The first line has three numbers N,M,K(1 ≤ N ≤ 1000, 1 ≤ M ≤ 10000, 1 ≤ K ≤ 10)

Next, there are three numbers x, y and l in each row, indicating that x cloud and Y cloud can be connected together through the cost of L. (1≤X,Y≤N,0≤L<10000)

30% of data n < = 100, m < = 1000

Output format:

 

Output a row for each group of data, only an integer, indicating the minimum cost.

If you can't even output K marshmallows, please output 'No Answer'.

 

Example of input and output

Input example ා 1:
3 1 2
1 2 1
Output example:
1

Explain

Original of Xiamen No.1 Middle School YMS

 

Original title: Luo Valley P1195

Explanation:

You can think of each marshmallow??? In the topic as a tree (each marshmallow is connected and made up of multiple clouds)

In order to build this k-tree, we need to use n-k edges at the cost of n-k edges

In order to minimize the cost, we need to consider sorting and build a minimum spanning tree

 

The code is as follows:

#include <bits/stdc++.h>

using namespace std;

inline int read() {
    int x = 0 , f = 1; char ch = getchar();
    for ( ; !isdigit(ch) ; ch = getchar()) if (ch == '-') f = -1;
    for ( ; isdigit(ch) ; ch = getchar()) x = x * 10 + ch - '0';
    return x * f;
}

const int maxn = 1e5 + 10;
const int maxEdge = 1e5 + 10;

struct Edge {
    int from , to , dis;
    
    bool operator < (const Edge &k) const {
        return this->dis < k.dis;
    }
}edge[maxn];

int f[maxn];

int n , m , k , sum , ans;

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

inline void make() {
    for (int i = 1 ; i <= n ; i ++) {
        f[i] = i;
    }
}

inline void merge(int x , int y) {
    int fx = find(x) , fy = find(y);
    f[fy] = fx;
}

int main() {
    n = read() , m = read() , k = read();
    for (int i = 1 ; i <= m ; i ++) {
        edge[i].from = read() , edge[i].to = read() , edge[i].dis = read();
    }
    
    sort(edge + 1 , edge + m + 1);
    
    make();
    
    for (int i = 1 ; i <= m ; i ++) {
        int fx = find(edge[i].from) , fy = find(edge[i].to);
        
        if (fx != fy) {
            merge(edge[i].from , edge[i].to);
            sum ++;
            ans += edge[i].dis;
        }
        
        if (sum == n - k) {
            printf("%d\n" , ans);
            exit(0);
        }
    }
    
    puts("No Answer");
}

Posted by Xzone5 on Sat, 04 Jan 2020 19:22:11 -0800