CSU 1116 kingdoms (Minimum Spanning Tree enumeration)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.