//最小生成树裸题,稠密图,用prim实现不用kruskal
#include#include #define MAX 110#define INF 1000*100+10int g[MAX][MAX];int n,m;int lowcoat[MAX],adj[MAX];void Prim(){ int i,j,k,min,ans; for(i=1; i<=n; i++) lowcoat[i]=g[1][i] , adj[i]=1; lowcoat[1]=0; for(i=1; i g[k][j]) lowcoat[j]=g[k][j] , adj[j]=k; } for(ans=0 , i=1; i<=n; i++) if(g[i][j=adj[i]]!=-1) ans+=g[i][j]; printf("%d\n",ans); return ;}int main(){ int i,j,a,b; while(scanf("%d",&n)!=EOF) { for(i=1; i<=n; i++) for(j=1; j<=n; j++) scanf("%d",&g[i][j]); scanf("%d",&m); for(i=1; i<=m; i++) { scanf("%d%d",&a,&b); g[a][b]=g[b][a]=-1; //不能改为0,会导致算法出错,仔细想想即可 }/* for(i=1; i<=n; i++) { for(j=1; j<=n; j++) printf("%d ",g[i][j]); printf("\n"); }*/ Prim(); } return 0;}