zoj QS 1586 Network (prim算法)

QS Network


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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"stdio.h"
#include"string.h"
#include"iostream"
#include"queue"
#include"algorithm"
using namespace std;
#define N 1005
const int inf=0x3fffffff;
int g[N][N],a[N],dis[N];
bool mark[N];
void prim(int s,int n)
{
    int i,j,u,min,sum=0;
    memset(mark,0,sizeof(mark));
    mark[s]=1;
    for(i=0;i<n;i++)
    {
        dis[i]=g[s][i];
    }
    for(j=1;j<n;j++)
    {
        min=inf;
        u=s;
        for(i=0;i<n;i++)
        {
            if(!mark[i]&&dis[i]<min)
            {
                min=dis[i];
                u=i;
            }
        }
        mark[u]=1;
        sum+=min;
        for(i=0;i<n;i++)
        {
            if(dis[i]>g[u][i])
            {
                dis[i]=g[u][i];
            }
        }
    }
    printf("%d\n",sum);
}
int main()
{
    int T,n,i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                scanf("%d",&g[i][j]);
                if(i!=j)
                    g[i][j]+=(a[i]+a[j]);
            }
        }
        prim(0,n);
    }
    return 0;
}

zoj QS 1586 Network (prim算法)

时间: 2024-11-06 03:41:41

zoj QS 1586 Network (prim算法)的相关文章

ZOJ 1586 QS Network prim优化模板

链接: 1586 题意: 有一个N X N的网络,每两个点都有边相连,边的权值用邻接矩阵输入,每个点也有一个权值,当它们之间的那条边被选取时,需要加上两个点的权值.求这个网络的最小生成树. 直接套用prim算法的模板 其中用到一个节约内存的优化 将lowdistance 和visit 两个数组 结合起来 如果访问过lowdistance改成-1即可 另外该题中边的权值应为边本身的权值加上两端点的权值. #include<iostream> #include<cstdio> #inc

poj 2349 Arctic Network (prim算法)

Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10525   Accepted: 3470 Description The Department of National Defence (DND) wishes to connect several northern outposts by a wireless network. Two different communication tec

数据结构与算法系列研究七——图、prim算法、dijkstra算法

图.prim算法.dijkstra算法 1. 图的定义 图(Graph)可以简单表示为G=<V, E>,其中V称为顶点(vertex)集合,E称为边(edge)集合.图论中的图(graph)表示的是顶点之间的邻接关系. (1) 无向图(undirect graph)      E中的每条边不带方向,称为无向图.(2) 有向图(direct graph)      E中的每条边具有方向,称为有向图.(3) 混合图       E中的一些边不带方向, 另一些边带有方向.(4) 图的阶      指

POJ 1459 &amp; ZOJ 1734 Power Network (网络最大流)

http://poj.org/problem?id=1459 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734 Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 22674   Accepted: 11880 Description A power network consists of nodes (power s

最小生成树问题(prim算法)POJ-1258 Agri-Net

/* 这个题很水,但是,莫名其妙runtime error一晚上,重写了一遍就又没了,很伤心! 题意很简单,大致为n个村庄,连光缆,要求连上所有村庄的长度最短. 输入n,接着是n*n的矩阵,直接用prim算法写就行: */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath>

杭电1162--Eddy&#39;s picture(Prim()算法)

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8070    Accepted Submission(s): 4084 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to b

hihocoder1097最小生成树(prim算法)

prim算法描述: prim算法的思想和代码都跟dijkstra算法非常相似. 在dijkstra算法中,我们用每次取出未标记集合中到源点最近的点进行标记并更新其邻接点到源点的距离:当d[x]+w<d[y]时更新d[y]=d[x]+w. 而在prim算法中,我们只需要对dijkstra算法稍作改动即可,即只需要改变一下更新操作:当w<d[y]时更新d[y]=w,此时d[y]的含义是节点y连接到最小生成树的边的最小取值.也就是说,从图中某个节点发出了边可能有若干条,而最终将该节点连接到最小生成树

prim算法模板

var g:array[1..10,1..10] of longint; d:array[1..10] of longint; f:array[1..10] of boolean; procedure prim; var i,j,k,min:longint; begin fillchar(g,sizeof(g),0); fillchar(f,sizeof(f),0); for i:=1 to n do d[i]:=g[1,i]; f[1]:=true; for i:=2 to n do begi

最小生成树(prim算法,Kruskal算法)c++实现

1.生成树的概念 连通图G的一个子图如果是一棵包含G的所有顶点的树,则该子图称为G的生成树. 生成树是连通图的极小连通子图.所谓极小是指:若在树中任意增加一条边,则将出现一个回路:若去掉一条边,将会使之变成非连通图. 生成树各边的权值总和称为生成树的权.权最小的生成树称为最小生成树. 2.最小生成树的性质用哲学的观点来说,每个事物都有自己特有的性质,那么图的最小生成树也是不例外的.按照生成树的定义,n 个顶点的连通网络的生成树有 n 个顶点.n-1 条边. 3.构造最小生成树,要解决以下两个问题