zoj 1586 QS Network【kruskal】

QSNetwork最小成本计算
本文介绍了一个关于QS生物网络连接的问题,在银河系cgb的w-503行星上,智能生物QS通过特定的品牌适配器和电缆建立网络连接。文章提供了一种使用Kruskal算法来确定网络构建的最小总成本的方法。

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



Author: JIANG, Jiefeng

Source: Zhejiang University Local Contest 2003, Preliminary




//2605331 	2011-07-29 16:46:54 	Accepted 	1586 	C++ 	160 	6048 	ylwh!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <algorithm>
#define N 1001
#define LEN (N*N-N) / 2 + 1
using namespace std;
struct edge
{
	int x, y, cost;
}map[LEN];
int n, len,  root[N];

bool cmp(struct edge a, struct edge b)
{
	return a.cost < b.cost;
}

void input()
{
	int price[N], i, j, temp;
	int count=1;
	scanf("%d", &n);
	len = (n*n - n) / 2;
	for(i=1; i<=n; i++)
		scanf("%d", &price[i]);
	for(i=1; i<=n; i++)
		for(j=1; j<=n; j++)
		{
			scanf("%d", &temp);
			if(i<j)
			{
				map[count].x = i;
				map[count].y = j;
				map[count].cost = price[i] + price[j] + temp;
				count++;
			}
		}
}

int find_set(int x)
{
	while(x != root[x])
		x = root[x];
	return x;
}

void KRUSKAL()
{
	int i, a, b, sum=0;
	for(i=1; i<=n; i++)
		root[i] = i;
	sort(map+1, map+len+1, cmp);

	for(i=1; i<=len; i++)
	{
		a = find_set( map[i].x );
		b = find_set( map[i].y );
		if(a != b)
		{
			root[b] = a;
			sum += map[i].cost;
		}
	}
	printf("%d\n", sum);		
}
	
int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
	{
		input();
		KRUSKAL();
	}
    return 0;
}



### 关于中国下载使用 #### 工具简介 中国是一款开源、跨平台的WebShell管理工具,专为渗透测试人员设计。它能够满足安全研究人员和网站管理员的需求,提供了一种高效的方式管理和操作被植入目标服务器中的WebShell[^1]。 #### 下载方式 可以通过访问其官方GitHub仓库获取最新版本的中国。具体链接可以在以下位置找到:`antSword/README_CN.md at master · AntSwordProject/antSword · GitHub`[^3]。该页面提供了详细的安装说明和技术支持文档。 #### 安装教程 为了成功部署并运行中国,需按照以下技术细节执行: - **环境准备** 需要确保本地计算机已安装Node.js及其相关依赖项。这是由于中国基于Electron框架构建而成,而后者依赖于Node.js生态系统。 - **克隆项目代码** 执行如下命令以从远程存储库拉取源码到本地机器: ```bash git clone https://github.com/AntSwordProject/antSword.git ``` 随后进入目录完成进一步配置工作。 - **编译打包过程** 利用npm包管理器来处理必要的前置条件以及最终的应用程序封装步骤: ```bash cd antSword npm install && npm run build ``` #### 基本功能演示 当一切设置完毕之后,启动应用程序即可开始体验核心特性之一——连接远端存在漏洞的服务实例。例如,在模拟环境中创建一个简单的文件上传接口作为练习对象;接着借助此工具向指定URL提交恶意脚本(如PHP类型的后门文件)。假设目标路径结构类似于`upload/uploads/shell.php`,那么实际填写时应调整相对关系表达式为`.//upload/uploads/shell.php`形式以便正确解析定位资源位置[^4]。 #### 实际应用场景案例分析 某位作者在其文章中分享了如何运用“中国”实现对特定类型缺陷的有效验证方法论。他特意开发了一个简化版模型用来展示整个流程逻辑链条是如何紧密衔接起来形成闭环解决方案的过程描述。这不仅有助于加深学习者对于理论知识点的理解程度,同时也提高了实践动手能力水平[^2]。 ### 注意事项 尽管上述内容展示了强大的功能性优势所在之处,但在真实世界里开展任何未经授权擅自入侵他人系统的活动均属违法行为,请务必严格遵守法律法规界限范围内合理合法地应用这些技能知识!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值