(最小生成树)QS Network -- ZOJ --1586
链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82831#problem/E
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 <cmath>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n;
int J[N][N], dist[N];
bool vis[N]; int Prim()
{
int i, j, ans=;
dist[]=;
memset(vis, , sizeof(vis));
vis[]=; for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int index=;
int MIN=INF;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN && dist[j])
{
index=j;
MIN=dist[j];
}
}
vis[index]=;
ans += MIN;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j])
dist[j]=J[index][j];
}
}
return ans;
} int main ()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, j, a, b[N];
scanf("%d", &n); memset(J, , sizeof(J));
memset(b, , sizeof(b)); for(i=; i<=n; i++)
{
scanf("%d", &a);
b[i] = a;
} for(i=; i<=n; i++)
for(j=; j<=n; j++)
{
scanf("%d", &a);
J[i][j] = a;
} for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
J[i][j] += b[i]+b[j];
} int ans=Prim(); printf("%d\n", ans);
}
return ;
}
(最小生成树)QS Network -- ZOJ --1586的更多相关文章
- E - QS Network - zoj 1586(简单)
题意:在一个叫做QS的星系,他们使用一些特殊的通讯方式,两个人之间通讯需要使用一个网络适配器,但是一个网络适配器只能跟一个人联系,所有它连接几个人就需要及格适配器,而且每个人都有一些不同的偏好,喜欢的 ...
- QS Network ZOJ - 1586 板子题
#include<iostream> #include<algorithm> using namespace std; ; struct edge{ int a,b; doub ...
- 最小生成树-QS Network(Prim)
题目大意: 给出的案例结果得出步骤,如下图所示,从结点1开始查找,找出的一条路径如绿色部分所标注.(关键处在于连接每条路径所需要的适配器的价格得加上去) 代码实现: #include<iostr ...
- ZOJ 1586 QS Network (最小生成树)
QS Network Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit Sta ...
- 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(Kruskal算法求解MST)
题目: In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS. QScommunica ...
- ZOJ1586:QS Network (最小生成树)
QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...
- ZOJ1586——QS Network(最小生成树)
QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...
随机推荐
- 数据类型-DataFrame
数据类型-DataFrame DataFrame是由多个Series数据列组成的表格数据类型,每行Series值都增加了一个共用的索引 既有行索引,又有列索引 行索引,表明不同行,横向索引,叫inde ...
- sqlserver批量导出存储过程、函数、视图
select text from syscomments s1 join sysobjects s2 on s1.id=s2.id where xtype = 'V' xtype V 视图 P ...
- Unity 5.1+ Assertion Library (断言库)
Unity 5.1+ ,加入了“断言库”,在 Asset 类中可以方便的找到需要使用断言的函数. UnityEngine.Assertions.Assert.IsNotNull( ) 为何使用断言 使 ...
- Ubuntu cd
查看目录文件内容 ./ or filename/file.* cd 返回用户主目录 ~,,,,,/home/user cd ..不管用 cd / 返回用户根目录 root
- 翻转链表reverse linked list:全部,m~n
全部 [抄题]: Reverse a singly linked list. [思维问题]: 以为要用dummy node [一句话思路]: 直接全部转过来就行了,用dummy node反而多余 [输 ...
- Extjs Vbox布局方式,以及align种类,flex,pack属性含义简介
VBox布局方式,熟悉下一下几个主要属性: 一.align:字符类型,指示组件在容器内的对齐方式.这个是基于容器的左上角来排列的.pack不同,pack是根据容器的最上边来显示的. 1.left(默认 ...
- ViewPager欢迎界面
一.几张图片组成欢迎界面 下方有几个点对应每个图片 当图片被选中时对应的点会变亮,当对应的点被点击时也会切换到指定画面 以下是代码 package com.example.viewpager_1; i ...
- 批量MD5命名文件
#coding=utf-8 import os import hashlib def GetFileMd5(filename): if not os.path.isfile(filename): pr ...
- ubuntu14.04安装opengl
OpenGL 是一套由SGI公司发展出来的绘图函式库,它是一组 C 语言的函式,用于 2D 与 3D 图形应用程式的开发上. 不可或缺的就是编译器与基本的函式库,如果系统没有安装的话,依照下面的方式安 ...
- win10下安装oracle11G Examples出错[INS-32025][INS-52001]
安装oracle examples时提示出错:[INS-32025] 所选安装与指定 Oracle 主目录中已安装的软件冲突.[INS-52001] Oracle Database Examples ...