Description

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

题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用。

分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和。

#include<stdio.h>
#include<string.h>
#include<map>
#include<iostream>
#include<algorithm>
#include<math.h>
#define N 1100
#define INF 0xfffffff using namespace std; int dist[N], vis[N], n, maps[N][N]; void Init()
{
for(int i=; i<=n; i++)
{
vis[i] = ;
dist[i] = INF;
for(int j=; j<=n; j++)
{
maps[i][j] = INF;
}
}
} int Prim(int start)
{
int ans = ;
for(int i=; i<=n; i++)
dist[i] = maps[start][i];
vis[start] = ;
for(int i=; i<=n; i++)
{
int Min = INF,index = -;
for(int j=; j<=n; j++)
{
if(vis[j]== && Min > dist[j])
Min = dist[j], index = j;
}
if(index == -) break;
vis[index] = ;
ans += Min;
for(int j=; j<=n; j++)
{
if(vis[j]== && dist[j] > maps[index][j])
{
dist[j] = maps[index][j];
}
}
}
return ans;
} int main()
{
int T, i, j, p[N], x;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
Init();
for(i=; i<=n; i++)
scanf("%d", &p[i]);
for(i=; i<=n; i++)
{
for(j=; j<=n; j++)
{
scanf("%d", &x);
if(i>j)
maps[i][j] = maps[j][i] = x + p[i] + p[j];
}
maps[i][i] = ;
}
int ans = Prim();
printf("%d\n", ans);
}
return ;
}

QS Network---zoj1586最小生成树的更多相关文章

  1. ZOJ1586——QS Network(最小生成树)

    QS Network DescriptionIn the planet w-503 of galaxy cgb, there is a kind of intelligent creature nam ...

  2. ZOJ1586:QS Network (最小生成树)

    QS Network 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1586 Description: In th ...

  3. ZOJ1586 QS Network

    QS Network Time Limit: 2 Seconds      Memory Limit: 65536 KB Sunny Cup 2003 - Preliminary Round Apri ...

  4. 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 ...

  5. ZOJ 1586 QS Network Kruskal求最小生成树

    QS Network Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the ...

  6. ZOJ 1586 QS Network (最小生成树)

    QS Network Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Sta ...

  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 ...

  10. E - QS Network

    E - QS Network 思路:最小生成树,数组不要开小了. #include<cstdio> #include<cstring> #include<iostream ...

随机推荐

  1. Android开发-- The content of the adapter has changed but ListView did not receive a notification - With AsyncTask

    最近在联系开发DaysMatter时遇到一个问题: app中使用ListView来展示所有事件,每次添加完事件后使用下面代码来更新ListView. toDoListView.refreshDrawa ...

  2. CookieUtils工具类

    package com.taotao.common.util; import java.io.UnsupportedEncodingException; import java.net.URLDeco ...

  3. 在iOS中使用icon font

    博文转载至 http://www.cocoachina.com/industry/20131111/7327.html 在开发阿里数据iOS版客户端的时候,由于项目进度很紧,项目里的所有图标都是用最平 ...

  4. mysql check约束无效

    转自http://blog.csdn.net/maxint64/article/details/8643288 今天在mysql中尝试使用check约束时,才知道在MySQL中CHECK约束是无效的, ...

  5. CWnd与HWND的区别与转换

    CWnd与HWND的区别与转换 2011-10-20 10:29:30|  分类: VC学习库|字号 订阅     一.区别HWND是句柄,CWnd是MFC窗体类,CWnd中包含HWND句柄成员对象是 ...

  6. Java网络编程之TCP通信

    一.概述 Socket类是Java执行客户端TCP操作的基础类,这个类本身使用代码通过主机操作系统的本地TCP栈进行通信.Socket类的方法会建立和销毁连接,设置各种Socket选项. Server ...

  7. XML的基本用法

    一.概述 XML全称为可扩展的标记语言.主要用于描述数据和用作配置文件. XML文档在逻辑上主要由一下5个部分组成: XML声明:指明所用XML的版本.文档的编码.文档的独立性信息 文档类型声明:指出 ...

  8. JDBC批量执行executeBatch

    JDBC事务 在数据库中,所谓事务是指一组逻辑操作单元,使数据从一种状态变换到另一种状态.为确保数据库中数据的一致性,数据的操纵应当是离散的成组的逻辑单元:当它全部完成时,数据的一致性可以保持,而当这 ...

  9. 【Mac】安装MAMP的PHPredis扩展

    1 下载phpredis扩展安装包 cd /usr/local git clone https://github.com/nicolasff/phpredis.git 2 依次执行以下操作完成安装 $ ...

  10. 概率法计算PI

    #include <iostream> using namespace std; //概率计算PI int main() { ; double val; int i; ; i<; i ...