zoukankan      html  css  js  c++  java
  • Jungle Roads--hdu1301

    Jungle Roads

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


    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<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int per[110];
     6 struct node
     7 {
     8     char a,b;
     9     int w;
    10 }s[1000];
    11 bool cmp(node x,node y)
    12 {
    13     return x.w<y.w;
    14 }
    15 void init()
    16 {
    17     for(int i=60;i<100;i++)
    18     per[i]=i;
    19 }
    20 
    21 int find(int x)
    22 {
    23     while(x!=per[x])
    24     x=per[x];
    25     return x;
    26 }
    27 bool join (int x,int y )
    28 {
    29     int fx=find(x);
    30     int fy=find(y);
    31     if(fx!=fy)
    32     {
    33         per[fx]=fy;
    34         return true;
    35     }//判断是否成环
    36     return false;
    37 }
    38 
    39 
    40 int main()
    41 {
    42     char a,b,c,d;
    43     int e,f,g,n;
    44     while(scanf("%d",&n),n)
    45     {
    46         init();
    47         getchar();
    48         n-=1;
    49         int sum=0,k=0;
    50         while(n--)
    51         {
    52             scanf("%c%d",&a,&e);
    53             getchar();
    54             for(int i=0;i<e;i++)
    55             {
    56                 scanf("%c%d",&b,&f);
    57                 getchar();
    58                 s[k].a=a;
    59                 s[k].b=b;
    60                 s[k].w=f;
    61                 k++;
    62             }
    63         }
    64         sort(s,s+k,cmp);
    65         for(int i=0;i<k;i++)
    66         {
    67             if(join(s[i].a,s[i].b))//这里直接把字符型当成整形处理
    68             sum+=s[i].w;
    69         }
    70         printf("%d
    ",sum);
    71     }
    72     return 0;    
    73 }
     
  • 相关阅读:
    以相同的條件,把多行記錄的某些欄位串在一起
    拆分記錄
    消息推送
    Web API数据传输加密
    OData 带更新的实例,并能取得元数据格式类型
    在ASP.NET Web API中使用OData
    浏览器根对象window之screen
    浏览器从输入到输出的过程与原理五之网络通信和三次握手
    浏览器从输入到输出的过程与原理四之互联网
    浏览器从输入到输出的过程与原理三之DNS
  • 原文地址:https://www.cnblogs.com/Eric-keke/p/4722135.html
Copyright © 2011-2022 走看看