ZOJ 1586 QS Network

题目链接

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352689.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

javaone_JavaOne 2012:101种改进Java的方法-开发人员参与为何如此重要

javaoneBruno Souza &#xff0c; Martijn Verburg和Heather Vancura在希尔顿酒店的美国大陆宴会厅4中展示了“ 101种改善Java的方法&#xff1a;开发人员为何如此重要”。 他们将其分为自己最熟悉的领域。 SouJava的创始人兼协调员 Souza谈到了通过用户组的更大参与。 Verberg…

谷歌浏览器flash_谷歌浏览器不支持Flash Player的问题

更新2020.6.10&#xff0c;这个答案更新过方式1&#xff1a;老版谷歌chrome浏览器里输入&#xff1a;chrome://flags/#run-all-flash-in-allow-mode进行设定。方式2&#xff1a;新版谷歌68&#xff0c;69以上版本&#xff0c;chrome浏览器操作方式&#xff1a;新版不再允许保存…

深入理解python之self

首先明确的是self只有在类的方法中才会有&#xff0c;独立的函数或方法是不必带有self的。self在定义类的方法时是必须有的&#xff0c;虽然在调用时不必传入相应的参数。 self名称不是必须的&#xff0c;在python中self不是关键词&#xff0c;你可以定义成a或b或其它名字都可以…

Oracle12c异常关闭后启动PDBORCL(ORA-01033)

这个问题已经困扰了我好几天找解决方案&#xff0c;终于找到&#xff1a; 由于Oracle12c的特殊性&#xff0c;但许多用户并不想在创建用户时前面要加"C##" 那么就要创建PDBORCL数据库&#xff0c;来与Oracle以前的版本保持一致(如Oracle 11g) 可能由于断电或者异常关…

带有Spring Cloud Microservices的JSON Web令牌

在Keyhole&#xff0c;我们已经发布了几个有关微服务的博客 。 我们已经讨论了微服务环境中使用的架构模式&#xff0c;例如服务发现和断路器 。 我们甚至在平台和工具上发布了博客&#xff0c;例如最近关于Service Fabric的博客 。 我们已经介绍过的架构的重要组成部分是围绕…

STC用PCA测量脉宽_教你测量玉手镯圈号及如何轻松快速摘戴玉手镯?

一、如何测量玉手镯的圈号&#xff1f;测量和田玉手镯的圈号并不复杂&#xff0c;自己在家就能轻松搞定哦&#xff01;共有两种方法可选。方法一&#xff1a;游标卡尺法所需工具&#xff1a;游标卡尺具体方法&#xff1a;如照片所示&#xff0c;使用游标卡尺测量手掌最宽处(大拇…

Python中的函数(一)

接触过C语言的朋友对函数这个词肯定非常熟悉&#xff0c;无论在哪门编程语言当中&#xff0c;函数&#xff08;当然在某些语言里称作方法&#xff0c;意义是相同的&#xff09;都扮演着至关重要的角色。今天就来了解一下Python中的函数用法。 一.函数的定义 在某些编程语言当中…

Shell基础命令

它又是一种程序设计语言。作为命令语言&#xff0c;它交互式解释和执行用户输入的命令或者自动地解释和执行预先设定好的一连串的命令&#xff1b;作为程序设计语言&#xff0c;它定义了各种变量和参数&#xff0c;并提供了许多在高级语言中才具有的控制结构&#xff0c;包括循…

电脑报警5声_电脑故障怎么判断 常见电脑故障诊断方法介绍【详解】

在电脑使用的过程&#xff0c;出现一些电脑故障是在所难免的&#xff0c;很多小伙伴对一些常见电脑故障诊断的方法不是很了解&#xff0c;不知道自己电脑出现的这些故障 究竟是什么原因造成的 。关于软件故障有很多种可能性&#xff0c;一般都是比较容易解决的&#xff0c;今天…

洛谷P3857 [TJOI2008]彩灯(线性基)

传送门 线性基裸题 直接把所有的状态都带进去建一个线性基 然后答案就是$2^{cnt}$&#xff08;$cnt$代表线性基里数的个数&#xff09; 1 //minamoto2 #include<cstdio>3 #include<cstring>4 #define ll long long5 const int N55;6 ll b[N],a[N];int n,m,cnt;char…

sort函数

sort函数:#include <algorithm>,默认从小到大&#xff0c;如果降序可写第三方函数进行排序&#xff0c;EXP:sort(array,arrayn,cmp) 1.普通排序,升序 01#include <iostream> 02#include <algorithm> 03using namespace std; 04int main() 05{ 06 int a[10]{…

javaone_JavaOne 2012:非阻塞数据结构如何工作?

javaone当我查看今天的日程安排时&#xff0c;我感到有些惊讶&#xff0c;并指出我目前计划今天参加的所有会议都在希尔顿举行。 当我意识到JavaOne演示文稿中大约有一半是在希尔顿酒店中并且似乎按路线大致定位时&#xff0c;这变得有些不足为奇了。 Tobias Lindaaker &#x…

台式电脑键盘字母乱了_电脑键盘上的一个不起眼的按键,特别实用却被粗心的人忽略...

笔记本电脑与台式电脑的键盘有一个小小的区别。笔记本电脑没有输入数字的小键盘&#xff0c;而台式电脑键盘有&#xff1b;笔记本电脑键盘有一个Fn键&#xff0c;而台式电脑键盘没有。正是笔记本电脑键盘有了这个Fn键&#xff0c;为我们使用者提供了许多特别实用的功能&#xf…

ubuntu下crontab启动,重启,关闭命令

启动&#xff1a;/etc/init.d/cron start ( service cron start )重启&#xff1a;/etc/init.d/cron restart ( service cron restart )关闭&#xff1a;/etc/init.d/cron stop ( service cron stop )转载于:https://www.cnblogs.com/yu-yuan/p/9722202.html

NET Framework 4.0 安装失败:安装时发生严重错误

NET Framework 4.0 安装失败&#xff1a;安装时发生严重错误 看日志&#xff0c;好像是缺少系统组件。不知道你的系统是怎么装的&#xff0c;有耐心的话&#xff0c;请用系统盘执行Windows的默认方式安装。即常说的硬装&#xff0c;不要用网上常用的ghost方式安装。执行完硬装后…

C语言文件操作 fopen, fclose, mkdir(打开关闭文件,建文件夹,判断文件是否存在可读或可写)

1.建文件夹 int _mkdir(const char *path,mode_t mode); 函数名: _mkdir    功 能: 建立一个目录    用 法: int _mkdir( const char *dirname );    头文件库&#xff1a;direct.h    返回值&#xff1a;创建一个目录&#xff0c;若成功则返回0&#xff0c;否则返回-1…

差分进化算法_OPTIMUS软件功能特性介绍【全局优化算法模块】

导读&#xff1a;面向应用工程师的商业软件咨询、自研软件定制开发服务的仿真公众号&#xff0c;点击关注进入菜单&#xff0c;查看更多精彩内容。OPTIMUS提供自适应进化算法(Self-adaptive Evolution)&#xff0c;从用户给定的起始解或者算法随机产生的起始种群开始&#xff0…

Java生产力提示:社区的热门选择

社区已经发言。 我们已将您最好和最出色的生产力技巧汇总到一篇文章中。 我们都有自己的小技巧&#xff0c;可以帮助我们提高工作效率&#xff0c;并提高生产率。 我们使用工具来避免繁琐的日常任务&#xff0c;并运行脚本来自动化流程。 我们所做的一切只是为了确保一切就绪&…

JAVA获取Classpath根路径的方法

方法一&#xff1a; String path Test.class.getResource("/").toString(); System.out.println("path " path); 此方法在tomcat下面没有问题&#xff0c;可以取到WEB-INF/classes path file:/home/ngidm_db2/AS_Tomcat7_0_29/webapps/NGIDM/WEB-INF/c…

Navicat 9.1、10.0 简体中文最新版,注册码(For Mysql)

Navicat 9.1、10.0 简体中文最新版&#xff0c;注册码(For Mysql) by 尘缘 on 七月 17th, 2011 // Filed Under → MySQL Navicat属于偶的必备开发工具&#xff0c;最新版的自动提示&#xff0c;SQL格式化比较好用。 今天测试过Navicat 9.1.11&#xff0c;注册码可以使用。 下…