最小生成树 E - QS Network
Sunny Cup 2003 - Preliminary Round
April 20th, 12:00 - 17:00
Problem E: QS Network
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<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
#define MAXN 1004
#define INF 0x3f3f3f3f
int g[MAXN][MAXN],lowcost[MAXN],p[MAXN],n;
bool been[MAXN];
int Prim(int beg)
{
int ans = ;
memset(been,false,sizeof(been));
for(int i=;i<=n;i++)
{
lowcost[i] = g[beg][i];
}
been[beg] = true;
for(int j=;j<n;j++)
{
int Minc = INF,k=-;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]<Minc)
{
Minc = lowcost[i];
k = i;
}
}
if(k==-) return -;
been[k] = true;
ans+=Minc;
for(int i=;i<=n;i++)
{
if(!been[i]&&lowcost[i]>g[k][i])
{
lowcost[i] = g[k][i];
}
}
}
return ans;
}
int main()
{
int t,tmp;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<=n;i++)
{
cin>>p[i];
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
cin>>tmp;
if(i!=j)
g[i][j] = p[i]+p[j]+tmp;
else
g[i][j] = tmp;
}
}
int ans = Prim();
cout<<ans<<endl;
}
return ;
}
最小生成树 E - QS Network的更多相关文章
- (最小生成树)QS Network -- ZOJ --1586
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...
- ZOJ1586——QS Network(最小生成树)
QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...
- 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 ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- ZOJ1586 QS Network
QS Network Time Limit: 2 Seconds Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...
- 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 ...
- E - QS Network
E - QS Network 思路:最小生成树,数组不要开小了. #include<cstdio> #include<cstring> #include<iostream ...
随机推荐
- 洛谷 P3437 [POI2006]TET-Tetris 3D
二维线段树区间更新啊 树套树的外层树,如果是线段树的话一般似乎不能打标记?(毕竟标记不好下传) 然而起码对于这题是可以的...对于外层线段树,每个节点放两个内层线段树dat和setv,分别是得到的值和 ...
- 同余模定理 HDOJ 5373 The shortest problem
题目传送门 /* 题意:题目讲的很清楚:When n=123 and t=3 then we can get 123->1236->123612->12361215.要求t次操作后, ...
- 解决:阿里云ECS上启动tomcat后,第一次访问时间特别长
Re在ECS上启动tomcat后,第一次访问时间特别长 2017-04-25 10:16:04 INFO com.world.socket.ServerSocketListener 25- ...
- c# Queue实现生产者(Producer)消费者(Consumer)模式
我们在开发过程中经常会遇到需要从一个地方不断获取数据然后又需要交给另一个线程对数据进行二次加工的情况,这种场景适合使用生产者-消费者模式. Demo展示 //中间的容器 public static c ...
- 数据库学习:for xml path
一.开发环境 数据库:SQLServer2012 二.语法简介 for xml path它以xml形式展示查询的结果集 三.语法介绍 现在数据库中有一张表 1.基本语法 select * from B ...
- Android setOnPageChangeListener 过时了怎么办?
今天使用ViewPager发现setOnPageChangeListener的方法居然过期了,而且AS编译不通过了,最后查了一下原来把set换成add了,代码如下: setOnPageChangeLi ...
- esp8266 SOC方案经过半年沉淀之后再度重启
我发誓这是最后一次玩esp8266,该脱坑了...... 以前看不懂的教程 http://club.gizwits.com/thread-6447-1-1.html 2018教程 http://clu ...
- 继续C#开发or转做产品
本人今年大四,C#开发,在一家公司实习了一年后,面临一个选择,是继续C#开发还是转做产品?我C#开发能力目前还一般,但很有兴趣.也喜欢设计与创意,做过产品专员.求大婶们指导!
- GCC编译连接c++代码的四个阶段(Four stages of GCC compilation of C++ code)
There are four stages for GCC to compile c/c++ applications: Preprocessing, Compilation proper, Asse ...
- 00The C Programming Language
The C Programming Language C语言是一门面向过程.抽象化的通用程序设计语言,广泛应用于底层开发.C语言能以简易的方式编译.处理低级存储器.C语言是仅产生少量的机器语言以及不需 ...