QS Network

Time Limit: 1000ms
Memory Limit: 32768KB

This problem will be judged on FZU. Original ID: 1096
64-bit integer IO format: %I64d      Java class name: Main

 

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

解题:需要注意:每个网络适配器只能用于单一的通信中
 #include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 0x3f3f3f3f
#define pii pair<int,int>
using namespace std;
const int maxn = ;
struct arc{
int to,w,next;
arc(int x = ,int y = ,int z = -){
to = x;
w = y;
next = z;
}
}e[maxn*maxn];
int head[maxn],d[maxn],adapter[maxn],tot,n;
bool done[maxn];
priority_queue< pii,vector< pii >,greater< pii > >q;
void add(int u,int v,int w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
int prim(){
int ans = ;
for(int i = ; i < maxn; ++i){
d[i] = INF;
done[i] = false;
}
while(!q.empty()) q.pop();
q.push(make_pair(d[] = ,));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
ans += d[u];
for(int i = head[u]; ~i; i = e[i].next){
if(d[e[i].to] > e[i].w) q.push(make_pair(d[e[i].to] = e[i].w,e[i].to));
}
}
return ans;
}
int main(){
int kase;
scanf("%d",&kase);
while(kase--){
memset(head,-,sizeof(head));
int ans = tot = ,tmp;
scanf("%d",&n);
for(int i = ; i < n; ++i)
scanf("%d",adapter+i);
for(int i = ; i < n; ++i){
for(int j = ; j < n; ++j){
scanf("%d",&tmp);
if(i != j) add(i,j,tmp+adapter[i]+adapter[j]);
}
}
printf("%d\n",prim());
}
return ;
}

FZU 1096 QS Network的更多相关文章

  1. ZOJ1586 QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  2. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  3. ZOJ1586——QS Network(最小生成树)

    QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...

  4. (最小生成树)QS Network -- ZOJ --1586

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

  5. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

  6. ZOJ1586 QS Network 2017-04-13 11:46 39人阅读 评论(0) 收藏

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  7. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  8. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  9. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

随机推荐

  1. 10.29 工作笔记 ndk编译C++,提示找不到头文件(ndk-build error: string: No such file or directory)

    ndk编译C++.提示找不到头文件(ndk-build error: string: No such file or directory) 被这个问题弄得愁眉苦脸啊.心想为啥一个string都找不到呢 ...

  2. 使用python fabric搭建RHEL 7.2大数据基础环境以及部分优化

    1.使用python fabric进行Linux基础配置 使用python,可以让任何事情高效起来,包括运维工作,fabric正式这样一套基于python2的类库,它执行本地或远程shell命令提供了 ...

  3. 使用U盘安装win8(win8.1)系统

    下载镜像 下载系统镜像,可以选32位或64位.下完后放到非系统盘(如果需要重新分区的,要先保存到移动硬盘或U盘) 使用U盘制作PE 准备一个4G以上的U盘(备份好U盘里的资料,因为刻录PE需要格式化U ...

  4. CUDA学习之从CPU架构说起

    最近要学习GPU编程,就去英伟达官网下载CUDA, 遇到的第一个问题就是架构的选择 所以我学习的CUDA的第一步是从学习认识CPU架构开始的,x86-64简称x64,是64位版的x86指令集,向前兼容 ...

  5. Maven(一)之Maven入门

    一.Maven简介 Maven可以翻译为“知识的积累”.“专家”.“内行”.作为Apache组织中的一个颇为成功的开源项目,Maven主要服务于基于Java平台的项目构建.依赖管理.和项目信息管理.M ...

  6. Hyper-V 导入与导出虚拟机

    虚拟机的导入与导出功能可以将虚拟机通过文件的方式进行转移,可以将虚拟机的文件复制到活动硬盘,然后带到其他的地点进行导入,这样方便了虚拟机的跨地域的转移.但是有一点要注意,所有要转移的虚拟机都必须处于停 ...

  7. NOIP2017 Day-1 模板荟萃

    #include<bits/stdc++.h> #define MAXN 100005 using namespace std; int read(){ ;char c=getchar() ...

  8. [转载]-win7启动本地MongoDB的四种方式

    2016年04月07日 09:52:34 cherry__cheng 阅读数:19451 标签: win7启动本地MongoDB的四种方式快速启动本地mongodb 更多 个人分类: mongodb& ...

  9. 用Python爬取影视网站,直接解析播放地址。

    记录时刻! 写这个爬虫主要是想让自己的爬虫实用,把脚本放到了服务器,成为可随时调用的接口. 思路算是没思路吧!把影视名带上去请求影视网站,然后解析出我们需要的播放地址. 我也把自己的接口分享出来.接口 ...

  10. wall---向系统当前所有打开的终端上输出信息

    wall命令用于向系统当前所有打开的终端上输出信息.通过wall命令可将信息发送给每位同意接收公众信息的终端机用户,若不给予其信息内容,则wall命令会从标准输入设备读取数据,然后再把所得到的数据传送 ...