This problem is still the minimum spanning tree template can be solved, but on the side of the value of the above to be processed slightly.
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int n=1000;const int inf=1<<28;int cost[n][n],mincost[n],brand[n];bool used[N];int n;int Prim () {for (in T i=1;i<=n;i++) {mincost[i]=inf; Used[i]=false; } mincost[1]=0; int res=0; while (true) {int _min=inf,u=-1; for (int i=1;i<=n;i++) if (!used[i]&&_min>mincost[i]) _min=mincost[i],u=i; if (u==-1) break; Used[u]=true; Res+=_min; for (int i=1;i<=n;i++) mincost[i]=min (Mincost[i],cost[u][i]); } return res; int main () {int t; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=1;i<=n;i++) scanf ("%d", &brand[i]); for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {scanf ("%d", &cost[i][j]); Cost[i][j]+=brand[i]+BRAND[J]; } int Ans=prim (); printf ("%d\n", ans); } return 0;}
ZOJ 1586 QS Network