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. iOS--创建炫酷的渐变色界面

    { CAGradientLayer *_layer; } //创建渐变层 _layer =[CAGradientLayer layer]; _layer.frame=self.view.frame; ...

  2. 分布式缓存技术redis学习系列(二)——详细讲解redis数据结构(内存模型)以及常用命令

    Redis数据类型 与Memcached仅支持简单的key-value结构的数据记录不同,Redis支持的数据类型要丰富得多,常用的数据类型主要有五种:String.List.Hash.Set和Sor ...

  3. Java动态、重写 理解

    相关类: class A ...{ public String show(D obj)...{ return ("A and D"); } public String show(A ...

  4. CSS 百分比 margin & padding

    前段时间我同事对于margin和padding应用百分比值似乎有些误解,觉得可能是个普遍问题,所以觉得有必要拿出来单独写一下. margin和padding都可以使用百分比值的,但有一点可能和通常的想 ...

  5. makefile 笔记

    1.Makefile中命令前的@和-符号 如果make执行的命令前面加了@字符,则不显示命令本身而只显示它的结果; Android中会定义某个变量等于@,例如 hide:= @ 通常make执行的命令 ...

  6. MSSQL 跨服器调用存储过程

    A库 CREATE PROCEDURE [dbo].[A_P_Test] AS BEGIN SELECT * FROM dbo.A_LoadData END B库  在B中调用A库存储过程 注:是同一 ...

  7. uva10375 Choose and Divide(唯一分解定理)

    uva10375 Choose and Divide(唯一分解定理) 题意: 已知C(m,n)=m! / (n!*(m-n!)),输入整数p,q,r,s(p>=q,r>=s,p,q,r,s ...

  8. 修改ie的默认值 为ie10

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10" />

  9. 在AngularJs中怎么设置请求头信息(headers)及不同方法的比较

    在AngularJS中有三种方式可以设置请求头信息: 1.在http服务的在服务端发送请求时,也就是调用http()方法时,在config对象中设置请求头信息:事例如下: $http.post('/s ...

  10. 害死人不偿命的(3n+1)猜想

    卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数 ...