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

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

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的更多相关文章

  1. CSU 1116 Kingdoms(枚举最小生成树)

    题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116 解题报告:一个国家有n个城市,有m条路可以修,修每条路要一定的金币,现在这个国家只 ...

  2. CSU 1116 Kingdoms

    题意:给你n个城市,m条被摧毁的道路,每条道路修复需要c元,总共有k元,给你每个城市的人口,问在总费用不超过k的情况下 与1号城市相连的城市的最大总人口(包括1号城市) 思路:1号城市是必取的,剩余最 ...

  3. BZOJ 1116: [POI2008]CLO

    1116: [POI2008]CLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 922  Solved: 514[Submit][Status][ ...

  4. csuoj 1511: 残缺的棋盘

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1511 1511: 残缺的棋盘 时间限制: 1 Sec  内存限制: 128 MB 题目描述 输入 ...

  5. light oj 1116 - Ekka Dokka

    1116 - Ekka Dokka   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Ekka ...

  6. csuoj 1354: Distinct Subsequences

    这个题是计算不同子序列的和: spoj上的那个同名的题是计算不同子序列的个数: 其实都差不多: 计算不同子序列的个数使用dp的思想: 从头往后扫一遍 如果当前的元素在以前没有出现过,那么dp[i]=d ...

  7. hdu 1116 Play on Words

    http://acm.hdu.edu.cn/showproblem.php?pid=1116 欧拉通路和欧拉回路 #include <cstdio> #include <cstrin ...

  8. BZOJ 4057: [Cerc2012]Kingdoms( 状压dp )

    状压dp.... 我已开始用递归结果就 TLE 了... 不科学啊...我dp基本上都是用递归的..我只好改成递推 , 刷表法 将全部公司用二进制表示 , 压成一个数 . 0 表示破产 , 1 表示没 ...

  9. nefu 1116 字符串加密

    字符串加密 Problem : 1116 Time Limit : 1000ms Memory Limit : 65536K description 给你一段经过加密的字符串,我们称之为密文,现在请你 ...

随机推荐

  1. Delphi基本类型--枚举 子界 集合 数组

    [plain] view plain copy <strong>根据枚举定义集合 </strong> TMyColor = (mcBlue, mcRed); TMyColorS ...

  2. QuickTest Professionar 快捷键

    新建Test – New Test CTRL + N录制 – Record F3打开Test – Open Test CTRL + O新建业务组件 – Business Component > ...

  3. 实践:Backbone作前端,Django+Tastypie作后端的简单Web在线聊天室

    一.界面设计: 二.数据模型设计 id 每个发言都有一个独立的id由tastypie自动生成 content 发言的内容 username 发言者 date 发言时间 三.前端制作 这里没有用到Bac ...

  4. Official online document, install svn server in centOS

    http://www.krizna.com/centos/install-svn-server-on-centos-6/

  5. [软件推荐]Windows文件夹多标签工具Clover

    Clover 是 Windows Explorer 资源管理器的一个扩展,为其增加类似谷歌 Chrome 浏览器的多标签页功能,目前最新版本为:3.1.7 Clover 把 Chrome 标签页有的样 ...

  6. A*算法——启发式搜索

    A*算法 本质还是搜索:加了优化而已 关于这个优化,听到两种说法: 1.剪枝 通过判断预计最少还要几步,加强版剪枝 比如说一个经典剪枝: 如果 步数≥已知最小值 则 剪枝 升级| V 如果 步数+最少 ...

  7. MongoDB基本使用

    成功启动MongoDB后,再打开一个命令行窗口输入mongo,就可以进行数据库的一些操作. 输入help可以看到基本操作命令: show dbs:显示数据库列表 show collections:显示 ...

  8. JVM中,对象在内存中的布局

    在hotSpot虚拟机中,对象在内存中的布局可以分成对象头.实例数据.对齐填充三部分. 对象头:主要包括: 1.对象自身的运行行元数据,比如哈希码.GC分代年龄.锁状态标志等,这部分长度在32位虚拟机 ...

  9. 局域网内利用gitlab,jenkins自动生成gitbook并发布(nginx)

    安装了GitBook,内网使用,没法用上gitbook的网页. 用gitbook serve只能展示一本书,而且也不利于长期维护. 于是使用gitlab,jenkins,和nginx配合gitbook ...

  10. PHP+Zend 输出时中文乱码问题

    1.把输入的格式改成 echo iconv("GB2312","UTF-8",'我爱PHP'); 2.其他的方法,还不会用,有待完善........