1116: Kingdoms+csuoj+暴力枚举+最小生成树
2014-09-04 18:19
399 查看
1116: Kingdoms
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 125 Solved: 33[Submit][Status][WebBoard]Description
A kingdom has n cities numbered 1 to n, and some bidirectional roads connecting cities. The capital is always city 1.After a war, all the roads of the kingdom are destroyed. The king wants to rebuild some of the roads to connect the cities, but unfortunately, the kingdom is running out of money. The total cost of rebuilding roads should notexceed K.Given the list of m roads that can be rebuilt (other roads are severely damaged and cannot be rebuilt), the king decided to maximize the total population in the capital and all other cities that are connected (directly or indirectly)with the capital (we call it "accessible population"), can you help him?Input
The first line of input contains a single integer T (T<=20), the number of test cases.Each test case 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), representing a destroyed road connecting city u and v, whose rebuilding cost is c.Note that two cities can be directly connected by more than one road, but a road cannot directly connect a city and itself.Output
For each test case, print the maximal accessible population.Sample Input
2 4 6 6 500 400 300 200 1 2 4 1 3 3 1 4 2 4 3 5 2 4 6 3 2 7 4 6 5 500 400 300 200 1 2 4 1 3 3 1 4 2 4 3 5 2 4 6 3 2 7
Sample Output
1100 1000
解决方案:此题可以先确定1是在点集里,然后暴力枚举其它城市是否要连,对每个枚举的结果求最小生成树,选出符合条件的最优解。
code:#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn=0x3f3f3f3f;int n,m,k;int population[20];int Map[20][20];bool exits[20];bool vis[20];int d[20];int p;void init(int n){for(int i=1; i<=n; i++){for(int j=1; j<=n; j++){Map[i][j]=(i!=j?maxn:0);}}}int prim(){int sum=0;memset(vis,false,sizeof(vis));for(int i=1; i<=n; i++){d[i]=Map[1][i];}vis[1]=true;for(int i=2; i<=n; i++){int min=maxn,mini;for(int j=1; j<=n; j++){if(!vis[j]&&exits[j]&&d[j]<min){min=d[j];mini=j;}}if(min==maxn) break;sum+=min;vis[mini]=true;for(int k=1; k<=n; k++){if(exits[k]&&!vis[k]&&Map[mini][k]<d[k]){d[k]=Map[mini][k];}}}int cnt1=0,cnt2=0;for(int i=1;i<=n;i++){cnt1+=exits[i];}for(int i=1;i<=n;i++){cnt2+=vis[i];}if(cnt1==cnt2)return sum;else return maxn;}void dfs(int cur){if(cur>n){int pr=prim();int temp=0;if(pr<=k){for(int i=1; i<=n; i++){if(exits[i]){temp+=population[i];}}if(temp>p) p=temp;}return ;}for(int i=0; i<2; i++){exits[cur]=i==1?true:false;dfs(cur+1);}}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&k);for(int i=1; i<=n; i++){scanf("%d",&population[i]);}init(n);for(int i=0; i<m; i++){int from,to,c;scanf("%d%d%d",&from,&to,&c);if(Map[from][to]>c){Map[from][to]=Map[to][from]=c;}}p=population[1];exits[1]=true;dfs(2);printf("%d\n",p);}return 0;}
相关文章推荐
- CSU 1116 Kingdoms(枚举最小生成树)
- csu 1116 Kingdoms 最小生成树 二进制枚举
- CSU 1116 Kingdoms 最小生成树(prim)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- csuoj 1116: Kingdoms
- CSU 1116 Kingdoms(Kruskal)(枚举)
- HDU-1598-find the most comfortable road(暴力枚举+Kruskal最小生成树)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- hud1598 find the mostcomfortable road最小生成树 暴力枚举
- CSU 1116 Kingdoms(Kruskal)(枚举)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- HDU 4081 Qin Shi Huang's National Road System(最小生成树+暴力枚举边)
- HDU 2489 Minimal Ratio Tree (暴力枚举+最小生成树)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- CSUOJ 1116 Kingdoms
- CSU 1541 There is No Alternative (最小生成树+枚举)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- CSU 1116 Kingdoms(Kruskal)(枚举)