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. nyoj 20水

    #include<stdio.h> #include<string.h> #define N 110000 struct node { int u,v,next; }bian[ ...

  2. lpad&amp;rpad

    lpad( string, padded_length, [ pad_string ] ) string: 准备被填充的字符串 padded_length: 填充之后的字符串长度 pad_string ...

  3. shell文本过滤编程(十一):paste命令

    [版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com] 从字面上能够看出.paste命令和cut命令功能相反,cut命 ...

  4. java中文件路径读取

    windows下 1)相对路径 public static final String TestDataExcelFilePath="src/omstestdata.xlsx"; 2 ...

  5. 基于servlet实现一个web框架

    servlet作为一个web规范.其本身就算做一个web开发框架,可是其web action (响应某个URI的实现)的实现都是基于类的,不是非常方便,而且3.0之前的版本号还必须通过web.xml配 ...

  6. python spark kmeans demo

    官方的demo from numpy import array from math import sqrt from pyspark import SparkContext from pyspark. ...

  7. nyoj--74--小学生算术(水)

    小学生算术 时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述 很多小学生在学习加法时,发现"进位"特别容易出错.你的任务是计算两个三位数在相加时需要多少 ...

  8. Java-MyBatis:MyBatis3 | 日志

    ylbtech-Java-MyBatis:MyBatis3 | 日志 1.返回顶部 1. 日志 Mybatis 的内置日志工厂提供日志功能,内置日志工厂将日志交给以下其中一种工具作代理: SLF4J ...

  9. java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "getClassLoader")

    转自:https://blog.csdn.net/bluecard2008/article/details/80921682?utm_source=blogxgwz0 摘要: 今天在使用jetty做容 ...

  10. Centos7 minimal 系列之Nginx搭建(三)

    一.安装nginx 1.1.安装依赖包 yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-d ...