zoj 1586 QS Network 【最小生成树变形】

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586

Sunny Cup 2003 - Preliminary Round

April 20th, 12:00 - 17:00

Problem E: QS Network


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

题意:比如给你三个点a, b, c,每个点处有一个值。将这三个点连通起来,有a---->b---->c这种连接,也有a---->b, a---->c这种连接(另外b---->a, b---->c就不说了,类似),两个点相连有边权,现在要你加上节点的值,不妨开个value数组,分别存放a, b, c这三点的值。现在让你将整个图连通求代价和的最小值(代价和也包括你端点的值哦)。

思路:就kruskal了,prim今天没在状态,好吧,下面给上代码:

#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 1e3+10;
const int inf = 0x3f3f3f3f;
int pre[maxn],value[maxn];
int find(int x)
{
    if(pre[x]==x)
        return x;
    return find(pre[x]);
}
void join(int x,int y)
{
    int fx = find(x);
    int fy = find(y);
    if(fx != fy)
        pre[fx]=fy;
}
struct inp{
    int x,y,w;
}p[maxn*maxn+100];
bool cmp(inp a,inp b)
{
    return a.w<b.w;
}
int main()
{
    int t,n,cnt,ans,b;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1;i<=n;i++) //这一步我刚开始又是忘记了,汗
            pre[i] = i;
        for(int i=1;i<=n;i++)
            cin>>value[i];
        cnt = 0,ans = 0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                cin>>b;
                if(i>=j) continue;
                p[cnt].x = i;
                p[cnt].y = j;
                p[cnt++].w = b + value[i] + value[j];  //这一步是核心的处理,加上两个端点处的值
            }
        }
        sort(p,p+cnt,cmp);
        for(int i=0;i<n*(n-1)/2;i++)
        {
            if(find(p[i].x) != find(p[i].y))
            {
                join(p[i].x,p[i].y);
                ans+=p[i].w;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值