CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116
解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只有K个金币,每个城市有一些人,要你求怎么修路使得总的费用在K的范围内,同时使得跟首都连接的城市的人口(包括首都的人口)要最多,问最多的人口是多少。
枚举连接哪些城市,然后分别构造最小生成树。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = ;
int pop[maxn],mat[maxn][maxn];
int T,n,m,k; int prim(int d,int& ans)
{
int visit[maxn],f = ;
memset(visit,,sizeof(visit));
d = (d << ) + ;
for(int i = ;i <= n;++i)
{
visit[i] = d & ;
d >>= ;
}
int tot = ;
visit[] = ; //等于2才是在集合中的
while()
{
int l = -,temp = 0x7fffffff;
for(int i = ;i <= n;++i)
if(visit[i] == )
{
for(int j = ;j <= n;++j)
if(visit[j] == && mat[i][j] < temp)
{
l = j;
temp = mat[i][j];
}
}
if(l == - || temp > ) break;
tot += temp;
visit[l] = ;
}
ans = ;
for(int i = ;i <= n;++i)
if(visit[i] == )
ans += pop[i];
return tot;
} int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i = ;i <= n;++i)
scanf("%d",&pop[i]);
int x,y,z;
memset(mat,0x3f,sizeof(mat));
while(m--)
{
scanf("%d%d%d",&x,&y,&z);
mat[x][y] = mat[y][x] = min(mat[x][y],z);
}
int tot = ,t;
for(int i = ;i <= ( << (n-))-;++i)
{
int temp = prim(i,t);
if(temp <= k) tot = max(tot,t);
}
printf("%d\n",tot);
}
return ;
}
CSU 1116 Kingdoms(枚举最小生成树)的更多相关文章
- CSU 1116 Kingdoms
题意:给你n个城市,m条被摧毁的道路,每条道路修复需要c元,总共有k元,给你每个城市的人口,问在总费用不超过k的情况下 与1号城市相连的城市的最大总人口(包括1号城市) 思路:1号城市是必取的,剩余最 ...
- csuoj 1116: Kingdoms
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 1116: Kingdoms Time Limit: 3 Sec Memory Limit ...
- UVA1395 Slim Span(枚举最小生成树)
题意: 求最小生成树中,最大的边减去最小的边 最小值. 看了题解发现真简单=_= 将每条边进行从小到大排序,然后从最小到大一次枚举最小生成树,当构成生成树的时候,更新最小值 #include < ...
- Poj(2784),二进制枚举最小生成树
题目链接:http://poj.org/problem?id=2784 Buy or Build Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- HDU 2489 Minimal Ratio Tree(dfs枚举+最小生成树)
想到枚举m个点,然后求最小生成树,ratio即为最小生成树的边权/总的点权.但是怎么枚举这m个点,实在不会.网上查了一下大牛们的解法,用dfs枚举,没想到dfs还有这么个作用. 参考链接:http:/ ...
- HDU 2489 Minimal Ratio Tree (DFS枚举+最小生成树Prim)
Minimal Ratio Tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- 【HDOJ1598】【枚举+最小生成树】
http://acm.hdu.edu.cn/showproblem.php?pid=1598 find the most comfortable road Time Limit: 1000/1000 ...
- hdu 2489(枚举 + 最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 思路:由于N, M的范围比较少,直接枚举所有的可能情况,然后求MST判断即可. #include ...
- hdu1598 find the most comfortable road 枚举+最小生成树
#include<cstdio> #include<cstring> #include<algorithm> #define MAXN 210 #define IN ...
随机推荐
- POJ3468A Simple Problem with Integers(区间加数求和 + 线段树)
题目链接 题意:两种操作:一是指定区间的数全都加上一个数,二是统计指定区间的和 参考斌神的代码 #include <iostream> #include <cstring> # ...
- break; continue; goto; return在循环中的应用
1. break表示跳出循环,程序指向循环体后的第一条语句: ; ) { ) break; console.writeline("{0}",i++); } console.read ...
- 15款优秀移动APP产品原型设计工具
一新来小盆友问:“移动产品原型设计都用啥工具?” 答:“@#¥……&%*” 又问:“能详细说下各个工具吗?我比较一下” “……” 好吧,谁让我那么的爱分享而你又是小美女呢 ———————正文开 ...
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【九】——API变了,客户端怎么办?
系列导航地址http://www.cnblogs.com/fzrain/p/3490137.html 前言 一旦我们将API发布之后,消费者就会开始使用并和其他的一些数据混在一起.然而,当新的需求出现 ...
- 计算div里面li个数
方式一:js var content=document.getElementById("content"); alert(document.getElementsByTagName ...
- yum配置文件详解
yum是什么: Yellow dog Updater, Modified主要功能是更方便的添加/删除/更新RPM包,自动解决包的倚赖性问题,它能便于管理大量系统的更新问题. yum特点:可以同时配置多 ...
- 免 sudo 使用 docker
免 sudo 使用 docker 如果还没有 docker group 就添加一个: sudo groupadd docker 将用户加入该 group 内.然后退出并重新登录就生效啦. sudo g ...
- VS代码段扩展Snippet Designer is a Visual Studio plug in which allows you to create and search for snippets inside the IDE
Snippet Designer is a Visual Studio plug in which allows you to create and search for snippets insid ...
- Linux python <tab>自动补全
为Python添加交互模式下TAB自动补全以及命令历史功能. 1.获取python目录 [root@localhost ~]# python Python 2.6.6 (r266:84292, Jul ...
- yii2中事务不能回滚的解决
$player1 = Player::findOne(1); $player1->nickname = '111'; $player2 = Player::findOne(2); $player ...