QS Network (minimum spanning tree)

Source: Internet
Author: User

Test instructions: If the two QS between to want to connect the net, in addition to their network cable costs, both to buy adapters, so that 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.

Solution: With the prim algorithm, when constructing the adjacency matrix, the weights of I to J plus the weights of I and J points can be used.

Attach the AC code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #define Infinity 1000000#include< Iostream> #include <algorithm>using namespace std;int n;int g[501][501];int qs[1000];int lowcost[100000],    Closeset[100000];int used[100000];int Prim (int vcount) {int sum=0;    int i,j,k;    int min;        for (i=0; i<vcount; i++) {lowcost[i]=g[0][i];        closeset[i]=0;    used[i]=0;    } used[0]=1;        for (I=1; i<=vcount-1; i++) {j=0;        min = infinity; for (k=1; k<vcount; k++) if ((!used[k]) && (lowcost[k]<min)) {min =lowco                ST[K];            J=k;        } used[j]=1;        Sum+=min; for (k=1; k<vcount; k++) if (!used[k]&& (G[j][k]<lowcost[k])) {lowcost[                K]=G[J][K];            Closeset[k]=j; }} return sum;}    int main () {int t,i,j,n,sum;    scanf ("%d", &t);     while (t--) {   scanf ("%d", &n);        for (i=0;i<n;i++) scanf ("%d", &qs[i]);                for (i=0;i<n;i++) {for (j=0;j<n;j++) {scanf ("%d", &g[i][j]);            G[I][J]=G[I][J]+QS[I]+QS[J];        }} sum=prim (n);    printf ("%d\n", sum); } return 0;}

Prim algorithm See: http://www.cnblogs.com/PJQOOO/p/3855017.html. I was following this to learn the smallest spanning tree.

———— ANONYMOUS.PJQ

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.