Question link: http://acm.csu.edu.cn/OnlineJudge/problem.php? Id = 1116
Solution Report: There are n cities in a country, M roads can be repaired, and each road requires a certain amount of gold coins. Now there are only K gold coins in this country, and each city has some people, ask how to build the road so that the total cost is within K and the population of the city connected to the capital (including the population of the capital) is the most.
Enumerate which cities are connected and construct the Minimum Spanning Tree.
1 # include 2 # include 3 # include 4 # include 5 using namespace STD; 6 const int maxn = 20; 7 int pop [maxn], mat [maxn] [maxn]; 8 int t, n, m, K; 9 10 int prim (int d, Int & ANS) 11 {12 INT visit [maxn], F = 1; 13 memset (visit, 0, sizeof (visit); 14 d = (d <1) + 1; 15 For (INT I = 1; I <= N; ++ I) 16 {17 visit [I] = D & 1; 18 d >>=1; 19} 20 int tot = 0; 21 visit [1] = 2; // equal to 2 is 22 while (1) 23 {24 int L =-1, temp = 0x7fffff; 25 for (INT I = 1; I <= N; ++ I) 26 if (visit [I] = 2) 27 {28 for (Int J = 1; j <= N; ++ J) 29 If (visit [J] = 1 & mat [I] [J] 100000) break; 36 tot + = temp; 37 visit [l] = 2; 38} 39 ans = 0; 40 for (INT I = 1; I <= N; ++ I) 41 if (visit [I] = 2) 42 ans + = pop [I]; 43 return tot; 44} 45 46 int main () 47 {48 scanf ("% d", & T ); 49 while (t --) 50 {51 scanf ("% d", & N, & M, & K); 52 for (INT I = 1; I <= N; ++ I) 53 scanf ("% d", & pop [I]); 54 int x, y, z; 55 memset (MAT, 0x3f, sizeof (MAT); 56 while (M --) 57 {58 scanf ("% d", & X, & Y, & Z ); 59 mat [x] [Y] = mat [y] [x] = min (MAT [x] [Y], Z); 60} 61 int tot = 0, T; 62 for (INT I = 0; I <= (1 <(n-1)-1; ++ I) 63 {64 int temp = prim (I, T ); 65 if (temp <= k) tot = max (TOT, T); 66} 67 printf ("% d \ n", TOT); 68} 69 return 0; 70}
View code
CSU 1116 kingdoms (Minimum Spanning Tree enumeration)