ZOJ 1586 QS Network(Kruskal算法求解MST)
题目:
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.
<b< dd="">
Output
for each data set,output the minimum cost in a line. NO extra empty lines needed.
<b< dd="">
Sample Input
1
3
10 20 30
0 100 200
100 0 300
200 300 0
<b< dd="">
Sample Output
370
题意描述:
题目描述的很有意思,不过有点绕。简单来说就是每两个人要建立联系,需要分别购买一个自己最喜欢的接收器,和共同购买一个共用转换器。
先输入结点的个数N,再输入N个数,表示结点最喜欢的接收器的价格,最后输入N*N的矩阵表示i和j建立联系的转换器价格。
解题思路:
该开始的时候想用Prim算法,在选择一条边可以建造的时候,将两个结点都计一下数,最后乘以每个结点喜欢的接收器的价格再加上最小生成树的值。但是这种解法是有漏洞的,因为如果接收器是后来加的,它的费用是没有考虑进最省路径的,所以有可能,接收器的价格很贵,虽然转换器是最省的但加上接收器的话就不是最省路径了。综上所述,选择Kruskal算法,不同的是将接收器的价格事先计算在这条边上,那么就跟模板题一样了。
代码实现:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct edge
{
int u,v,w;
};
int cmp(struct edge x,struct edge y)
{
return x.w<y.w;
}
struct edge e[*];
int merge(int v,int u);
int getf(int v);
int map[][],f[];
int main()
{
int t,n,a[],i,j,k,c,sum;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<=n;i++)
for(j=;j<=n;j++)
scanf("%d",&map[i][j]); k=;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
e[k].u=i;
e[k].v=j;
e[k++].w=map[i][j]+a[i]+a[j];
}
}
sort(e+,e+k,cmp);
for(i=;i<=n;i++)
f[i]=i;
sum=;
c=;
for(i=;i<k;i++)//边的数目
{
if( merge(e[i].u,e[i].v) )
{
c++;
sum += e[i].w;
}
if( c == n-)
break;
}
printf("%d\n",sum);
}
return ;
} int merge(int v,int u)
{
int t1,t2;
t1=getf(v);
t2=getf(u);
if(t1 != t2)
{
f[t2]=t1;
return ;
}
return ;
}
int getf(int v)
{
if(f[v]==v)
return v;
else
{
f[v]=getf(f[v]);
return f[v];
}
}
易错分析:
1、注意遍历的时候遍历的是边的条数(代码功力还是要加强的)
ZOJ 1586 QS Network(Kruskal算法求解MST)的更多相关文章
- 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 (Prim)
ZOJ - 1586 QS Network (Prim) #include<iostream> #include<cstring> using namespace std; + ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- POJ 1251 Jungle Roads(Kruskal算法求解MST)
题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...
- HDU 2682 Tree(Kruskal算法求解MST)
题目: There are N (2<=N<=600) cities,each has a value of happiness,we consider two cities A and ...
- HDU 5253 连接的管道(Kruskal算法求解MST)
题目: 老 Jack 有一片农田,以往几年都是靠天吃饭的.但是今年老天格外的不开眼,大旱.所以老 Jack 决定用管道将他的所有相邻的农田全部都串联起来,这样他就可以从远处引水过来进行灌溉了.当老 J ...
- ZOJ 1586 QS Network MST prim水题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 题目大意: QS是一种生物,要完成通信,需要设备,每个QS需要的设备的价格 ...
- zoj 1586 QS Network
最小生成树,刚刚学了Prim算法. 对每条边变的权值进行预处理,c[i][j] = c[i][j] + p[i] + p[j] 其中c[i][j]为输入的权值,p[i],p[j]为连接这两个节点所需的 ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
随机推荐
- fleet中service之间的依赖关系
最近有人在topcoder上提出使用fleet在集群上部署service时有时候会发现,当启动依赖于整个集群服务的service时,只会检查那个service所在机器的依赖关系,这样就会造成一些问题, ...
- 爬虫浅谈一:一个简单c#爬虫程序
这篇文章只是简单展示一个基于HTTP请求如何抓取数据的文章,如觉得简单的朋友,后续我们再慢慢深入研究探讨. 图1: 如图1,我们工作过程中,无论平台网站还是企业官网,总少不了新闻展示.如某天产品经理跟 ...
- WPF添加样式字典Style
新建Resource Dictionary文件,取名Style: 将常用的样式写入该文件: 在App.xaml中引用该文件: <Application x:Class="Machine ...
- C# 成员变量和属性的区别
之前一直在C#中使用这两者, 却一直不知道成员变量和属性还是不一样的两种概念. 不过说不一样, 也不是完全对. 简单举个例子: public class myclass { public string ...
- C++解析头文件-Qt自动生成信号定义
目录 一.概述 二.实现思路 三.代码讲解 1.类图 2.QtCppDescription 3.测试 四.源代码 一.概述 上一篇文章C++解析头文件-Qt自动生成信号声明我们主要讲解了怎么去解析C+ ...
- C语言 IPv6 十六进制 转 十进制
#include <stdio.h> #include <string.h> #include <math.h> //ipv4地址转换 int ipv4_to_i( ...
- 初识面向对象-封装、property装饰器、staticmathod(静态的方法)、classmethod(类方法) (五)
封装 # class Room:# def __init__(self,name,length,width):# self.__name = name# self.__length = length# ...
- PHPhotos
PHPhotoLibrary: @abstract A PHPhotoLibrary provides access to the metadata and image data for the ph ...
- 石头剪刀布Java实现
java实现石头剪刀布过程 首先来看石头剪刀布的所有可能情况,具体如下图 第一种思路是穷举所有可能,使用if条件语句,略显呆板和麻烦. 第二种思路,因为计算机存的是数字,所以我们可以从数字角度来找规律 ...
- 初识storm
storm是Twitter开发的一个开源的分布式实时计算系统,可以简单可靠的处理大量的数据流.storm有很多的应用场景,如实时分析,在线机器学习,持续计算,分布式RPC,ETL等等.storm支持水 ...