【kuangbin带你飞】专题六 最小生成树

[kuangbin带你飞]专题六 最小生成树

A、POJ - 1251 Jungle Roads (最小生成树模板)

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input
The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above.
Output
The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit.

细节还是有的。

写题别想当然地做,还是要按照题目要求用ch-'A'来写,并且因为ch-'A'会等于0,所以我们初始化的时候边界要到0

for(int i = 0; i <= n; ++i)
    fa[i] = i;
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>

using namespace std;

const int N = 500007;

int n, m;
struct node{
    int x, y, z;
    bool operator<(const node &t)const {
        return z < t.z;
    }
}a[N];

int t;
int cnt;
int fa[N];

int find(int x){
    if(fa[x] == x)return x;
    return fa[x] = find(fa[x]);
}
int num;
int main(){
    while(~scanf("%d", &n) && n){
        cnt = 0;
        for(int i = 1, x; i <= n - 1; ++ i){
            char ch;
            cin >> ch;
            int tmp = ch - 'A';
            cin >> num;
            for(int j = 1; j <= num; ++ j)
                cin >> ch >> x, a[++ cnt] = {tmp, ch - 'A', x};
        }

        sort(a + 1 ,a + 1 + cnt);

        for(int i = 0; i <= n; ++i)
            fa[i] = i;
        int ans = 0;

        for(int i = 1; i <= cnt; ++ i){
            int x = find(a[i].x), y = find(a[i].y);
            if(x != y){
                ans += a[i].z;
                fa[x] = y;
            }
        }
        printf("%d\n", ans);
    }
}

B、POJ - 1287 Networking (模板)

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area.
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.
Input
The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i.
Output
For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>

using namespace std;

const int N = 500007;

int n, m;
struct node{
    int x, y, z;
    bool operator<(const node &t)const {
        return z < t.z;
    }
}a[N];

int t;
int cnt;
int fa[N];

int find(int x){
    if(fa[x] == x)return x;
    return fa[x] = find(fa[x]);
}
int num;
int main(){
    while(~scanf("%d%d", &n, &m) && n){
        cnt = 0;
        for(int i = 1, x, y, z; i <= m; ++ i){
            scanf("%d%d%d", &x, &y, &z);
            a[++ cnt ] = {x, y, z};
        }

        sort(a + 1 ,a + 1 + cnt);

        for(int i = 0; i <= n; ++i)
            fa[i] = i;
        int ans = 0;

        for(int i = 1; i <= cnt; ++ i){
            int x = find(a[i].x), y = find(a[i].y);
            if(x != y){
                ans += a[i].z;
                fa[x] = y;
            }
        }
        printf("%d\n", ans);
    }
}

C、POJ 2031 Building a Space Station

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>

using namespace std;

const int N = 500007;

int n, m;
struct node{
    int x, y;
    double z;
    bool operator<(const node &t)const {
        return z < t.z;
    }
}a[N];

int t;
int cnt;
int fa[N];

int find(int x){
    if(fa[x] == x)return x;
    return fa[x] = find(fa[x]);
}
int num;
double x[N], y[N], z[N], w[N];
int main(){
    while(~scanf("%d", &n) && n){
        cnt = 0;
        for(int i = 1; i <= n; ++ i){
            scanf("%lf%lf%lf%lf", &x[i], &y[i], &z[i], &w[i]);
        }
        for(int i = 1; i <= n; ++ i){
            for(int j = 1; j <= n; ++ j){
                if(i != j){
                    double dist = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]) + (z[i] - z[j]) * (z[i] - z[j]));
                    if(dist <= w[i] + w[j])a[++ cnt] = {i, j, 0};
                    else a[++ cnt] = {i, j, dist - w[i] - w[j]};
                }
            }
        }

        sort(a + 1 ,a + 1 + cnt);

        for(int i = 0; i <= n; ++ i)
            fa[i] = i;
        double ans = 0;

        for(int i = 1; i <= cnt; ++ i){
            int x = find(a[i].x), y = find(a[i].y);
            if(x != y){
                ans += a[i].z;
                fa[x] = y;
            }
        }
        printf("%f\n", ans);
    }
}

D、HDU - 1233 还是畅通工程 (最小生成树模板)

某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input 测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N (100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
Output 对每个测试用例,在1行里输出最小的公路总长度。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
typedef long long ll;
using namespace std;

const int N = 100007;

int fa[N];
int n, m;
struct node{
    int u, v, w;
    bool operator <(const node &t)const {
        return w < t.w;
    }
}e[N];

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

int main()
{
    while(~scanf("%d", &n) && n){
        int cnt = 0;
        for(int i = 1; i <= n * (n - 1) / 2 ; ++ i){
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            e[ ++ cnt ] = {x, y, z};
        }
        sort(e + 1, e + 1 + cnt);

        for(int i = 1; i <= n; ++ i)
            fa[i] = i;

        ll res = 0;

        for(int i = 1; i <= cnt; ++ i){
            int x = e[i].u, y = e[i].v;
            int nx = find(x), ny = find(y);
            if(nx != ny){
                res += e[i].w;
                fa[nx] = ny;
            }
        }
        printf("%lld\n", res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45697774/article/details/108328319