CSU 1116 Kingdoms
来源:互联网 发布:手机淘宝投诉电话多少 编辑:程序博客网 时间:2024/05/23 12:29
1116: Kingdoms
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 339 Solved: 103
[Submit][Status][Web Board]
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 not exceed 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
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
这道题乍一看么什么思路,但是一看到点只有16个!所以果断枚举点!!因为想从边下手,然而边最多的时候有100条,所以如果每个点储存一个边的链表再去广搜遍历的时候就会有好多重复的情况,复杂度差不多是斐波那契的复杂度,所以果断放弃。
那现在谈谈怎么枚举点,很容易想到用01串来枚举,最多有16个点,所以就可以从0枚举得到1<<16,数量级都不到10的6次方,所以肯定行的,对每种情况进行一次最小生成树处理,最坏是16的平方,所以总的复杂度使程序在1秒内跑过是没问题的。那废话不多说,直接贴出代码,相应解释有注释说明:
#include <iostream>#include <algorithm>#include <iterator>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>using namespace std;#define INF 0x3f3f3f3fint map[20][20],people[20],low[20];int n,m,p;int vis[20];int prim(int t,int& sum){ memset(vis,0,sizeof(vis)); int pos,price,tot; pos=1; price=0; tot=0; for(int i=1;i<=n;i++)//寻找每种情况包含的点 { vis[i]=t&1; t=(t>>1); if(vis[i]) tot++;//记录有多少点 } if(vis[1]==0) tot++; for(int i=1;i<=n;i++) { if(vis[i]) low[i]=map[pos][i]; else low[i]=INF; } low[1]=0; vis[1]=2; tot--;//只需要将tot个点进行最小生成树就行了 int MIN; for(int k=1;k<=tot;k++) { MIN=INF; pos=1; for(int i=1;i<=n;i++) { if(vis[i]==1&&MIN>low[i]) { MIN=low[i]; pos=i; } } if(pos==1) return INF;//如果存在点扩展不到,就果断放弃这种情况 price+=low[pos]; sum+=people[pos]; vis[pos]=2; for(int i=1;i<=n;i++) { if(vis[i]==1&&map[pos][i]<low[i]) low[i]=map[pos][i]; } } return price;}int main(){// freopen("s","r",stdin); int T; scanf("%d",&T); while(T--) { memset(map,0x3f,sizeof(map)); scanf("%d%d%d",&n,&m,&p); for(int i=1;i<=n;i++) scanf("%d",&people[i]);//记录每个城市的人数 for(int j=1;j<=m;j++) { int u,v,c; scanf("%d%d%d",&u,&v,&c); if(c<map[u][v])//处理重边,保留花费最少的边 { map[u][v]=map[v][u]=c; } } int price,sum,res=0;// cout<<(1<<(n-1))<<endl; for(int i=0;i<=(1<<(n));i++)//枚举点,对每个情况找一次最小生成树 { sum=people[1]; price=prim(i,sum);//返回总花费 if(price<=p) res=max(res,sum);//更新最大的人数 } printf("%d\n",res); } return 0;}
0 0
- CSU 1116 Kingdoms
- CSU 1116 Kingdoms
- CSU 1116: Kingdoms(状态压缩+DP)
- CSU 1116 Kingdoms(Kruskal)(枚举)
- csu 1116 Kingdoms 最小生成树 二进制枚举
- CSUOJ 1116 Kingdoms
- Kingdoms
- 1116: Kingdoms+csuoj+暴力枚举+最小生成树
- Kingdoms UVA
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- CSU
- Android 中的进度条示例 ProgressDialog
- POJ 3862 Asteroids(两个三维凸包的重心到表面最短距离和)
- Matlab添加yalmip
- 关于mini2440LCD模块总结
- 1708: A - B
- CSU 1116 Kingdoms
- Linux系统进程管理及相关操作函数
- proto.c:3640: failed assertion "idx >=0 && idx < num_tree_types"
- UIView(animation)
- C语言进阶:整型提升
- 设计模式之代理模式
- 为什么会有作弊,会有为自己的家人安排工作?
- LeetCode-Decode Ways
- Ubuntu Kylin 15.04 support Meizu MX5 adb test