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. Libcurl笔记二

    一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. The ...

  2. iOS 非ARC基本内存管理系列 2-多对象内存管理(3) 利用@property来自动管理内存

    iOS 基本内存管理-多对象内存管理(2)中可以看到涉及到对象的引用都要手动管理内存:每个对象都需要写如下代码 // 1.对要传入的"新车"对象car和目前Person类对象所拥有 ...

  3. Call与Apply

    1.前言 ECMAscript中提供了两个方法(call,apply)用于改变对象内部的this指针,它们两个的作用都是一样的,但是传递的参数有点不大相同. 它们的大概语法为: call(this, ...

  4. apache重写字段详细说明

    用Apache虚拟主机的朋友很多,apache提供的.htaccess模块可以为每个虚拟主机设定rewrite规则,这对网站SEO优化相当有用,同时也改善了用户体验.国内的虚拟机一般不提供.htacc ...

  5. thymeleaf 模板引擎

    1.创建模板解析器 Create Template Resolver  用来加载模板 // create template resolver //创建模板解析器可以用Servlet上下文模板解析器Se ...

  6. Couchbase上发布的关于NoSQL的技术论文

    Couchbase是CouchDB与Membase两个NoSQL数据库相结合的产物,本文推荐的是Couchbase官方发表的一篇论文,命名为<NoSQL Database Technology& ...

  7. Hello,iOS

    xcode 6.1 File-New-Project.. iOs-Application-Simple View Application Main.storyboard ==> 拖一个TextV ...

  8. 用JS给浏览器的关闭按钮添加事件

    以下是指在js中实现,而非 <body onunload="close()"> 这种方法! 因为这样是在unload掉body的时候触发,而无论任何浏览器,都会在关闭的 ...

  9. Java 动态代理(转载)

    JAVA的动态代理  代理模式  代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后 处理消息等.代理类与 ...

  10. ios Base64编解码工具类及使用

    为了避免明码传递http内容,可以用base64编码后传输,收到方再解码,也方便了2进制数据的字符串式传输. 对于ios来说,google给提供了一个很好的工具类,方便进行base64编解码,当然也可 ...