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;}