zoukankan      html  css  js  c++  java
  • ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目:

    In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunicate with each other via networks. If two QS want to get connected, they need to buy two network adapters (one for each QS) and a segment of network cable. Please be advised that ONE NETWORK ADAPTER CAN ONLY BE USED IN A SINGLE CONNECTION.(ie. if a QS want to setup four connections, it needs to buy four adapters). In the procedure of communication, a QS broadcasts its message to all the QS it is connected with, the group of QS who receive the message broadcast the message to all the QS they connected with, the procedure repeats until all the QS's have received the message.

    A sample is shown below:


    A sample QS network, and QS A want to send a message.

    Step 1. QS A sends message to QS B and QS C;

    Step 2. QS B sends message to QS A ; QS C sends message to QS A and QS D;

    Step 3. the procedure terminates because all the QS received the message.

    Each QS has its favorate brand of network adapters and always buys the brand in all of its connections. Also the distance between QS vary. Given the price of each QS's favorate brand of network adapters and the price of cable between each pair of QS, your task is to write a program to determine the minimum cost to setup a QS network.

    Input

    The 1st line of the input contains an integer t which indicates the number of data sets.

    From the second line there are t data sets.

    In a single data set,the 1st line contains an interger n which indicates the number of QS.

    The 2nd line contains n integers, indicating the price of each QS's favorate network adapter.

    In the 3rd line to the n+2th line contain a matrix indicating the price of cable between ecah pair of QS.

    Constrains:

    all the integers in the input are non-negative and not more than 1000.


    <b< dd="">

    Output

    for each data set,output the minimum cost in a line. NO extra empty lines needed.


    <b< dd="">

    Sample Input

    1
    3
    10 20 30
    0 100 200
    100 0 300
    200 300 0


    <b< dd="">

    Sample Output

    370

    题意描述:
    题目描述的很有意思,不过有点绕。简单来说就是每两个人要建立联系,需要分别购买一个自己最喜欢的接收器,和共同购买一个共用转换器。

    先输入结点的个数N,再输入N个数,表示结点最喜欢的接收器的价格,最后输入N*N的矩阵表示i和j建立联系的转换器价格。

    解题思路:

    该开始的时候想用Prim算法,在选择一条边可以建造的时候,将两个结点都计一下数,最后乘以每个结点喜欢的接收器的价格再加上最小生成树的值。但是这种解法是有漏洞的,因为如果接收器是后来加的,它的费用是没有考虑进最省路径的,所以有可能,接收器的价格很贵,虽然转换器是最省的但加上接收器的话就不是最省路径了。综上所述,选择Kruskal算法,不同的是将接收器的价格事先计算在这条边上,那么就跟模板题一样了。

    代码实现:

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 using namespace std;
     5 struct edge
     6 {
     7     int u,v,w;
     8 };
     9 int cmp(struct edge x,struct edge y)
    10 {
    11     return x.w<y.w;
    12 }
    13 struct edge e[1000*1000];
    14 int merge(int v,int u);
    15 int getf(int v);
    16 int map[1010][1010],f[1010];
    17 int main()
    18 {
    19     int t,n,a[1010],i,j,k,c,sum;
    20     scanf("%d",&t);
    21     while(t--)
    22     {
    23         scanf("%d",&n);
    24         for(i=1;i<=n;i++)
    25             scanf("%d",&a[i]);
    26         for(i=1;i<=n;i++)
    27             for(j=1;j<=n;j++)
    28                 scanf("%d",&map[i][j]);
    29 
    30         k=1;
    31         for(i=1;i<=n;i++)
    32         {
    33             for(j=i+1;j<=n;j++)
    34             {
    35                 e[k].u=i;
    36                 e[k].v=j;
    37                 e[k++].w=map[i][j]+a[i]+a[j];
    38             }
    39         }    
    40         sort(e+1,e+k,cmp);
    41         for(i=1;i<=n;i++)
    42             f[i]=i;
    43         sum=0;
    44         c=0;
    45         for(i=1;i<k;i++)//边的数目 
    46         {
    47             if( merge(e[i].u,e[i].v) )
    48             {
    49                 c++;
    50                 sum += e[i].w; 
    51             }
    52             if( c == n-1)
    53                 break;
    54         }
    55         printf("%d
    ",sum);
    56     }
    57     return 0;
    58 }
    59 
    60 int merge(int v,int u)
    61 {
    62     int t1,t2;
    63     t1=getf(v);
    64     t2=getf(u);
    65     if(t1 != t2)
    66     {
    67         f[t2]=t1;
    68         return 1;
    69     }
    70     return 0;
    71 }
    72 int getf(int v)
    73 {
    74     if(f[v]==v)
    75     return v;
    76     else
    77     {
    78         f[v]=getf(f[v]);
    79         return f[v];
    80     }
    81 }

    易错分析:

    1、注意遍历的时候遍历的是边的条数(代码功力还是要加强的)

  • 相关阅读:
    Kubernetes之Ingress-Nginx
    Prometheus之AWS-EC2自动发现
    Prometheus之kubernetes-sd自动发现
    ORACLE 遇到ORA 03113 数据库连接卡住
    ORACLE 遇到ORA-31693 ORA-31617 ORA-19505 ORA-27037
    Oracle 计划任务批量清理临时表实例
    GO 基础
    CentOS 7.3安装完整开发环境
    3.6 String 与 切片&str的区别
    ubantu上编辑windows程序
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/7277957.html
Copyright © 2011-2022 走看看