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

湖南省第八届大学生计算机程序设计竞赛试题 湖南省第八届ACM大学生计算机程序设计竞赛试题,湖南省第八届ACM大学生计算机程序设计竞赛试题;湖南省第八届ACM大学生计算机程序设计竞赛试题; 立即下载

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

HINT

Source



题意:有m条可修复的路,每个城市有相应的人口数,修复每条路需要指定的话费,问你在话费不超过K,最多能与多少人相通(可以当作能获取多少资源)


解题思路:比赛时,由于到最后一个小时才接到这题,思路很混乱就开始上去写了,比赛时将代码写戳了。言归正传:

    我们可以用二进制状态表示访问的每种情况,比如1010,表示城市二和四被访问了,然后我们只要建立相应的状态转移方程即可。


<span style="font-size:18px;">#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#define INF 0x3f3f3f3f
using namespace std;
const int N=17;
int map[N][N];
int value[N];
int dp[1<<N];
int main()
{
    int n,m,l;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d %d",&n,&m,&l);
        int x,y,dis;
        memset(map,0x3f,sizeof(map));
        for(int i=1;i<=n;i++) scanf("%d",&value[i]);
        for(int i=0;i<m;i++){
                scanf("%d %d %d",&x,&y,&dis);
                map[y][x]=map[x][y]=min(map[x][y],dis);
        }
        memset(dp,0x3f,sizeof(dp));
        dp[1]=0;
        for(int i=2;i<(1<<n);i++) ///状态
        {
            if(i&1){
                for(int j=1;j<=n;j++){
                    if(i &(1 <<(j-1))){
                        for(int k=1;k<=n;k++){
                            if(i &(1 <<(k-1))){
                                dp[i]=min(dp[i],dp[i &(~(1 <<(k-1)))]+map[k][j]);
                            }
                        }
                    }
                }
            }
        }
        int ans=0;
        for(int i=1;i<(1<<n);i++)
        {
            int sum=0;
            if(dp[i]<=l){
                for(int j=1;j<=n;j++){
                    if(i &(1 <<(j-1)))
                        sum+=value[j];
                }
            }
            ans=max(ans,sum);
        }
        printf("%d\n",ans);
    }
    return 0;
}

</span>


大模型推理中Prefill与Decode、KV Cache三者说明 Prefill 阶段对输入 Prompt 并行编码,一次性构建KV Cache,属于计算密集型任务。Decode 阶段逐 token 迭代生成,复用KV Cache避免重复计算,属于显存带宽密集型任务。KV Cache是自回归推理的核心基石,用显存换时间,实现两阶段衔接与高效生成。PD 分离通过任务解耦、异构硬件、异步调度、独立批处理,解决传统一体化推理的资源冲突与利用率低下问题,是当前云端大模型推理服务的主流工程优化方向。 阅读详情

相关推荐

PythonOCC基础使用:建模——倒角倒圆(二维+三维)

占坑

weixin_42755384的博客 4639

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

飞塔防火墙CVE-2022-40684漏洞复现:手把手教你搭建靶场环境(附调试技巧)

本文详细指导如何搭建Fortinet飞塔防火墙CVE-2022-40684认证绕过漏洞的靶场环境,涵盖虚拟机部署、网络配置、调试模式开启及漏洞原理分析。通过手把手教程,读者可掌握漏洞复现与深度调试技巧,深入理解该认证绕过漏洞的利用与防御。

h6i7j8的博客 695

UVa 12507 - Kingdoms(状压dp)

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 rebui

Wabrush的博客 484

CA51F3系列(国产)+低功耗模式

CA51F351P6芯片是基于1T 8051内核的8位微控制器,通常情况下,运行速度比传统的8051芯片快10倍,性能更加优越。内置8K Flash程序存储器,可多次重复编程的特性给用户开发带来了极大的方便。不仅保留了传统8051芯片的基本特性,还集成了12Bit ADC、LCD/LED驱动、Touch Key、16Bit PWM、 UART、RTC、低电压检测(LVD)等功能模块。支持IDLE、STOP和低速运行三种省电模式以适应不同功耗要求的应用。

WANGJIHUIwang的博客 572

VS2022如何兼容老项目?.NET Framework 4.0/4.5安装避坑指南

本文提供了在Visual Studio 2022中兼容和维护.NET Framework 4.0/4.5等老旧项目的完整解决方案。针对VS2022无法直接识别旧框架的核心问题,文章详细剖析了原因,并重点推荐通过官方NuGet包安全获取引用程序集的方法,给出了从下载、解压到部署至标准目录的详细步骤。同时,文章还深入探讨了项目文件格式过旧、NuGet包还原失败等进阶问题的排查与解决策略,旨在帮助开发者高效、稳定地在现代开发环境中维护历史代码库。

weixin_27734811的博客 514

CSU 1116 Kingdoms(Kruskal)(枚举)

KingdomsDescription 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

Darkarts 597

TekTronix 高速信号完整性基础以及测试方法分析

如果信号速率比较高,则CTLE与FFE都无法满足要求,尤其是大于10G,比如USB3.1Gen2 10Gbps,PCIe4.0 16Gbps,这些场合都会采用DFE均衡,DFE的优点是,它既可以识别当前比特,并且可以判决前面的n个比特(Tap的阶数越高,看到的前面的比特数越多),根据前面比特的变化来计算出要处理当前比特所要变化的幅度,来补偿由于前面比特变化所引起的信号幅度的变化。10G以下,一般都不太关心这个串扰,高于10G的信号,会发现相互之间的串扰越来越明显,已经成为引起误码的重要的原因之一。

weixin_45365488的博客 4142

湖南省第八届程序设计竞赛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

android11.0 Launcher3 高端定制之循环滑动

待机界面可以向左、向右循环切换页面 效果图 修改清单 packages/apps/Launcher3/res/values-zh-rCN/strings.xml packages/apps/Launcher3/res/values/config.xml packages/apps/Launcher3/res/values/strings.xml packages/apps/Launcher3/res/xml/launcher_preferences.xml packages/apps/Launc

cczhengv 1549

(王道408考研数据结构)第二章线性表-第一节:线性表的定义和基本操作

文章目录一:线性表的定义二:线性表的基本操作 一:线性表的定义 线性表(Linear List):零个或多个数据元素的有限序列 元素之间是有顺序的 若元素存在多个,则第一个元素无前驱,最后一个元素无后继,其他元素都有且只有一个前驱和后继 线性表强调有限,“所有的整数按递增次序排列”很明显不是线性表 线性表可以表示为 L=(a1,a1,...,ai+1,...,an)L=(a_{1},a_{1},...,a_{i+1},...,a_{n})L=(a1​,a1​,...,ai+1​,...,an​) a

快乐江湖的博客 3704

CSU 1116 Kingdoms

  原题链接:http://122.207.68.93/OnlineJudge/problem.php?id=1116   由于n比较小,直接枚举,然后对于每一个状态用最小生成树算法验证即可,感觉是水过去的,复杂度O(2n*n*m)。(有状压DP做法?) #include <stdio.h> #include <string.h> #include <...

weixin_30781631的博客 65

基于simulink的BMS系统中电池单体故障隔离与恢复建模

本文介绍了使用Simulink构建BMS电池单体故障诊断与容错控制模型的方法。主要内容包括:1)搭建12串锂电池组模型;2)实现单体故障注入(过压、断线等);3)设计故障诊断与隔离模块(采用Stateflow状态机);4)开发恢复判断逻辑(需满足电压、温度等条件);5)系统监控与仿真设置。文章还提供了工程实践建议,如防环流设计、通信冗余等,并指出进阶方向(多故障处理、AI预警等)。该模型适用于新能源汽车、储能系统的BMS设计,可提高系统可靠性和安全性。

xiaoheshang_123的博客 140

CSU 1116 Kingdoms

传送门 Description 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. ...

weixin_30266829的博客 94

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

csu 1116 Kingdoms 最小生成树 二进制枚举

来源 湖南省第八届大学生计算机程序设计竞赛 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 bidi

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

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

1116: Kingdoms+csuoj+暴力枚举+最小生成树

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

u012870383的专栏 923

SAP B1中文培訓教材_SDK

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

ZOJ 1425 Crossed Matchings(动态规划)

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1425 Crossed Matchings Time Limit: 2 Seconds      Memory Limit: 65536 KB There are two rows of positive integer numbers. W

hellohelloC的博客 1183

POJ 2385 Apple Catching(DP)

Apple CatchingTime Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatus Description It is a little known fact that cows love apples. Farmer John has two apple

hellohelloC的博客 940

http://codeforces.com/contest/721/problem/C Journey(DAG上的dp)

Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently Irina arrived to one of the most famous cities of Berl

hellohelloC的博客 761

HDU 5418 Victor and World(状压dp、floy最短路)

Victor and WorldTime Limit:2000MS    Memory Limit:131072KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 5418 Description After trying hard for many years, Victor has finally rec

hellohelloC的博客 699

Codeforces 543A. Writing Code DP

Writing CodeTime Limit:3000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64uSubmitStatusPracticeCodeForces 543ADescriptionProgrammers working on a large project have just received a task to

hellohelloC的博客 677

ZOJ 3469 Food Delivery

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3469 Food Delivery Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving problems, we usually p

hellohelloC的博客 672

HDU 3236 Gift Hunting(0-1背包变异)

Gift Hunting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1743    Accepted Submission(s): 581 Problem Description After winning

hellohelloC的博客 661

HDU 1428 漫步校园(BFS+DFS)

漫步校园Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticeHDU 1428 Description LL最近沉迷于AC不能自拔,每天寝室、机房两点一线。由于长时间坐在电脑边,缺乏运动。他决定充分利用每次从寝室到机房的时间,在校园里

hellohelloC的博客 645

POJ 1390 Blocks(DP)

BlocksTime Limit:5000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu SubmitStatus Description Some of you may have played a game called 'Blocks'. There are n blocks in a row, ea

hellohelloC的博客 633
上一篇: 数列上的分治(求逆序数)
下一篇: HDU 3236 Gift Hunting(0-1背包变异)
hellohelloC
博客等级 码龄12年 15粉丝 107原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值