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. 2、java注释、标识符、数据类型、类型转换

    一.三种注释:单行注释.多行注释.文档注释(只能在类前或者方法前,@author作者) 二.java使用的编码为unicode码[0-65535]   包含ASCII码,在0-255中 ASCII码( ...

  2. 【翻译自mos文章】开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法

    开启dblink的 oracle net trace/tracing --对dblink进行跟踪的方法. 參考原文: DBLINK: How to Enable Oracle Net Tracing ...

  3. windows tensorflow 版本与升级

    tensorflow 的版本在 1.1.0/1.2.0 之后 api 迎来重大变化,有必要将版本升级到最新的 1.1.0 以上. 1. 使用 upgrade CPU:pip3 install –upg ...

  4. directshow 获取本地摄像头播放

    最近因为项目的需要,做了一个基本的获取本地笔记本摄像头并且播放的例子,因为网上的关于这部分的完整例子基本都没有,那我就上传一个吧,希望能够帮到需要学习视频的朋友. 另外也是为了纪念雷霄骅博士为音视频方 ...

  5. java9新特性-2-安装与官网说明

    1.jdk 9的下载 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html         下载安 ...

  6. C#-汉字转拼音缩写

    /// 〈summary〉 /// 汉字转拼音缩写 /// Code By MuseStudio@hotmail.com /// 2004-11-30 /// 〈/summary〉 /// 〈para ...

  7. RelativeLayout中的baseline

    比如,加入两个相邻的TextView,给第二个TextView一个大一点的padding(比如20dp),如果加了layout_alignBaseline到第二个TextView中的话, TextVi ...

  8. JDOJ 2939: Suffix Automaton 广义后缀自动机_统计子串

    建立广义后缀自动机,对每个节点都建立各自的 $Parent$ 数组. 这样方便统计,不会出现统计错误. 考虑新加入一个字符. 1 这条转移边已经存在,显然对答案没有贡献. 2 这条转移边不存在,贡献即 ...

  9. 编译php并与nginx整合

    告诉 Nginx 如何处理 php 文件:          nginx>vim  conf/nginx.conf                     location ~ \.php${ ...

  10. nginx旧版本升级新版本

        比如我们现在所用的是 nginx 是1.4 版本,过了一段时间后我们有新的稳定版 1.6 问世,我们想升级到新的版本怎么办?           1.把新版本解压.安装,然后将 sbin/ng ...