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 ...
随机推荐
- CCEditBox/CCEditBoxImplAndroid
#ifndef __CCEDITBOXIMPLANDROID_H__ #define __CCEDITBOXIMPLANDROID_H__ #include "cocos2d.h" ...
- echarts 地图 动态 展示 结合css+js
echarts地图展示功能非常强大,官网上静态展示的样例非常多了,动态的资料少.研究了下.我眼下实现的通过ajax从server获取数据动态展示地图. 另外,我们有时候希望在地图之上做些自己定义的东西 ...
- 聊聊高并发(四十四)解析java.util.concurrent各个组件(二十) Executors工厂类
Executor框架为了更方便使用,提供了Executors这个工厂类.通过一系列的静态工厂方法.能够高速地创建对应的Executor实例. 仅仅有一个nThreads參数的newFixedThrea ...
- HDFS的安全模式
- OpenGL编程逐步深入(十)索引绘制
准备知识 OpenGl提供了一些绘图函数.到目前为止我们使用的glDrawArrays绘图函数属于"顺序绘制".这意味着顶点缓冲区从指定的偏移量开始被扫描,每X(点为1,直线为2等 ...
- Win32++:可替代MFC的Windows桌面应用开发框架
写在前面 有过Win32编程经验的朋友都知道,使用Windows提供的API开发桌面应用是相当繁琐的,创建一个功能简单能接收并处理消息的窗口至少也得几百行代码.创建一个可视化的窗口一般要以下几个步骤: ...
- EasyUI--Alert()
1.$.messager.alert(title, msg, icon, fn) 2 <script type="text/javascript"> $(functio ...
- c#做对比软件
一些 HTML内容比较/文本差异比较 开源代码 1. DiffPlex - a .NET Diff Generator http://diffplex.codeplex.com/SourceCo ...
- vue实现文字上下滚动
实现文字的上下滚动使用positon的relative的top属性,通过动态改变top来实现相关内容的更换,通过transion来实现相关的动画效果, 相关的dom内容 <template> ...
- mongodb 主从
mongodb 主从 因为条件限制我们把主从放在一台服务器上面 相关参数 在启动从的时候可以增加以下参数 --autoresync 当发现从服务器的数据不是最新时,开始从主服务器请求同步数据 --sl ...