(最小生成树)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

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 1000;
const int INF = 0xfffffff;

int n;
int J[N][N], dist[N];
bool vis[N];

int Prim()
{
    int i, j, ans=0;
    dist[1]=0;
    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        int index=1;
        int MIN=INF;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN && dist[j])
            {
                index=j;
                MIN=dist[j];
            }
        }
        vis[index]=1;
        ans += MIN;
        for(j=1; 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, 0, sizeof(J));
        memset(b, 0, sizeof(b));

        for(i=1; i<=n; i++)
        {
            scanf("%d", &a);
            b[i] = a;
        }

        for(i=1; i<=n; i++)
        for(j=1; j<=n; j++)
        {
            scanf("%d", &a);
            J[i][j] = a;
        }

        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            J[i][j] += b[i]+b[j];
        }

        int ans=Prim();

        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-06 04:52:52

(最小生成树)QS Network -- ZOJ --1586的相关文章

E - QS Network - zoj 1586(简单)

题意:在一个叫做QS的星系,他们使用一些特殊的通讯方式,两个人之间通讯需要使用一个网络适配器,但是一个网络适配器只能跟一个人联系,所有它连接几个人就需要及格适配器,而且每个人都有一些不同的偏好,喜欢的适配器的牌子也是不同的,现在让你求出来让QS人之间相互通讯最少需要多少代价? 输入第一行是测试数据组数,下面是一个N,代表N个人,下一样有N个数表示每个人喜欢的适配器的价格,接着一个矩阵,表示两点间通讯(电缆??)的价格. 分析:只要在每条边的权值上加上两个适配器的价格就可以了. *********

最小生成树-QS Network(Prim)

题目大意: 给出的案例结果得出步骤,如下图所示,从结点1开始查找,找出的一条路径如绿色部分所标注.(关键处在于连接每条路径所需要的适配器的价格得加上去) 代码实现: 1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 #define MAX 1000 5 //注意此处范围得按照题意设置为>=1000,否则会Segmentation Fault 6 #define MAXCOST 0x7fffffff

ZOJ 1586 QS Network(最小生成树 prim)

题意  输入n  然后输入n个数  代表连接时每个站点自身的消耗   然后输入n*n的矩阵  第i行第j列的数代表第i个站点和第j个站点之间路上的花费  链接i,j两个节点的总花费为两站点自身花费加上路上的花费  求⑩这n个站点连通的最小总花费 又是裸的最小生成树  给点的就用prim咯 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int N = 10

ZOJ 1586 QS Network (经典MST~Prim)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=586 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 n

ZOJ 1586 QS Network(kruskal)(基础)

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

QS Network(最小生成树)

题意:若两个QS之间要想连网,除了它们间网线的费用外,两者都要买适配器, 求使所有的QS都能连网的最小费用. 分析:这个除了边的权值外,顶点也有权值,因此要想求最小价值,必须算边及顶点的权值和. 解决方法:用prim算法,在构造邻接矩阵时,在i到j的权值的基础上再加上i点的权值和j点的权值即可. 附上AC代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #define infinity 1000

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

QS Network DescriptionIn 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 segm

ZOJ QS Network (prime_裸题)

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.

E - QS Network

E - QS Network 思路:最小生成树,数组不要开小了. #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 100010 using namespace std; int t,n,tot,sum,ans; int fa[MAXN],mon[MAXN]; struct nond{ int x,y,z; }v[1000050]; i