ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&&prim)
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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<cmath>
#define MAXN 1005
#define INF 0x1f1f1f1f using namespace std; using namespace std; int n, ans, cas; //QS适配器的个数,结果,測试数据组数;
int lowcost[MAXN]; //充当Prim算法中的两个数组lowcost和nearvex的作用;
int adapt[MAXN]; //每一个QS喜欢的适配器的价格;
int Edge[MAXN][MAXN]; //邻接矩阵; void Init()
{
scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d", &adapt[i]); //输入价格;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++) //邻接矩阵初始化;
{
scanf("%d", &Edge[i][j]);
if(i == j) Edge[i][j] = INF;
else Edge[i][j] += (adapt[i]+adapt[j]);
}
}
memset( lowcost, 0, sizeof(lowcost) );
ans = 0;
} void Prim() //Prim算法核心;
{
lowcost[0] = -1; //从顶点0開始构造最小生成树;
for(int i=1; i<n; i++) lowcost[i] = Edge[0][i];
for(int i=1; i<n; i++) //把其它n-1个顶点扩展到生成树其中;
{
int min = INF, k;
for(int j=0; j<n; j++) //找到当前可用的权值最小的边;
{
if(lowcost[j] != -1 && lowcost[j] < min)
{
k = j;
min = lowcost[j];
}
}
ans += min;
lowcost[k] = -1; //把顶点k扩展进来;
for(int i=0; i<n; i++) if(Edge[k][i] < lowcost[i])
{
lowcost[i] = Edge[k][i];
}
}
printf("%d\n", ans);
} int main()
{
scanf("%d", &cas);
while(cas--)
{
Init();
Prim();
}
return 0;
}
ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&&prim)的更多相关文章
- Facebook Hacker Cup 2014 Qualification Round 竞赛试题 Square Detector 解题报告
Facebook Hacker Cup 2014 Qualification Round比赛Square Detector题的解题报告.单击这里打开题目链接(国内访问需要那个,你懂的). 原题如下: ...
- DP VK Cup 2012 Qualification Round D. Palindrome pairs
题目地址:http://blog.csdn.net/shiyuankongbu/article/details/10004443 /* 题意:在i前面找回文子串,在i后面找回文子串相互配对,问有几对 ...
- 最小生成树算法(Prim,Kruskal)
边赋以权值的图称为网或带权图,带权图的生成树也是带权的,生成树T各边的权值总和称为该树的权. 最小生成树(MST):权值最小的生成树. 生成树和最小生成树的应用:要连通n个城市需要n-1条边线路.可以 ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- Facebook Hacker Cup 2014 Qualification Round
2014 Qualification Round Solutions 2013年11月25日下午 1:34 ...最简单的一题又有bug...自以为是真是很厉害! 1. Square Detector ...
- 8VC Venture Cup 2016 - Elimination Round D. Jerry's Protest 暴力
D. Jerry's Protest 题目连接: http://www.codeforces.com/contest/626/problem/D Description Andrew and Jerr ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition)
暴力 A - Orchestra import java.io.*; import java.util.*; public class Main { public static void main(S ...
- 8VC Venture Cup 2016 - Elimination Round
在家补补题 模拟 A - Robot Sequence #include <bits/stdc++.h> char str[202]; void move(int &x, in ...
- VK Cup 2012 Qualification Round 1 C. Cd and pwd commands 模拟
C. Cd and pwd commands Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset ...
随机推荐
- Android 百度地图开发(二)--- 定位功能之MyLocationOverlay,PopupOverlay的使用
转载请注明出处http://blog.csdn.net/xiaanming/article/details/11380619 这一篇文章主要讲解的是百度地图的定位功能,然后还有MyLocationOv ...
- 基于visual Studio2013解决C语言竞赛题之1080填运算符
题目 解决代码及点评 /************************************************************************/ /* ...
- Android学习笔记(九)——更复杂的进度对话框
显示操作进度的对话框 1.使用上一篇创建的同一项目.在activity_main.xml文件里加入一个Button: <Button android:id="@+id/btn_dial ...
- python发送post和get请求
python发送post和get请求 get请求: 使用get方式时,请求数据直接放在url中. 方法一. import urllib import urllib2 url = "http: ...
- 网页插入QQ 无需加好友
<p>联系方式:1073351325<a href="tencent://message/?Menu=yes&uin=1073351325&Site=dsf ...
- LLBL Gen Pro 5.0
LLBL Gen Pro 5.0 企业应用开发入门 Solutions Design 公司于2016年5月发布了LLBL Gen Pro 5.0,这个新版本的发布出乎于我的意料.我的猜想是从4.2升级 ...
- 关于vs2005 __RPC__out __RPC__in 没有定义编译错误
1. 下载WDK http://www.microsoft.com/en-us/download/details.aspx?id=11800 2. 安装WDK 3. vs2005 设置:工具--> ...
- CMDeviceMotion使用
CMDeviceMotion使用 by 吴雪莹 manager = [[CMMotionManager alloc] init]; ViewController *__weak weakSelf=se ...
- 消息函数一般是私有的,因为不需要程序员显示的调用,但子类如果需要改写这个方法,则改成保护方法Protected
许多的面向对象程序设计语言都支持对消息的处理.消息处理是一种动态响应客户类发出的请求,它与过程调用不同.过程调用中,客户类必须知道服务类提供了哪些过程,以及每个过程的调用约定,并且在调用时需要明确指出 ...
- JAVA中IO和NIO的详解分析,内容来自网络和自己总结
用一个例子来阐释: 一辆客车上有10个乘客,他们的目的地各不相同,当没有售票员的时候,司机就需要不断的询问每一站是否有乘客需要下车,需要则停下,不需要则继续开车,这种就是阻塞的方式. 当有售票员的时候 ...