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 ...
随机推荐
- ASP.NET - 缓存(Cache)
页面缓存: 给页面添加<%@ OutPutCache Duration = “15” VaryByParam = “none” %> 这样就可以启用页面缓存了,那么在规定的时间内,页面之访 ...
- ASP.NET - 用户控件制作
首先添加用户控件: 在里面写上代码: <%@ Control Language="C#" AutoEventWireup="true" CodeBehin ...
- Eclipse用法和技巧六:自动生成get和set方法1
java的类中,除了常量声明为静态且公有的,一般的对象数据作用域,都是声明为私有的.这样做能保护对象的属性不会被随意改变,调试的时候也会方便很多:在类的公有方法中大一个调用栈就能看到哪里改了属性值.声 ...
- WebService 之 WSDL文件 解说
恩,我想说的是,是不是常常有人在开发的时候,特别是和第三方有接口的时候,走的是SOAP协议,然后用户给你一个WSDL文件,说依照上面的进行适配,嘿嘿,这个时候,要是你曾经没有开发过,肯定会傻眼,那假设 ...
- 将 Shiro 作为一个许可为基础的应用程序 五:password加密/解密Spring应用
考虑系统password的安全,眼下大多数系统都不会把password以明文的形式存放到数据库中. 一把会採取下面几种方式对password进行处理 password的存储 "编码" ...
- 我们究竟什么时候可以使用Ehcache缓存(转)
一.Ehcache是什么 EhCache是Hibernate的二级缓存技术之一,可以把查询出来的数据存储在内存或者磁盘,节省下次同样查询语句再次查询数据库,大幅减轻数据库压力. 二.Ehcache的使 ...
- windows消息处理(强烈推荐,收藏)
由于看了一下,比较好理解,暂时先放到这里,待有空再翻译.只是在每节后大致介绍一下讲的内容. 感觉写的比较全,无论从消息的原理还是从MFC操作上来说,值得一看,我也在此做个收藏. (一) 说明:以下首先 ...
- asp.net web api帮助文档的说明
为asp.net的mvc web api填写自己的帮助文档 1. 加入Help的area(能够通过命令行或其它方式加入) 命令行:Install-Package Microsoft.AspNet.We ...
- 判断指定进程是否为x64的方法(在ntdll判断某个x64函数是否存在)
BOOL IsWow64ProcessEx(HANDLE hProcess) { // 如果系统是x86的,那么进程就不可能有x64 bool isX86 = false; #ifndef _WIN6 ...
- C++ 虚函数表解析(比较清楚,还可打印虚函数地址)
C++ 虚函数表解析 陈皓 http://blog.csdn.net/haoel 前言 C++中的虚函数的作用主要是实现了多态的机制.关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父 ...