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 ;
}

UVA 12507 Kingdoms的更多相关文章

  1. UVa 109 - SCUD Busters(凸包计算)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  2. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  3. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  4. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  5. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  6. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  7. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  8. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  9. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. C/C++ 图像二进制存储与读取

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50782792 在深度学习时,制作样本数 ...

  2. 洛谷 P1131 [ZJOI2007]时态同步

    P1131 [ZJOI2007]时态同步   题目描述 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数字1,2,3….进行标号.电路板的各个节点由若干 ...

  3. 【剑指Offer学习】【面试题63:二叉搜索树的第k个结点】

    题目:给定一棵二叉搜索树,请找出当中的第k大的结点. 解题思路 假设依照中序遍历的顺序遍历一棵二叉搜索树,遍历序列的数值是递增排序的. 仅仅须要用中序遍历算法遍历一棵二叉搜索树.就非常easy找出它的 ...

  4. FFMpeg在Windows下搭建开发环境【转】

    本文转载自:http://blog.csdn.net/wootengxjj/article/details/51758621 版权声明:本文为博主原创文章,未经博主允许不得转载. FFmpeg 是一个 ...

  5. php中的self关键字和this关键字的区别和联系

    php中的self关键字和this关键字的区别和联系 面向对象编程(OOP,Object OrientedProgramming)现已经成为编程人员的一项基本技能.利用OOP的思想进行PHP的高级编程 ...

  6. netty结构

    一.先纵览一下Netty,看看Netty都有哪些组件? 为了更好的理解和进一步深入Netty,我们先总体认识一下Netty用到的组件及它们在整个Netty架构中是怎么协调工作的.Netty应用中必不可 ...

  7. BZOJ 4241 分块

    思路: 考虑分块 f[i][j]表示从第i块开头到j的最大值 cnt[i][j]表示从第i块开始到序列末尾j出现了多少次 边角余料处理一下就好啦~ //By SiriusRen #include &l ...

  8. FFmpeg 移植 Android

    近期项目需要解析苹果的HLS流媒体协议,而FFmpeg从0.11.1“Happiness”版本开始,才增加了对HLS协议的支持.目前网上关于FFmpeg编译移植的文章有很多,但大多都是对旧版本的说明. ...

  9. SQL*PLUS命令的使用大全

    Oracle的sql*plus是与oracle进行交互的客户端工具.在sql*plus中,可以运行sql*plus命令与sql*plus语句. 我们通常所说的DML.DDL.DCL语句都是sql*pl ...

  10. Android 数字四舍五入

    BigDecimal b = new BigDecimal(hour).setScale(1, BigDecimal.ROUND_HALF_UP); setScale(int newScale, in ...