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