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. android自己定义圆盘时钟

    自己定义圆盘时钟的大概流程:由于圆盘时钟的圆盘是不须要动的,所以不必要加在自己定义的view里面,在view里面仅仅须要绘制秒针和分针时针并控其转动就可以. 下面就是自己定义view的主要代码: pa ...

  2. C++的IO操作

    #include <iostream> using namespace std; int main() {         char name[20];         char gend ...

  3. Vue小技巧,如何导入普通JS文件

    最近在开发一个展示3D模型的WEB程序,在工程中使用了VUE和ThreeJS库.Three.js本身是支持CommonJS的,但我们还用到了OBJLoader模块,此模块不支持CommonJS,改成C ...

  4. hdoj--5256--序列变换(lis变形)

    序列变换 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. Python: PS 图像调整--颜色梯度

    本文用 Python 实现 PS 中的色彩图,可以看到颜色的各种渐变,具体的效果可以参考以前的博客: http://blog.csdn.net/matrix_space/article/details ...

  6. [JZOJ3385] [NOIP2013模拟] 黑魔法师之门 解题报告(并查集)

    Description 经过了16个工作日的紧张忙碌,未来的人类终于收集到了足够的能源.然而在与Violet星球的战争中,由于Z副官的愚蠢,地球的领袖applepi被邪恶的黑魔法师Vani囚禁在了Vi ...

  7. OpenGL的前世和今生

    这并不是一个恰当的题目,因为我主要想说的是OpenGL的今生,基于OpenGL3.x一种更现代化的方式.但是把前世和今生放在一起在语言上更加连贯,而且适当的了解过去,会帮助理解现在的OpenGL,以一 ...

  8. Apache Bench测试

    - 压力测试神器 Siege - Locust Web测压工具python开源 - 介绍: ab是apachebench命令的缩写. ab的原理:ab命令会创建多个并发访问线程,模拟多个访问者同时对某 ...

  9. 我的Spring MVC第一个应用 (最终版)

    项目结构图: 代码如下: Product package com.mstf.bean; import java.io.Serializable; /** * Product类,封装了一些信息,包含三个 ...

  10. sql 除法运算 保留两位小数

    sql 除法运算 保留两位小数 SELECT 1530/60 select cast(1530*1./60 as decimal(18,1))