zoukankan      html  css  js  c++  java
  • POJ1251 Jungle Roads(最小生成树模板题)

    描述

    传送门:我是传送门

    t

    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

    思路

    最小生成树的模板题
    只需要对输入进行处理

    但是仍旧踩了坑

    碰到数据范围比较小的就直接开大数组,避免RE

    涉及到字符的输入用cin比较保险,避免特殊情况用scanf出错

    代码

    Kruskal算法

     1 /*
     2  * ===============================================
     3  *
     4  *       Filename:  A.cpp
     5  *
     6  *           Link:  http://poj.org/problem?id=1251
     7  *
     8  *        Version:  1.0
     9  *        Created:  2018/09/26 11时47分40秒
    10  *       Revision:  none
    11  *       Compiler:  g++
    12  *
    13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
    14  *   Organization:  QLU_浪在ACM
    15  *
    16  * ===============================================
    17  */
    18 #include <stdio.h>
    19 #include <algorithm>
    20 #include <stdlib.h>
    21 #include <iostream>
    22 using namespace std;
    23 #define clr(a, x) memset(a, x, sizeof(a))
    24 #define rep(i,a,n) for(int i=a;i<=n;i++)
    25 #define pre(i,a,n) for(int i=n;i>=a;i--)
    26 #define ll long long
    27 #define max3(a,b,c) fmax(a,fmax(b,c))
    28 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    29 const double eps = 1e-6;
    30 const int INF = 0x3f3f3f3f;
    31 const int mod = 1e9 + 7;
    32 struct node{int u,v,w;}e[105];
    33 int f[105],n,m,ans,tmp,eu,ev,cnt,temp;
    34 char start,en;
    35 inline bool cmp(node a,node b){return a.w<b.w;};
    36 inline int find(int x)
    37 {
    38     while(x != f[x])
    39         x = f[x] = f[f[x]];
    40     return x;
    41 };
    42 int kruskal()
    43 {
    44     cnt = 0;
    45     ans = 0;
    46     for(int i = 0;i <= n;i++)   f[i] = i;
    47     sort(e,e+m,cmp);
    48     for(int i = 0;i < m;i++)
    49     {
    50         eu = find(e[i].u);  ev = find(e[i].v);
    51         if(eu == ev)    continue;
    52         ans += e[i].w;
    53         f[ev] = eu;
    54         if(++cnt == n-1)
    55             break;
    56     }
    57     return ans;
    58 }
    59 
    60 
    61 int main()
    62 {
    63     ios
    64 #ifdef ONLINE_JUDGE 
    65 #else 
    66         freopen("in.txt","r",stdin);
    67     // freopen("out.txt","w",stdout); 
    68 #endif
    69     // scanf("%d",&n);
    70     while(cin >> n)
    71     {
    72         if(n == 0)
    73             break;
    74         m = 0;
    75         for(int i = 0;i < n-1;i++)
    76         {
    77             // scanf("%c %d",&start,&tmp);
    78             cin >> start >> tmp;
    79             for(int j = 0;j < tmp;j++,m++)
    80             {
    81                 // scanf("%c %d",&en,&temp);
    82                 cin >> en >> temp;
    83                 eu = start-'A',ev = en-'A';
    84                 e[m].u = eu,e[m].v = ev,e[m].w = temp;
    85             }
    86         }
    87         // printf("%d
    ",kruskal());
    88         cout << kruskal() << endl;
    89     }
    90     fclose(stdin);
    91     // fclose(stdout);
    92     return 0;
    93 }

    Prime算法

      1 /*
      2  * ================================================
      3  *
      4  *       Filename:  A-2.cpp
      5  *
      6  *           Link:  http://poj.org/problem?id=1251
      7  *
      8  *        Version:  1.0
      9  *        Created:  2018/09/26 13时21分21秒
     10  *       Revision:  none
     11  *       Compiler:  g++
     12  *
     13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
     14  *   Organization:  QLU_浪在ACM
     15  *
     16  * ================================================
     17  */
     18 #include <stdio.h>
     19 #include <algorithm>
     20 #include <stdlib.h>
     21 #include <iostream>
     22 #include <string.h>
     23 using namespace std;
     24 #define clr(a, x) memset(a, x, sizeof(a))
     25 #define rep(i,a,n) for(int i=a;i<=n;i++)
     26 #define pre(i,a,n) for(int i=n;i>=a;i--)
     27 #define ll long long
     28 #define max3(a,b,c) fmax(a,fmax(b,c))
     29 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     30 const double eps = 1e-6;
     31 const int INF = 0x3f3f3f3f;
     32 const int mod = 1e9 + 7;
     33 const int N = 110;
     34 int cost[N][N];
     35 int mmin,n,m,tot,ans,now,eu,ev;
     36 int v2[N];
     37 bool v1[N];
     38 
     39 void init()
     40 {
     41     clr(v1,0);
     42     for(int i = 0;i <= n;i++)
     43         for(int j = 0;j <= n;j++)
     44         {
     45             if(i == j)  cost[i][j] = 0;
     46             else    cost[i][j] = INF;
     47         }
     48 }
     49 
     50 int prim()
     51 {
     52     // 找到与1节点相连的边进行标记
     53     for(int i = 1;i <= n;i++)
     54     {
     55         v2[i] = cost[1][i];
     56     }
     57     v1[1] = 1;
     58     tot = 1;
     59     ans = 0;
     60     while(tot < n)
     61     {
     62         mmin = INF;
     63         tot++;
     64         for(int i = 1;i <= n;i++)
     65         {
     66             if(!v1[i] && v2[i] < mmin)
     67             {
     68                 mmin = v2[i];
     69                 now = i;
     70             }
     71         }
     72         ans += mmin;
     73         for(int i = 1;i <= n;i++)
     74         {
     75             if(v2[i] > cost[now][i] && !v1[i])
     76                 v2[i] = cost[now][i];
     77         }
     78         v1[now] = 1;
     79     }
     80     return ans;
     81 }
     82 
     83 int main()
     84 {
     85     ios
     86 #ifdef ONLINE_JUDGE 
     87 #else 
     88         freopen("in.txt","r",stdin);
     89     // freopen("out.txt","w",stdout); 
     90 #endif
     91     int tmp,temp;
     92     char s,e;
     93     while(cin >> n)
     94     {
     95         if(n == 0)  break;
     96         init();
     97         for(int i = 1;i < n;i++)
     98         {
     99             cin >> s >> tmp;
    100             eu = s-'A'+1;
    101             while(tmp--)
    102             {
    103                 cin >> e >> temp;
    104                 ev = e-'A'+1;
    105                 if(cost[eu][ev] > temp)
    106                 {
    107                     cost[eu][ev] = cost[ev][eu] = temp;
    108                 }
    109             }
    110         }
    111         printf("%d
    ",prim());
    112     }
    113     fclose(stdin);
    114     // fclose(stdout);
    115     return 0;
    116 }
  • 相关阅读:
    Repeater 双向排序
    将具有固定格式的text 类型中的数据分离出来的一种方法
    ASP.NET 快速构建应用程序页面主框架
    2分分页处理存储过程通用存储过程
    C#3.0之匿名类型
    常用的js收集
    用CSS实现DataGird滚动而表头不动
    Lucene.Net 创建索引和检索
    Lucene.net 实现全文搜索
    SQL 中操作日期的几个函数
  • 原文地址:https://www.cnblogs.com/duny31030/p/14305065.html
Copyright © 2011-2022 走看看