csuoj 1116: Kingdoms
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]
Description
Input
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
HINT
Source
分析;
此题可以先确定1是在点集里,然后暴力枚举其它城市是否要连,对每个枚举的结果求最小生成树,选出符合条件的最优解。
AC代码:
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=0x3f3f3f3f;
int n,m,k;
int population[];
int Map[][];
bool exits[];
bool vis[];
int d[];
int p;
void init(int n)
{
for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
Map[i][j]=(i!=j?maxn:);
}
}
}
int prim()
{
int sum=;
memset(vis,false,sizeof(vis));
for(int i=; i<=n; i++)
{
d[i]=Map[][i];
}
vis[]=true;
for(int i=; i<=n; i++)
{ int min=maxn,mini;
for(int j=; 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=; k<=n; k++)
{
if(exits[k]&&!vis[k]&&Map[mini][k]<d[k])
{
d[k]=Map[mini][k];
}
} }
int cnt1=,cnt2=;
for(int i=;i<=n;i++){
cnt1+=exits[i];
}
for(int i=;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=;
if(pr<=k)
{
for(int i=; i<=n; i++)
{
if(exits[i])
{
temp+=population[i];
}
}
if(temp>p) p=temp;
}
return ;
}
for(int i=; i<; i++)
{
exits[cur]=i==?true:false;
dfs(cur+);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&population[i]);
}
init(n);
for(int i=; 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[];
exits[]=true;
dfs();
printf("%d\n",p);
}
return ;
}
csuoj 1116: Kingdoms的更多相关文章
- CSU 1116 Kingdoms(枚举最小生成树)
题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...
- CSU 1116 Kingdoms
题意:给你n个城市,m条被摧毁的道路,每条道路修复需要c元,总共有k元,给你每个城市的人口,问在总费用不超过k的情况下 与1号城市相连的城市的最大总人口(包括1号城市) 思路:1号城市是必取的,剩余最 ...
- BZOJ 1116: [POI2008]CLO
1116: [POI2008]CLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 922 Solved: 514[Submit][Status][ ...
- csuoj 1511: 残缺的棋盘
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec 内存限制: 128 MB 题目描述 输入 ...
- light oj 1116 - Ekka Dokka
1116 - Ekka Dokka PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Ekka ...
- csuoj 1354: Distinct Subsequences
这个题是计算不同子序列的和: spoj上的那个同名的题是计算不同子序列的个数: 其实都差不多: 计算不同子序列的个数使用dp的思想: 从头往后扫一遍 如果当前的元素在以前没有出现过,那么dp[i]=d ...
- hdu 1116 Play on Words
http://acm.hdu.edu.cn/showproblem.php?pid=1116 欧拉通路和欧拉回路 #include <cstdio> #include <cstrin ...
- BZOJ 4057: [Cerc2012]Kingdoms( 状压dp )
状压dp.... 我已开始用递归结果就 TLE 了... 不科学啊...我dp基本上都是用递归的..我只好改成递推 , 刷表法 将全部公司用二进制表示 , 压成一个数 . 0 表示破产 , 1 表示没 ...
- nefu 1116 字符串加密
字符串加密 Problem : 1116 Time Limit : 1000ms Memory Limit : 65536K description 给你一段经过加密的字符串,我们称之为密文,现在请你 ...
随机推荐
- Is It A Tree?[HDU1325][PKU1308]
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- 《bootstrap》实战---小问题,大Bug
参照书中代码写了个示例,能够实现大页面单行导航,小页面显示收缩按钮,但是就是不能让收缩按钮发挥作用.也不知道哪儿出了问题. 想想算了,代码也不多,重新来吧.写道导航的时候,突然发现一个<nav& ...
- JavaScript之数组方法整理
Array概述 除了Object类型,最常用的类型: 实质:有序的数据列表, 特性:可以动态的调整数组的大小 创建数组的两种方式 构造函数创建方式 var arr = ...
- Python爬虫:Xpath语法笔记
一.选取节点 常用的路劲表达式: 表达式 描述 实例 nodename 选取nodename节点的所有子节点 xpath(‘//div’) 选取了div节点的所有子节点 / 从根节点选取 xpat ...
- Linux将中文目录改为英文
修改系统语言环境 1.修改系统当前语言环境 export LANG=en_US 2.更新家目录的文件命名 xdg-user-dirs-gtk-update 跳出对话框,提示是否更新成英文,同意即可 3 ...
- Notif
<?php/** * 支付宝通知接口 * @author YinKu.Alan * @date 2016-03-06 */class NotifyAction extends CommonAct ...
- java classpath getResource getResourceAsStream
1.classpath 用于指定java运行时,jvm寻找class文件以及jar文件的存储目录.jvm依据classpath中出现的jar文件以及目录,依次寻找,直到找到指定class文件. 例:j ...
- laravel DB事物
public function store(Request $request, $id) { $externalAccount = ExternalAccounts::find($id); DB::b ...
- backbonejs
前言: backbone由以下模块组成. 一.Event 监听事件,自定义事件.绑定到任何对象. http://www.css88.com/doc/backbone/#Events 这个是下面模块核心 ...
- 基于apache的tomcat负载均衡和集群配置
最近不是很忙,用零碎时间做点小小的实验. 以前公司采用F5负载均衡交换机,F5将请求转发给多台服务器,每台服务器有多个webserver实例,每个webserver分布在多台服务器,交叉式的分布集群. ...