前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >九度OJ——1154Jungle Roads

九度OJ——1154Jungle Roads

作者头像
AI那点小事
发布2020-04-18 19:50:52
2330
发布2020-04-18 19:50:52
举报
文章被收录于专栏:AI那点小事AI那点小事

题目描述:

这里写图片描述
这里写图片描述

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, 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. 样例输入: 9 A 2 B 12 I 25 B 3 C 10 H 40 I 8 C 2 D 18 G 55 D 1 E 44 E 2 F 60 G 38 F 0 G 1 H 35 H 1 I 35 3 A 2 B 10 C 40 B 1 C 20 0 样例输出: 216 30


思路:Kruskal算法的应用。算法思路详见:最小生成树算法(下)——Kruskal(克鲁斯卡尔)算法 AC代码:

代码语言:javascript
复制
#include <iostream>
#include <queue>
using namespace std;

typedef struct node{
    int start;
    int end;
    int len;
}Edge;

struct cmp{
    bool operator() (Edge e1,Edge e2){
        return e1.len>e2.len;   
    }
};

 const int MAX = 5000;
 int data[MAX];
 int N,start,end,len,n,sum;
 char ch;
 priority_queue<Edge,vector<Edge>,cmp> Q;

int Find(int root)
{
    if(data[root] < 0){
        return root;
    }
    return data[root] = Find(data[root]);
}

void Union(int root1 ,int root2)
{
    root1 = Find(root1);
    root2 = Find(root2);
    if(root1 == root2){
        return;
    }else if(root1 < root2){
        data[root1] += data[root2];
        data[root2] = root1;
    }else{
        data[root2] += data[root1];
        data[root1] = root2;
    }
    N--;
}

int Kruskal()
{
    int sum = 0;
    while(!Q.empty()){
        Edge e = Q.top();
        Q.pop();
        int root1 = e.start;
        int root2 = e.end;
        if(Find(root1) != Find(root2)){
            sum += e.len;
            Union(root1,root2);
        }
        if(N == 1){
            break;
        }
    }
    return sum;
 } 

int main()
{
    while(cin>>N){
        if(N == 0){
            break;
        }
        while(!Q.empty()){
            Q.pop();
        }
        for(int i = 0 ; i < MAX ; i++){
            data[i] = -1;
        } 
        for(int i = 1 ; i < N ; i++){
            cin>>ch>>n;
            start = ch-'A'+1;
            if(n > 0){
                for(int j = 0 ; j < n ; j++){
                    cin>>ch>>len;
                    end = ch-'A'+1;
                    Edge e;
                    e.start = start;
                    e.end = end;
                    e.len = len;
                    Q.push(e);
                }   
            }
        }
        sum = Kruskal();
        cout<<sum<<endl;
    }

    return 0;
}

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档