ZOJ 1586(最小生成树)Problem E: QS Network

Description:

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.

output:

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

sample input:

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

sample output :

370
如果能读懂题意应该就能做出来了,这个题的大致意思是这样的,刚上来t组输入这个都能看懂,然后接下来一行是n,就拿题中给的例子来解释
1
3
10 20 30
0 100 200
100 0 300
200 300 0
题中的n是3,接下来共有n+1行就是3+1=4行 第一行 10 20 30,代表1买路由器花费10元,2买路由器花费20元,3买路由器花费30元,然后路由器的价钱都知道了,但是只有路由器还不能把他们连起来,你需要去买网线将他们的路由器连起来才算完成任务,接下来3行就是一个矩阵,比如第一行第二个数的值是100,就代表将1和2的路由器连起来需要买网线的花销是100元,然后分析这个例子,1和2的路由器连起来需要买网线的花销是100元,买路由器的钱是10+20,1和3连起来需要买网线的花销是200元,买路由器的钱是10+30,1和2连起来同时1和3那么就相当于1,2,3都连通了,最后的最少花销就是100+200+10+20+10+30=370

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<algorithm>
#include<iostream>
using namespace std;
int v[1050000],q[1000000],f[107000];
 int sum;
struct lll
{
   int x,y,z;
}s[1060000];//这个一定要开大,要不然会报错
int found(int x)
{
    if(x!=f[x])
    {
        f[x]=found(f[x]);
    }
    return f[x];
}
int lsz(lll a,lll b)
{
    return a.z<b.z;
}
void merch1(int x,int y,int z)
 {
     int x1=found(x),y1=found(y);
     if(x1!=y1)
     {
         f[x1]=y1;
         sum+=z;
     }
     return ;
 }
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        sum=0;
        for(int a=0;a<=100000;a++)
        {
            f[a]=a;
        }
        int n,h=0;
        scanf("%d",&n);
        for(int a=1;a<=n;a++)
        {
            scanf("%d",&q[a]);
        }
        for(int a=1;a<=n;a++)
        {
            for(int b=1;b<=n;b++)
            {
                scanf("%d",&v[b]);

            }
            for(int b=1;b<=n;b++)
            {
                s[h].x=a,s[h].y=b,s[h].z=v[b]+q[a]+q[b];
        h++;
            }


        }
        sort(s,s+h,lsz);
        for(int a=0;a<h;a++)
        {
            merch1(s[a].x,s[a].y,s[a].z);
        }

        printf("%d\n",sum);


    }

    return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_44122831/article/details/88658100
qs