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];
int cmp(nond a,nond b){
    return a.z<b.z;
}
int find(int x){
    if(fa[x]==x)    return x;
    else return fa[x]=find(fa[x]);
}
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)    scanf("%d",&mon[i]);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++){
                int x;scanf("%d",&x);
                v[++tot].x=i;v[tot].y=j;v[tot].z=x+mon[i]+mon[j];
            }
        sort(v+1,v+1+tot,cmp);
        for(int i=1;i<=n;i++)    fa[i]=i;
        for(int i=1;i<=tot;i++){
            int dx=find(v[i].x);
            int dy=find(v[i].y);
            if(dx==dy)    continue;
            sum++;fa[dy]=dx;
            ans+=v[i].z;
            if(sum==n-1)    break;
        }
        cout<<ans<<endl;ans=0;sum=0;tot=0;
    }
}

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8461186.html

时间: 2024-09-30 09:38:14

E - QS Network的相关文章

FZU 1096 QS Network

QS Network Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on FZU. Original ID: 1096 64-bit integer IO format: %I64d      Java class name: Main In the planet w-503 of galaxy cgb, there is a kind of intelligent creature named QS.

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

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.

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

E - QS Network - zoj 1586(简单)

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

zoj 1586 QS Network

最小生成树,刚刚学了Prim算法. 对每条边变的权值进行预处理,c[i][j] = c[i][j] + p[i] + p[j] 其中c[i][j]为输入的权值,p[i],p[j]为连接这两个节点所需的费用. #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 1010; int c[max