QS Network

Description
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

题目大意:

    QS是一种外星人(雾?),他们之间需要网线和路由器来联系(大雾?)。他们每个人都有自己喜欢的路由器,并且路由器上只能接一条网线(一个路由器只能与一个人联系)。

    输入T代表几组数据。每组数据的第一行为N,表示有多少个QS人,第二行为这N个人喜欢的路由器的价格。

    后面的N行为一个N*N的矩阵,表示他们之间各自的要想联系需要的网线的价格(距离)。

    输出:保证所有人相连的最小花销。

解题思路:

    最小生成树。需要注意的是 每条边的权值不单单是网线的价格还要包括两个顶点的路由器价格。

Code:

 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#define MAXN 1010*1010/2
using namespace std;
struct edge
{
int begin;
int end;
int dis;
} T[MAXN+];
int father[MAXN+],a[MAXN+];
void init()
{
for (int i=;i<=MAXN;i++)
father[i]=i;
}
int find(int x)
{
if (father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
void join(int x,int y)
{
int fx=find(x),fy=find(y);
if (fx!=fy)
father[fx]=fy;
}
bool cmp(struct edge a,struct edge b)
{
return a.dis<b.dis;
}
int main()
{
int C;
cin>>C;
while (C--)
{
init();
int k=;
int N;
cin>>N;
int cnt=;
for (int i=; i<=N; i++)
cin>>a[i];
for (int i=; i<=N; i++)
{
for (int j=; j<i; j++)
{
T[k].begin=i,T[k].end=j;
cin>>T[k].dis;
T[k].dis+=a[i]+a[j];
k++;
}
for (int j=i;j<=N;j++)
{
int tmp;
cin>>tmp;
}
}
sort(T+,T+k,cmp);
int sum=;
for (int i=;i<k;i++)
{
//printf("%d ",T[i].dis);
if (find(T[i].begin)!=find(T[i].end))
{
sum++;
cnt+=T[i].dis;
join(T[i].begin,T[i].end);
if (sum==N-) break;
}
}
printf("%d\n",cnt);
}
return ;
}

ZOJ1586——QS Network(最小生成树)的更多相关文章

  1. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  2. ZOJ1586 QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  3. 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 ...

  4. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  5. QS Network(最小生成树)

    题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决 ...

  6. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  7. (最小生成树)QS Network -- ZOJ --1586

    链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 http://acm.hust.edu.cn/vjudge/ ...

  8. 最小生成树 E - QS Network

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  9. ZOJ 1586 QS Network(Kruskal算法求解MST)

    题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...

随机推荐

  1. PERL 脚本

    PERL: Practical Extraction and Report Language 参考文档 1.Perl 5 version 24.0 documentation

  2. web响应式之bootstrap的基础用法。

    1/首先必须在head里面引用视窗viewport,以保证之后可以响应式分布 <!--meta:vp 响应式布局--> <meta name="viewport" ...

  3. MAC机中安装RUBY环境

    在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY的环境进行一总结.安装Ruby环境首先需要安装Xcode然后需要安装Homebrew,接下来需要安装RVM最后安装Ruby环境 ...

  4. AngularJS(5)-Http

    $http 是 AngularJS 中的一个核心服务,用于读取远程服务器的数据 加入下面有一个存储在web服务器上的数据,假设地址为http://TestWebData/myData.php { &q ...

  5. 教你配置linux服务器登陆欢迎信息

    登录一台测试机时发现显示PS1前还显了一行’Welcome to Cloudex’的欢迎信息,真丫牛B.在linux中,设置/etc/issue和/etc/motd文件即可实现这样的登录欢迎信息. 登 ...

  6. Python urllib2 模块学习笔记

    2015.3.6  urllib2的使用方法大致如下 # 定制Handler处理函数 opener = urllib2.build_opener(ProxyHandler, HTTPHandler) ...

  7. android.support.v7.app.AppCompatActivity

    1.Android Studio (api 23) 新建项目的时候 Activity public class MainActivity extends AppCompatActivity 2.系统默 ...

  8. 通过telnet命令进行网络邮件发送

    1.建立smtp邮箱服务连接 open smtp.sina.com 2.连接上邮箱服务后进行握手操作 helo smtp.sina.com 3.输入帐号密码进行验证::此步后缓冲区会输出一些字符,你只 ...

  9. 使用Schtasks命令的注意事项

    在使用Schtasks命令时遇到了两个棘手的问题,耗费了一点时间,出现这个问题的时候查找网络资源也找不到真正的解决方案,最后还是自己悟出来了原因,所以在此把这些问题记录下来.如下: 服务器环境:win ...

  10. Qt的gzip模块实现

    一直没找到Qt中方便的gzip模块,于是自己动手,调用zlib模块实现了一份. 目标:  1.gzip的压缩与解压 2.内存中操作 3.方便的Qt接口   实现分析: gzip 压缩算法为 defla ...