ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeup100000622问题 E: Jungle Roads

2021-07-29 13:59:03  阅读:157  来源: 互联网

标签:return int villages father ++ Codeup100000622 roads 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.

输入:

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 1 < n < 27 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.

输出:

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.

样例输入:

3
A 1 B 42
B 1 C 87
6
A 2 B 13 E 55
B 1 C 1
C 1 D 20
D 1 E 4
E 1 F 76
0

样例输出:

129
114

实现代码:

#include<cstdio>
#include<algorithm>
using namespace std;

const int maxv = 30;
const int maxe = 80;

struct edge {
    int u;
    int v;
    int dis;
} E[maxe];

bool cmp(edge a, edge b) {
    return a.dis < b.dis;
}

int father[maxv];
int findFather(int x) {
    int a = x;
    while(x != father[x]) {
        x = father[x];
    }
    while(a != father[a]) {
        int z = a;
        a = father[a];
        father[z] = x;
    }
    return x;
}

int kruskal(int n, int m) {
    int ans = 0, numEdge = 0;

    for(int i = 0; i < n; i++) {
        father[i] = i;
    }

    sort(E, E + m, cmp);
    for(int i = 0; i < m; i++) {
        int faU = findFather(E[i].u);
        int faV = findFather(E[i].v);
        if(faU != faV) {
            father[faU] = faV;
            ans += E[i].dis;
            numEdge++;
            if(numEdge == n - 1) {
                break;
            }
        }
    }

    if(numEdge != n - 1) {
        return -1;
    } else {
        return ans;
    }
}

int main() {
    int n;
    char c1, c2;
    int w;
    while(scanf("%d", &n) != EOF) {
        if(n == 0) {
            break;
        }

        int num;
        int m = 0;
        for(int i = 0; i < n - 1; i++) {
            getchar();
            scanf("%c", &c1);
            scanf("%d", &num);
            for(int j = 0; j < num; j++) {
                getchar();
                scanf("%c %d", &c2, &w);
                E[m].u = c1 - 'A';
                E[m].v = c2 - 'A';
                E[m].dis = w;
                m++;
            }
        }

        printf("%d\n", kruskal(n, m));
    }
    return 0;
}

标签:return,int,villages,father,++,Codeup100000622,roads,Jungle,Roads
来源: https://blog.csdn.net/Lerbronjames/article/details/119061078

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有