最小生成树-ZOJ1586QS Network

ZOJ1586QS Network

Problem 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

题意

求把n个QS直接或间接的连接起来的最小花费
链接每两个QS需要花费电缆的钱
和这两个QS每个需要一个网卡
输入是
数据组数
QS个数
每个QS那里的网卡的价钱
邻接矩阵-QS之间的电缆的价钱

思路

刚开始的思路是
用kruskal写
每条连起来的边的两端点需要的网卡++
然后wa了以后想到这样子不对啊
应该是把两个端点网卡的价钱和这条边断点的价钱当作整体来做
于是

AC代码

#include<cstdio>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

#define PI         acos(-1)
#define inf        0x3f3f3f3f
#define EPS        1e-6
#define mem(a, b)  memset(a, b, sizeof(a))
#define ll long long
#define mian main
int fa[1000000 + 10];
int pri[10000010];
struct node
{
    int u, v;
    int dis;
} mp[1000000 + 10];

int ifind(int x)
{
    int an = x;
    while(an != fa[an])
        an = fa[an];
    int t = x;
    while(fa[x] != an)
    {
        x = fa[x];
        fa[t] = an;
        t = x;
    }
    return an;

}
int ijoin(int a, int b)
{
    a = ifind(a);
    b = ifind(b);
    if(a != b)
    {
        fa[a] = b;
        return 1;
    }
    return 0;
}
bool cmp(node a, node b)
{
    return a.dis < b.dis;
}
int main()
{
    int t;
    scanf("%d", &t);
    int n, q;
    while(t--)
    {
        scanf("%d", &n);

        for(int i = 1; i <= n; i++)
        {
            fa[i] = i;
        }
        for(int i = 1; i <= n; i++)
            scanf("%d", &pri[i]);
        int no = 1;
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                int tmp;
                scanf("%d", &tmp);
                mp[no].dis = tmp + pri[i] + pri[j];
                mp[no].u = i;
                mp[no].v = j;
                no++;
            }

        }
        sort(mp + 1, mp + 1 + no, cmp);
        int sum = 0;
        for(int i = 1; i <= no; i++)
        {
            int tmp = ijoin(mp[i].u, mp[i].v);
            sum += mp[i].dis * tmp;
            //if(tmp)
            //{
              //  wk[mp[i].u]++;
                //wk[mp[i].v]++;
            //}
             //cout<<"i "<<mp[i].u<<"j "<<mp[i].v<<"dis "<<mp[i].dis<<' '<<ijoin(mp[i].u, mp[i].v)<<endl;;

        }
        //cout<<"sump"<<sum<<endl;
        //for(int i = 1; i <= n; i++)
            //sum += wk[i] * pri[i];
        printf("%d\n", sum);

    }

    return 0;
}


猜你喜欢

转载自blog.csdn.net/wuswi0412/article/details/81360046