UVa 12507 - Kingdoms(状压dp)

湖南省第八届大学生计算机程序设计竞赛试题 湖南省第八届ACM大学生计算机程序设计竞赛试题,湖南省第八届ACM大学生计算机程序设计竞赛试题;湖南省第八届ACM大学生计算机程序设计竞赛试题; 立即下载
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

题目大意, 给你个n点, m条边的无线图, 顶点1为首都, 每个点的权值为人口数, 每条边的权值为修复此道路的花费, 让你得出花费不超过k的情况下, 以首都为起点的最小生成树所包含的最大人口数。

看了别人的博客, 想了一晚上, 算是搞懂了一些。 基本思路就是暴力搜索, 顶点数为16, 可以用int类型的每一个二进制位表示所有顶点的全部组合形式, 然后验证每一种形式的可行性,最后得出一个最大解。

#include<bits/stdc++.h>
using namespace std;
int pre[20];//并查集
struct node
{
    int v;
    int u;
    int w;
} edge[111]; //边集
int num[20];//存每一点人口数
int k, n, m;
int vis[20];//标记出每一种组合形式
void mix(int a, int b)
{
    pre[a] = b;
}
int Find(int a)
{
    int t = a;
    while(pre[a] != a)
    {
        a = pre[a];
    }
    while(t != a)//并查集优化, 这个循环可以去掉
    {
        int tt = pre[t];
        pre[t] = a;
        t = tt;
    }
    return a;
}
bool cmp(struct node a, struct node b)
{
    return a.w <= b.w;
}
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d%d", &n, &m, &k);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &num[i]);
        }
        for(int i = 0; i < m; i++)
        {
            scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].w);
        }
        sort(edge,edge + m, cmp);
        int len = (1 << n);//将所有组合压缩到 0 - len- 1
        int max_p = -1;//存最大解
        for(int i = 0; i < len; i++)
        {
            int v_cnt = 0;//存每一种组合所包含的点数
            for(int j = 0; j < n; j++)//验证第j + 1个点在第i种组合中是否要连通
            {
                if((1 << j) & i)//第j位存在则将j+1点标记
                {
                    vis[j + 1] = 1;
                    v_cnt ++;
                }
                else
                    vis[j + 1] = 0;
            }
            if(!vis[1])//如果这种组合不包含首都则直接pass掉
            {
                continue;
            }
            for(int j = 1; j <= n; j++)//并查集准备
                pre[j] = j;
            int e_cnt = 0;//存边数量
            int cost = 0;//存花费
            for(int j = 0; j < m; j++)
            {
                int u = edge[j].u;
                int v = edge[j].v;
                int w = edge[j].w;
                int pre_u = Find(u);
                int pre_v = Find(v);
                if(vis[u] && vis[v] && (pre_u != pre_v))//两点被标记且还可以合并则将其合并,边数加一
                {
                    mix(pre_u, pre_v);
                    e_cnt++;
                    cost += w;
                }
                if(e_cnt == v_cnt - 1)
                    break;
            }
            int pop_cnt = 0;
            if(e_cnt == v_cnt - 1 && cost <= k)//满足最小生成树条件且花费不大于k即为一课可行最小生成树,找出各个可行最小生成树的最大人口数
            {
                for(int j = 1; j <= n; j++)
                {
                    if(vis[j])
                    {
                        pop_cnt += num[j];
                    }
                }
                max_p = max(pop_cnt, max_p);
            }

        }
        printf("%d\n", max_p);
    }
    return 0;


学习笔记——神经科学的数学基础 m%3D2X_%7Bi%7DiNy%3DAxW%3DABx_%7Bj%7Dy_%7Bi%7Dy%3DWxt=N7T8卷积神经网络(CNN)详细介绍及其原理详解_IronmanJay的博客-优快云博客https://blog.youkuaiyun.com/IronmanJay/article/details/128689946? 阅读详情

相关推荐

安装keil5(MDK)及导入pack包教程

首先说明的是我安装的Keil版本为Keil V5.29.0.0 1、安装软件 右键管理员权限运行安装包 设置安装路径以及pack的存放路径 随意输入First Name和E-mail 安装驱动 完成软件安装,然后可以先关闭弹出的Pack Install(一会再添加) 2、激活一下软件 打开桌面的Keil File → License Management(我之前注册过了) 右...

hrx-@@ 10万+

CSUOJ 1116 Kingdoms

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1116、 1116: Kingdoms Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 441  Solved: 145 Description A kingdom has n cities numbered

关于完全听不懂神犇在说什么的事 651

抖音短视频数据抓取实战系列(一)——模拟器性能对比与优化配置

本文是抖音短视频数据抓取实战系列的开篇,重点探讨了模拟器选型与优化配置。通过对比雷电、夜神、逍遥三大主流模拟器在性能、抖音兼容性及抓包友好度上的表现,文章指出雷电模拟器因其稳定的桥接网络模式和良好的兼容性,是数据抓取的优选。并提供了雷电模拟器从安装、网络桥接到系统优化的详细配置指南,旨在帮助读者构建稳定高效的抓取环境。

weixin_29230901的博客 246

湖南省第八届程序设计竞赛F

Problem F. Kingdoms   Akingdom has n cities numbered 1 to n, and some bidirectional roads connectingcities. The capital is always city 1.   After awar, all the roads of the kingdom are destroyed

住在半山腰的螃蟹专栏 892

5QI(5G QoS Identifier)

此外,5QI与网络切片(Network Slicing)密切相关,网络切片允许网络运营商在物理网络内部创建虚拟网络,为不同的客户和用例提供定制化服务和QoS。5QI(5G QoS Identifier)在5G网络中用于标识不同的服务质量要求,它允许网络对不同的数据流进行优先级划分和资源分配。:5QI的实施需要对5G网络的QoS参数有深入的理解,包括延迟、吞吐量、数据包丢失率等,这对技术人员提出了较高的要求。不是所有的5G设备都支持5QI的全部功能,这可能会限制5QI应用的广泛性。

记录有价值的内容 6531

sentaurus学习笔记(HFET_pGate_GaN仿真)(九)

`(sdedr:define-constant-profile-placement "Ohmic.s" "Ohmic" "Pl.ohmic.s" 0.002)`这一行将`"Ohmic"`掺杂剖面放置在`"Pl.ohmic.s"`参考评估窗口中,同样`0.002`可能表示掺杂剖面在窗口中的偏移量或其他参数。欧姆接触是半导体器件中的一种低阻抗电气接触,通常需要高掺杂来减少接触电阻并提高器件性能。通过这些步骤,SDE能够为器件的源极和漏极区域设置高掺杂的欧姆接触,这对于确保器件的高性能和低接触电阻至关重要。

qq_52640792的博客 1382

CSU 1116: Kingdoms(状态压缩+DP)

Kingdoms Time Limit: 3 Sec  Memory Limit: 64 MB Submit: 410  Solved: 127 [Submit][Status][Web Board] Description A kingdom has n cities numbered 1 to n, and some bidirectional roads connecti

hellohelloC的博客 664

RK3588上使用PCIE接口的国产千兆网卡YT6801

YT6801是裕太微电子开发的一款国产千兆PCIE以太网控制器芯片,可作为RK3588的PCIE外扩网口使用。

荻夜的博客 8212

UVA - 12507 状压DP

Kingdoms Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description 题意:在钱数为k的限制下,求建与1直接或者间接相连的桥,使得人数最多。 思路:初始化只有1的状态为0,其余为

Turne的博客 570

手把手教你学 Simulink (1.3):探索Simulink模块库的奇妙世界

Simulink模块库按照功能分为多个类别,每个类别包含专门用于特定任务的模块。% 打开Simulink库浏览器% 或者以编程方式获取库列表disp('可用的库列表:');end常用模块(Simulink):最基础和最常用的模块信号处理模块(DSP System Toolbox):专业信号处理工具通信模块(Communications Toolbox):通信系统设计控制系统模块(Control System Toolbox):控制系统设计与分析% 创建自定义模块库。

QQ_778132974的博客 383

新凯来嵌入式面经及参考答案

利用编译器预定义宏判断目标架构,如。

大模型大数据攻城狮的专栏 2065

UVA - 1204 Fun Game (状压DP)

题意: 一群小孩(至少两个)围成一圈做游戏。每一轮从某个小孩开始往他左边或者右边传手帕。一个小孩拿到手帕后在手帕上写上自己的性别,男孩写B,女孩写G,然后按照相同的方向传递给下一个小孩,每一轮都可能在任何一个小孩写完后停止,现在游戏已经进行了n轮,已知n轮中每轮手帕上留下的字,问最少可能有几个小孩。 分析: 定义状态dp[i][j][x]表示当前拿的字符串状态为i,最后一次拿的字符串为j,j...

兜率工的博客 524

【AI Agent】Hermes Agent解析

Hermes Agent:一个具备持久化学习能力的开源AI Agent框架 核心特点: 突破传统Agent的"无状态困境",通过记忆系统(SQLite+FTS5全文检索)实现跨会话经验积累 支持200+模型自由切换,11种模型家族和15+平台接入 五层架构设计(入口层、编排层、推理层、工具执行层、存储层)实现平台无关性 47+内置工具和动态技能沉淀机制,支持从任务执行中自动提炼可复用Skill 独特的四层记忆体系(会话/每日/长期/向量索引)和辩证式用户建模 与传统Agent对比: 具备自我进化能力,通过A

sysu_lluozh 928

UVA - 11825 状压DP

该题目是EMAXX推荐的练习题,刘汝佳的书也有解说 如果S0属于全集,那S0就可以作为一个分组,那么S分组数可以是best{当前S中S0的补集+1} 对于集合类的题目我觉得有点抽象,希望多做多理解把 #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i...

weixin_30642869的博客 123

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

UVA - 1204 Fun Game——状压dp

题意: 给定n个串(n&lt;=16),要求一个最短的环,使得每个串都是这个环的子串,输出环的长度。 思路: 定义dp(i,j,k)为状态(选定的串的集合, 最后一个串的下标,最后一个串的接法(0正接1反接))的最大重合长度 初始化: dp(1,0,0)=0; 因为题目要求的是环,所以我们一开始要任意选一个串作为环的开头,这里我选择的是下标为0的字符串,并且正放 转移方程: dp(...

hao_zong_yin的博客 502

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

POJ - 3259 Wormholes(SPFA最短路判断负环)

While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is

Wabrush的博客 813

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Roads and Libraries(最小生成树)

The Ruler of HackerLand believes that every citizen of the country should have access to a library. Unfortunately, HackerLand was hit by a tornado that destroyed all of its libraries and obstructed it

Wabrush的博客 609

FZU - 2254 英语考试(最小生成树)

在过三个礼拜,YellowStar有一场专业英语考试,因此它必须着手开始复习。这天,YellowStar准备了n个需要背的单词,每个单词的长度均为m。YellowSatr准备采用联想记忆法来背诵这n个单词:1、如果YellowStar凭空背下一个新词T,需要消耗单词长度m的精力2、如果YellowSatr之前已经背诵了一些单词,它可以选择其中一个单词Si,然后通过联想记忆的方法去背诵新词T,需要消耗

Wabrush的博客 601

POJ - 3660 Cow Contest (Warshall算法求传递闭包)

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is uni

Wabrush的博客 594

UVALive - 6039 Let's Go Green

There are a lot of cities in Tree Land kingdom and each city is connected to other cities with roads such that there is exists exactly one path to go from one city to any other city. Each road in Tree

Wabrush的博客 591

POJ - 2240 Arbitrage(SPFA判断正环| 前向星+SPFA尝试)

Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British

Wabrush的博客 515

POJ - 1860 Currency Exchange(SPFA最大路判断正环)

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can

Wabrush的博客 514

POJ - 3615 Cow Hurdles (Floyd变形)

Farmer John wants the cows to prepare for the county jumping competition, so Bessie and the gang are practicing jumping over hurdles. They are getting tired, though, so they want to be able to use as l

Wabrush的博客 442

SDUT 3363 数据结构实验之图论七:驴友计划(最短路)

Problem Description 做为一个资深驴友,小新有一张珍藏的自驾游线路图,图上详细的标注了全国各个城市之间的高速公路距离和公路收费情况,现在请你编写一个程序,找出一条出发地到目的地之间的最短路径,如果有多条路径最短,则输出过路费最少的一条路径。 Input 连续T组数据输入,每组输入数据的第一行给出四个正整数N,M,s,d,其中N(2 Output

Wabrush的博客 424

SDUT 2622 最短路径

最短路径 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Description 为了准备一年一度的校赛,大家都在忙着往赛场搬运东西,比如气球什么的。这时 YY 也没有闲着,他也加入了搬运工的行列。已知学校有 N 个路口和 M 条路,YY 并不是把东西直接搬到赛场,而是从 S 路口搬运到

Wabrush的博客 408

SDUT 2144 图结构练习——最小生成树

Problem Description  有n个城市,其中有些城市之间可以修建公路,修建不同的公路费用是不同的。现在我们想知道,最少花多少钱修公路可以将所有的城市连在一起,使在任意一城市出发,可以到达其他任意的城市。   Input  输入包含多组数据,格式如下。 第一行包括两个整数n m,代表城市个数和可以修建的公路个数。(n 剩下m行每行3个正整数a b c,

Wabrush的博客 386
上一篇: 堆
下一篇: FZU - 2254 英语考试(最小生成树)
Wabrush
博客等级 码龄9年 15粉丝 68原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值