zoukankan      html  css  js  c++  java
  • Jungle Roads

    Jungle Roads

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 5430    Accepted Submission(s): 3918


    Problem Description

    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.
     

    Sample Input
    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
     

    Sample Output
    216 30
     

     题意:就是让找出所有村庄相通的情况下所需最小维护费用;克鲁斯卡尔;

    代码:

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 struct Node{
     5     int s,e,d;
     6 };
     7 Node dt[80];
     8 int pre[30];
     9 int money;
    10 int find(int x){
    11     return pre[x]= x==pre[x]?x:find(pre[x]);
    12 }
    13 int cmp(const void *a,const void *b){
    14     if((*(Node *)a).d<(*(Node *)b).d)return -1;
    15     else return 1;
    16 }
    17 void merge(Node a){
    18     int f1,f2;
    19     if(pre[a.s]==-1)pre[a.s]=a.s;
    20     if(pre[a.e]==-1)pre[a.e]=a.e;
    21     f1=find(a.s);f2=find(a.e);
    22     if(f1!=f2){
    23         pre[f1]=f2;
    24         money+=a.d;
    25     }
    26 }
    27 void initial(){
    28     memset(pre,-1,sizeof(pre));
    29     memset(dt,0,sizeof(dt));
    30     money=0;
    31 }
    32 int main(){
    33         int N,M,temp;char a[5],b[5];
    34         while(~scanf("%d",&N),N){int k=0;
    35         initial();
    36             for(int i=0;i<N-1;i++){
    37                 scanf("%s%d",a,&M);
    38                 while(M--){
    39                     scanf("%s%d",b,&temp);
    40                     dt[k].s=a[0]-'A';dt[k].e=b[0]-'A';
    41                     dt[k].d=temp;
    42                     k++;
    43                 }
    44             }
    45             qsort(dt,k,sizeof(dt[0]),cmp);
    46             for(int i=0;i<k;i++){
    47                 merge(dt[i]);
    48             }
    49             printf("%d
    ",money);
    50         }
    51 return 0;
    52 }
  • 相关阅读:
    drf 之 JWT认证 什么是集群以及分布式 什么是正向代理,什么是反向代理
    drf 之自定制过滤器 分页器(三种)如何使用(重点) 全局异常 封装Response对象 自动生成接口文档
    课堂练习之“寻找最长单词链”
    《人月神话》读书笔记(三)
    用户体验
    第十四周进度报告
    课堂练习之“寻找水王”
    《人月神话》读书笔记(二)
    第二阶段冲刺(十)
    第二阶段冲刺(九)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4722087.html
Copyright © 2011-2022 走看看