Csuoj 1116:kingdoms

Source: Internet
Author: User

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116

1116:kingdoms Time limit:3 Sec Memory limit:64 MB
submit:293 solved:82
[Submit] [Status] [Web Board] DescriptionA kingdom have n cities numbered 1 to N, and some bidirectional roads connecting cities. the capital was always City 1.After a war, the roads of the kingdom is destroyed. The king wants to rebuild some's of the roads to connect the cities, but unfortunately, the kingdom was running out of money. The total cost of rebuilding roads should is not exceed K.Given The list of M roads that can is rebuilt (other roads is severely damaged and cannot be rebuilt), the King Decid Ed to maximize the total population in the capital and all other cities that is connected (directly or indirectly) with T He capital (we call it "accessible population"), can I help him?Inputthe first line of input contains a single integer T (t<=20), the number of test cases.Each test case is begins with three integers n (4<=n<=16), M (1<=m<=100) and K (1<=k<=100,000).the second line contains n positive integers pi (1<=pi<=10,000), the population of each city.Each of the following m lines contains three positive integers u, V, C (1<=u,v<=n, 1<=c<=1000), Representi Ng a destroyed road connecting City U and V, whose rebuilding cost is C.Note that the cities can is directly connected by more than one road, but a road cannot directly connect a city and it Self.Output

For each test case, print the maximal accessible population.

Sample Input
24 6 6500 400 300 2001 2 41 3 31 4 24 3 52 4 63 2 74 6 5500 400 300 2001 2 41 3 31 4 24 3 52 4 63 2 7
Sample Output
11001000
HINT Source

The computer program design competition of the university students in Hunan province

Analysis

This problem can be determined first 1 is in the point set, and then the violent enumeration of other cities to be connected, the results of each enumeration to find the minimum spanning tree, to select the best solution to match the conditions.

AC Code:

1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 Const intmaxn=0x3f3f3f3f;6 intn,m,k;7 intpopulation[ -];8 intmap[ -][ -];9 BOOLexits[ -];Ten BOOLvis[ -]; One intd[ -]; A intp; - voidInitintN) - { the      for(intI=1; i<=n; i++) -     { -          for(intj=1; j<=n; J + +) -         { +Map[i][j]= (I!=J?MAXN:0); -         } +     } A } at intPrim () - { -     intsum=0; -memset (Vis,false,sizeof(Vis)); -      for(intI=1; i<=n; i++) -     { ind[i]=map[1][i]; -     } tovis[1]=true; +      for(intI=2; i<=n; i++) -     { the  *         intmin=Maxn,mini; $          for(intj=1; j<=n; J + +)Panax Notoginseng         { -             if(!vis[j]&&exits[j]&&d[j]<min) the             { +min=D[j]; AMini=J; the             } +         } -         if(MIN==MAXN) Break; $sum+=min; $vis[mini]=true; -          for(intk=1; k<=n; k++) -         { the             if(exits[k]&&!vis[k]&&map[mini][k]<D[k]) -             {Wuyid[k]=Map[mini][k]; the             } -         } Wu  -  About     } $     intcnt1=0, cnt2=0; -      for(intI=1; i<=n;i++){ -cnt1+=Exits[i]; -     } A      for(intI=1; i<=n;i++){ +cnt2+=Vis[i]; the     } -     if(cnt1==Cnt2) $     returnsum; the     Else returnMAXN; the  the } the  - voidDfsintcur) in { the     if(cur>N) the     { About         intPr=Prim (); the         inttemp=0; the         if(pr<=k) the         { +              for(intI=1; i<=n; i++) -             { the                 if(Exits[i])Bayi                 { thetemp+=Population[i]; the                 } -             } -             if(temp>p) p=temp; the         } the         return ; the     } the      for(intI=0; i<2; i++) -     { theexits[cur]=i==1?true:false; theDFS (cur+1); the     }94 } the intMain () the { the     intT;98scanf"%d",&t); About      while(t--) -     {101scanf"%d%d%d",&n,&m,&k);102          for(intI=1; i<=n; i++)103         {104scanf"%d",&population[i]); the         }106 init (n);107          for(intI=0; i<m; i++)108         {109             int  from, To,c; thescanf"%d%d%d",& from,&to,&c);111             if(map[ from][to]>c) the             {113map[ from][to]=map[to][ from]=C; the             } the         } thep=population[1];117exits[1]=true;118Dfs2);119printf"%d\n", p); -     }121     return 0;122}
View Code

Csuoj 1116:kingdoms

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.