FZU 1096 QS Network
QS Network
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的更多相关文章
- ZOJ1586 QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ1586——QS Network(最小生成树)
QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...
- (最小生成树)QS Network -- ZOJ --1586
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...
- ZOJ 1586 QS Network(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- 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 ...
- ZOJ1586:QS Network (最小生成树)
QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...
- ZOJ 1586 QS Network Kruskal求最小生成树
QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...
- 最小生成树 E - QS Network
Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...
随机推荐
- ios xcode真机调试获取屏幕截屏
非常多时候我们须要在调试的过程中把手机屏幕截图发给其它人看,在android开发中我们能够非常方便的截图保存.而xcode开发ios的时候发现这个需求却如此困难.网上大部分都是介绍的以下这个方案.可是 ...
- AVEVA PDMS to DIALux
AVEVA PDMS to DIALux eryar@163.com Abstract. DIAL develops DIALux - the world's leading software f ...
- CodeForces 550B Preparing Olympiad(DFS回溯+暴力枚举)
[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每一个题目有对应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x, ...
- OCP-1Z0-051-题目解析-第50题
50. SLS is a private synonym for the SH.SALES table. The user SH issues the following command: DRO ...
- 树莓派学习路程No.2 GPIO功能初识 wiringPi安装
WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi遵守GUN Lv3.wiringPi使用C或者C++开发并且可以被其他语言包转,例如python.ruby或者PHP等.Wiri ...
- 海思 3520D 移植Qt4.5.3 一
一.移植Qt4.5.3 1.获得 Qt4.5.3 的源码Qt4.5.3源码的原始包 qt-embedded-opensource-src-4.5.3.tar.gz 将其复制到 /opt 下, ...
- Find and counter
Find: In a sense, find is the opposite of the [] operator. Instead of taking an index and extracting ...
- HDFS的配额
- 编码问题异常处理:UnicodeDecodeError: 'gbk' codec can't...
作为编码问题集合: 2)UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbd in position 0: invalid start by ...
- 洛谷 P1273 有线电视网 && caioj 1109 树形动态规划(TreeDP)4:比赛转播(树上分组背包总结)
从这篇博客往前到二叉苹果树都可以用分组背包做 这依赖性的问题,都可以用于这道题类似的方法来做 表示以i为根的树中取j个节点所能得的最大价值 那么每一个子树可以看成一个组,每个组里面取一个节点,两个节点 ...