题目链接

QS Network


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

题意:QS之间通过网络进行通讯,如果两个QS需要通讯,他们需要买两个网络适配器,每个QS一个,以及一段网线,每个是适配器只用于单一通信中,也就是说如果一个QS想建立4个通信,那么需要买4个适配器。在通信中,一个QS向所有和他连接的QS发送消息,接收到消息的QS又向它连接着的QS发送,直到所有QS都接到消息

分析:最小生成树

AC代码:

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
const int N=1010;
const int inf=0x3f3f3f3f3f;
int n;
int s[N],dis[N],e[N][N];
bool vis[N];
int Prim(int st){int ans=0;memset(vis,false, sizeof((vis)));vis[st]=true;for(int i=1;i<=n;i++){dis[i]=e[st][i];}int minn,u;for(int i=1;i<n;i++){minn=inf;for(int j=1;j<=n;j++){if(minn>dis[j]&&!vis[j]){minn=dis[j];u=j;}}if(minn==inf) break;vis[u]=true;ans+=minn;for(int j=1;j<=n;j++){if(dis[j]>e[u][j]&&!vis[j]) dis[j]=e[u][j];}}return ans;
}
void init(){for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j) e[i][j]=0;else e[i][j]=inf;}}
}
int main(){int t;scanf("%d",&t);while(t--){scanf("%d",&n);init();for(int i=1;i<=n;i++){scanf("%d",&s[i]);}for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){scanf("%d",&e[i][j]);if(i==j) e[i][j]=0;else e[i][j]+=s[i]+s[j];}}int ans=Prim(1);printf("%d\n",ans);}return 0;
}

View Code

转载于:https://www.cnblogs.com/kun-/p/9708253.html

ZOJ 1586 QS Network相关推荐

  1. kuangbin 最小生成树专题 - ZOJ - 1586 QS Network (朴素 Prim算法 模板题)

    kuangbin 最小生成树专题 - ZOJ - 1586 QS Network (朴素 Prim算法 模板题) 总题单 week 3 [kuangbin带你飞] 题单 最小生成树 + 线段树 Cli ...

  2. zoj 1586 QSNetwork 最小生成树 Prim Kruskal

    题意: 一个图,不止边有权值,点也有权值 Input: 输入的第一行包含一个整数t,它指示数据集的数量. 第二行中有T个数据集. 在单个数据集中,第一行包含一个整数n,表示qs的数目. 第2行包含n个 ...

  3. 2014牡丹江 现场赛 F zoj 3824 Fiber-optic Network

    首先赞一下题目, 好题 题意: Marjar University has decided to upgrade the infrastructure of school intranet by us ...

  4. kuangbin带你飞专题合集

    题目列表 [kuangbin带你飞]专题一 简单搜索 [kuangbin带你飞]专题二 搜索进阶 [kuangbin带你飞]专题三 Dancing Links [kuangbin带你飞]专题四 最短路 ...

  5. 算法学习经典例题整理

    陆续会对本篇博客进行更新! 搜索:https://vjudge.net/contest/292597 区间DP:https://vjudge.net/contest/293892 树状背包:https ...

  6. kuangbin带你飞 专题1-23 题单

    kuangbin大神,对于打过ACM比赛的ACMer,无人不知无人不晓. 在此,附上vjudge平台上一位大神整理的[kuangbin带你飞]专题目录链接. [kuangbin带你飞专题目录1-23] ...

  7. 老鱼的-kuangbin专题题解

    kuangbin专题问题一览 专题一 简单搜索 POJ 1321 棋盘问题 POJ 2251 Dungeon Master POJ 3278 Catch That Cow POJ 3279 Flipt ...

  8. c语言大小写字母互换1005,1005 Jugs,1005jugs

    1005 Jugs,1005jugs 辗转相减,新手入门题.两个容量的灌水题,无所谓最优解. 1 #include 2 3 intmain(){4 intA,B,T,sA,sB;5 while(sca ...

  9. php+rsa生成签名sign,PHP 做 RSA 签名 生成订单(支付宝例子)

    /组合签名 $a=time(); $b=substr($a, 1); //生成随机订单号 $orderid= $b.mt_rand(10000,99999); //合作身份者id,以2088开头的16 ...

最新文章

  1. usaco Beef McNuggets
  2. C# WinForm获取当前路径汇总
  3. Python自然语言处理工具包推荐
  4. java语言基础及集合基础大总结
  5. 文献记录(part57)--半监督学习方法
  6. 无线网络拓扑结构简析
  7. 【八】有验证码登录配置:通过 Cookie 跳过验证码登录接口
  8. oracle 网络访问配置tnsnames.ora文件的路径
  9. 后缀数组模板 hdu1403
  10. 11kw星三角启动延时几秒_带有延时启动的星角接启动方法
  11. 解决Tomcat控制台乱码(图文)
  12. 数据库习题(填空题三)
  13. H5手机转盘抽奖活动游戏页面源码
  14. 完美解析Opendrive地图格式数据
  15. 爱情保卫战 - 爱情保鲜剂 语录收集
  16. 东芝服务器报错误代码维修,实战维修 东芝复印机故障维修详解
  17. 千兆网线与千兆水晶头接法
  18. Android 免root 备份数据,教你安卓手机免Root恢复手机数据的三种方法
  19. Cesium基础-表面面积量算(依地形量算、依模型表面量算)
  20. python 百度智能完善拆分识别收货人地址

热门文章

  1. php 获取请求设备,php – 如何获取设备令牌
  2. python模块使用_一文让你学会所有的python模块使用
  3. 【lua学习】2.数据类型
  4. 计算机基础教育学,计算机基础教育教学改革与创新
  5. python监控服务器信息,Python监控服务器实现邮件微信报警
  6. java爬虫jsoup_Java爬虫之利用Jsoup自制简单的搜索引擎
  7. c语言程序求一一组数平均值,编写求一组整数的和与平均值的程序
  8. pat 乙级 1017 A除以B(C++)
  9. 光纤收发器的选购原则介绍
  10. [渝粤教育] 浙江工商大学 大学英语(4)(韩颖) 参考 资料