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. 香蕉派 Banana pi BPI-M2 四核开源单板计算机.

     Banana PI BPI-M2 是一款四核高性能单板计算机,Banana PI BPI-M2是一款比树莓派更强悍的四核Android4.4产品. Banana PI BPI-M2兼容性强大,能 ...

  2. 在WPF的MVVM框架中获取下拉选择列表中的选中项

    文章概述: 本演示介绍怎样在WPF的MVVM框架中.通过数据绑定的方式获取下拉列表中的选中项.程序执行后的效果例如以下图所看到的: 相关下载(代码.屏幕录像):http://pan.baidu.com ...

  3. Python学习笔记-小记

    1.字符串string 推断一个字符(char)是数字还是字母 str.isalpha() #推断是否为字母 str.isdigit() #推断是否为数字 推断一个字符串是否为空 if not str ...

  4. HDOJ 4944 FSF’s game

    http://blog.csdn.net/keshuai19940722/article/details/38519681 不明真相的补一发... FSF's game Time Limit: 900 ...

  5. 2016.03.02,英语,《Vocabulary Builder》Unit 03

    ambi/amphi: 指on both sides或者around的意思,ambi-来自拉丁语,amphi-来自希腊语.ambidextrous:[ˌæmbi'dekstrəs] adj. 两手俱利 ...

  6. 错误 'Cannot run program "/home/uv/IDE/adt/sdk/platform-tools/adb": error=2, No such file or directory

    转 Linux下Android SDK中adb找不到的解决方案 2013年04月22日 20:41:48 阅读数:7621 在Linux平台下配置Android SDK开发环境过程中,Eclipse会 ...

  7. 2017-3-4 leetcode 414 485 495

    虽说周末要早起来着,但是日子过得有点奇怪,一不小心就忘掉了... leetcode414 https://leetcode.com/problems/third-maximum-number/?tab ...

  8. WebRTC开源项目一览之二

    .Kurento视频直播系统4.1  应用实例搞视频会议就会涉及一对多.多对多.广播.转码.混音.合屏.录制,这就需要用到流媒体服务器,而kurento就具有这些功能.他主要用来作为webrtc的流媒 ...

  9. 由GPS坐标计算半径

    在实际应用当中,一般是通过一个个体的编码来查找该编码对应的地区中心的经纬度,然后再根据这些经纬度来计算彼此的距离,从而估算出某些群体之间的大致距离范围(比如酒店旅客的分布范围-各个旅客的邮政编码对应的 ...

  10. php-fpm配置笔记

    php-fpm配置不当,导致服务器经常出现502错误,上个学期多次调整都没有解决,网上找来资料,大都是增加max_children,可是我都加到顶了,php-fpm log里面还是有大量的警告: ee ...