1 #include<iostream> 2 #include<stdio.h> 3 #define MAXN 500 4 #define inf 100000000 5 using namespace std; 6 int pre[MAXN]; 7 int _m[MAXN][MAXN]; 8 int low_cost[MAXN]; 9 int edge[8000][2]; 10 int q; 11 int w; 12 int prime(int n); 13 int main() 14 { 15 //freopen("acm.acm","r",stdin); 16 int num; 17 int i; 18 int j; 19 int u; 20 int v; 21 cin>>num; 22 for(i = 0; i < num; ++ i) 23 { 24 for(j = 0; j < num; ++ j) 25 { 26 cin>>_m[i][j]; 27 } 28 } 29 cin>>q; 30 for(i = 0; i < q; ++ i) 31 { 32 cin>>u>>v; 33 -- u; 34 -- v; 35 _m[u][v] = -1; 36 _m[v][u] = -1; 37 } 38 w = 0; 39 cout<<prime(num) + w<<endl; 40 } 41 42 int prime(int n) 43 { 44 int i; 45 int j; 46 int k; 47 int sum = 0; 48 int min; 49 for(i = 1; i < n; ++ i) 50 { 51 low_cost[i] = _m[0][i]; 52 pre[i] = 0; 53 } 54 for(i = 1; i < n; ++ i) 55 { 56 min = inf; 57 for(j = 1; j < n; ++ j) 58 { 59 if(low_cost[j]&&low_cost[j] < min) 60 { 61 k = j; 62 min = low_cost[j]; 63 } 64 } 65 if(low_cost[k] == -1) 66 ++ w; 67 sum += low_cost[k]; 68 low_cost[k] = 0; 69 for(j = 1; j < n; ++ j) 70 { 71 if(_m[k][j] < low_cost[j] && low_cost[j]) 72 { 73 low_cost[j] = _m[k][j]; 74 pre[j] = k; 75 } 76 } 77 } 78 return sum; 79 }