D - Kingdoms

Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

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

Output for Sample Input

1100
1000

解题:枚举点,注意题意,最大人口不是指完全直接与1相连的点的人口,间接的也算

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int g[maxn][maxn],d[maxn],p[maxn];
int n,m,k,cost,pu;
void prim(int b) {
int i,vis[maxn] = {};
for(i = ; i <= n; ++i) d[i] = INF;
cost = pu = i = ;
while(b) {
vis[i+] = b&;
b >>= ;
++i;
}
vis[] = ;
d[] = ;
while(true) {
int minv = INF,index = -;
for(int i = ; i <= n; ++i)
if(vis[i] == && d[i] < minv) minv = d[index = i];
if(index == - || minv == INF) break;
cost += minv;
vis[index] = ;
for(int i = ; i <= n; ++i){
if(vis[i] == && d[i] > g[index][i]){
d[i] = g[index][i];
}
}
}
for(int i = ; i <= n; ++i) if(vis[i] == ) pu += p[i];
}
int main() {
int t,u,v,w;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&n,&m,&k);
for(int i = ; i <= n; ++i)
scanf("%d",p+i);
for(int i = ; i <= n; ++i)
for(int j = ; j <= n; ++j)
g[i][j] = INF;
for(int i = ; i < m; ++i){
scanf("%d %d %d",&u,&v,&w);
if(w < g[u][v]) g[u][v] = g[v][u] = w;
}
int maxp = ;
for(int i = ; i < (<<n); ++i){
prim(i);
if(cost <= k) maxp = max(maxp,pu);
}
printf("%d\n",maxp);
}
return ;
}

最新文章

  1. 【DP】HDU 1260
  2. hammer.js实现背景图手势缩放调整位置
  3. 分享个win平台cocos2d-x创建项目的快捷方式
  4. bzoj 3270 博物馆(高斯消元)
  5. MyBatis,动态传入表名,字段名的解决办法
  6. [Xcode使用 - 3] 复制Xcode5.1.1中的项目模板到Xcode6.1
  7. 链表C++模板实现
  8. Table view 备忘
  9. 再探Java基础——String.format(String format, Object… args)的使用
  10. github教程--廖雪峰的官方网站
  11. POJ 3348 Cows [凸包 面积]
  12. Maven 学习总结 (七) 之 灵活构建
  13. 软件工程(GZSD2015) 第二次作业文档模板
  14. MFC关于.rc文件 .rc2文件
  15. Debian stretch更换国内源
  16. Python——通过用户cookies访问微博首页
  17. totastmessage 触发事件后浮框消失的方法
  18. db2 backup export
  19. Python知识点整理,基础3 - 字典操作
  20. Architecture

热门文章

  1. mobile touch 备用
  2. PatentTips - Optimizing power usage by factoring processor architectural events to PMU
  3. 页面安装Jre
  4. poi判断一行是隐藏的getZeroHeight()
  5. 两列等高布局 padding+margin的负值 CSS布局奇淫技巧之-多列等高
  6. 怎样删除 Windows.old 目录
  7. 机器学习 数据量不足问题----1 做好特征工程 2 不要用太多的特征 3 做好交叉验证 使用线性svm
  8. nyoj--16--矩形嵌套(动态规划)
  9. Oracle 11G R2 用exp无法导出空表解决方法
  10. LigerUI 单独调用插件使用注意项