Zoj 1586 QS Network

Source: Internet
Author: User

Links: Zoj 1586

Test instructions: If the two QS between to want to connect the net, in addition to their network cable costs, both to buy adapters,

All QS can be connected to the minimum cost of the network

Analysis: In addition to the edge of the weights, vertices are also entitled to value, so to find the minimum value, must calculate the edge and vertex weights and

#include <cstdio> #include <algorithm>using namespace std;int f[1010],n,m;struct stu{int a,b,c;} T[500100];int cmp (struct stu x,struct Stu y) {return x.c<y.c;}    int find (int x) {if (x!=f[x]) F[x]=find (f[x]); return f[x];}    int Krus () {int i,k=0,s=0,x,y;        for (i=1;i<m;i++) {x=find (T[I].A);        Y=find (T[I].B);            if (x!=y) {s+=t[i].c;            k++;            if (k==n-1) break;        F[x]=y; }} return s;}    int main () {int t,i,j,a[1010],c,s=0;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);            for (i=1;i<=n;i++) {f[i]=i;        scanf ("%d", &a[i]);        } m=1;                for (i=1;i<=n;i++) for (j=1;j<=n;j++) {scanf ("%d", &c);                    if (j<i) {t[m].a=i;                    T[m].b=j;        T[M++].C=C+A[I]+A[J]; Calculates the weights and} sort for edges and vertices (t+1,t+m,cmp);        S=krus ();    printf ("%d\n", s); } return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.