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. 解决webstorm卡顿问题

    webstorm强大的功能就不多做介绍了.但是它的缺点也显而易见:吃内存. 电脑配置稍低一点,运行webstorm就特别容易卡顿,特别是项目比较大的时候,那卡顿得不要不要的. 在我的笔记本8g内存 2 ...

  2. JavaScript函数表达式

    函数表达式的基本语法形式 var functionName = function(arguments){ //函数体 } 递归建议 我们通过例子来一步步说明,递归的最佳实现方式.下面是普通递归调用的例 ...

  3. SPX Instant Screen Capture

    Today I will recommend a NICE screen capture tool, which name is SPA Instant Screen Capture. http:// ...

  4. 建设移动端web开发会涉及到的meta标签

    <meta name="format-detection" content="telephone=no"> <meta name=" ...

  5. IOS ReactiveCocoa

    一 前提: 在iOS开发过程中,当某些事件响应时,需处理的某些业务逻辑 Eg. 按钮点击:action ScrollView滚动:delegate 属性值改变:KVO ReactiveCocoa为事件 ...

  6. VerifyCodeUtil.java

    package com.vcredit.framework.utils; import java.awt.Color;import java.awt.Font;import java.awt.Grap ...

  7. #20145205 《Java程序设计》第3周学习总结

    教材学习内容总结 1.java中的浮点数具体是由分数进行表示的,所以在有些时候,看起来很简单的基本运算,运行结果会让人很是不理解,原因是分数有时表示浮点数时无法完全恰好表示,可能会出现无限循环的情况. ...

  8. C# 根据年月日获取星期几方法

    #region 根据年月日计算星期几(Label2.Text=CaculateWeekDay(2004,12,9);) /// <summary> /// 根据年月日计算星期几(Label ...

  9. 利用xtrabackup备份mysql数据库

    利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...

  10. Android--Activity(活动)

    1. 安卓中的 Activity 大致等于桌面应用中的window 2. Activity 的生命周期由系统控制, 所以在开发时要假设 Activity 会被随时销毁掉的情况, 比如: 应用中有一个 ...