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 给你一段经过加密的字符串,我们称之为密文,现在请你 ...
随机推荐
- Linux 查杀病毒的常见命令
1. 查看异常连接的网络端口及其对应的相应的进程 netstat -anlp | grep EST 2.看下相关的进程ID对应的可执行文件的位置 ps 2393 可以看到进程的可执行文件在哪? 3.临 ...
- nginx 直接在配置文章中设置日志分割
直接在nginx配置文件中,配置日志循环,而不需使用logrotate或配置cron任务.需要使用到$time_iso8601 内嵌变量来获取时间.$time_iso8601格式如下:2015-08- ...
- Constructing Roads——F
F. Constructing Roads There are N villages, which are numbered from 1 to N, and you should build som ...
- 快排查找第K小的数
#include "iostream.h" using namespace std; int findMedian(int *A,int left,int right){ int ...
- [html] 有利于seo优化的div+css命名规范
搜索引擎优化(seo)对命名规范有很多要求,下面是我收集的一些当下主流的命名(还是比较常用的): CSS样式命名 说明 网页公共命名 #wrapper 页面外围控制整体布局宽度 #container或 ...
- Hive_数据倾斜处理
Hive中三种join map join (小表join大表,将小表加入到内存) 设置map join: hive.auto.convert.join=true hive.mapjoin.smallt ...
- Mysql 中的事件//定时任务
什么是事件 一组SQL集,用来执行定时任务,跟触发器很像,都是被动执行的,事件是因为时间到了触发执行,而触发器是因为某件事件(增删改)触发执行: 开启事件 查看是否开启: show variables ...
- dede判断当前文章
<li><a href="/info/info3.html" class=s >企业文化 </a></li><li> ...
- WM_COPYDATA进程间通信方案
连续在两个公司使用WM_COPYDATA实现进程间通信了,整理一下 具体步骤: 一. 进程A通过ShellExecute启动进程B, 将用于通信的窗口句柄hWndA(已强转为int值)通过命令行参 ...
- FJNU 1196 汪老司机(DP or 建图+最短路)
1196: 汪老司机 Time Limit: 1000 MS Memory Limit: 257792 KB 64-bit interger IO format: %lld ...