最小生成树 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 ...
随机推荐
- jQuery——表单应用(4)
HTML: <!--复选框应用--> <!DOCTYPE html> <html> <head> <meta charset="UTF- ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- Linux重启和关机命令
Linux重启命令: 方式1:shutdown –r now 方式2:reboot Linux关机命令: shutdown –h now
- C#基础 特殊集合
//stack 干草堆 //先进后出 进 push 出 pop //初始化 //Stack ss = new Stack(); //ss.Push(1); //ss.Push(2); //ss.Pus ...
- 生成清除某个数据库下的所有表的SQL语句
方法1:重建库和表 用mysqldump --no-data把建表SQL导出来,然后drop database再create database,执行一下导出的SQL文件: 方法2:生成清空所有表的SQ ...
- PS技巧汇总
一.gif图流程 1:素材图片a 图片b 2:窗口--->时间轴/动画 3:复制所选帧--->设置帧延迟 4:文件--->存储为WEB格式--->gif格式 二.批处理--- ...
- 远程监视jboss应用java内存的配置(实测) .
前言 因为最近一个项目部署在客户那边运行一个月左右就会出现java内存溢出的问题,为了时时监控java内存的情况需要,需要远程查看服务器上java内存的一些情况.在公司模拟部署了远程监视linux下项 ...
- HDU_1018_n(1e7)的阶乘的结果的位数
http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...
- 分析器错误消息: 该配置节不能包含 CDATA 或文本元素。
原因当然是web.config配置文件中,有字符串文本了,估计不小心加上的一些字符,所以会报错,去掉就行,例如13行的s
- C++ 程序的编译
一.编译器都具备集成开发环境(Integrated Developed Environment,IDE) 二.程序源文件命名约定: C++ 的后缀一般是 .cpp .cc .C .cpp .cxx 三 ...