poj 2686 状态压缩

TMS320F280049C 学习笔记4 Led_ex1_blinky 初始化函数及系统时钟 承接上一篇学习笔记,这次开始实际运行例程。本文概述了280049C的第一个实验 Led灯闪烁。同时也总结了系统的初始化函数和时钟配置。 阅读详情
题目链接

解析

状态:现在在城市u,此时还剩下的车票集合为S
dp[S][u] 就是这个状态。
从这个状态出发,使用一张车票 i 在S中移动到相邻城市v,就相当于转移到“在城市v,此时还剩下的车票的集合为S\{i}”这个状态。

代码

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const double inf = 0x3f3f3f3f;
const int maxn = 10;
const int maxm = 50;
int t[maxn];
int d[maxm][maxm];
double dp[1<<maxn][maxm];
int n, m, p, a, b;
//dp[S][u] 表示当前车票的集合S在u城市的最小花费。
void solve() {
    for (int i=0; i<(1<<n); i++)
        fill(dp[i], dp[i]+m, inf);
    dp[(1<<n)-1][a-1] = 0;
    double res = inf;
    for (int S=(1<<n)-1; S>=0; S--) {   //枚举集合所有的元素
        res = min(res, dp[S][b-1]);  //当前集合中到终点的最小花费
        for (int u=0; u<m; u++) {   //当前在u城市
            for (int i=0; i<n; i++) {  //在u城市用i车票
                if (S>>i & 1) {
                    for (int v=0; v<m; v++) {//用i车票u到v城市的花费
                        if(d[u][v] >= 0)
                        dp[S & ~(1<<i)][v] = min(dp[S & ~(1<<i)][v]
                                , dp[S][u]+d[u][v]*1.0/(1.0*t[i])); //用了i车票取最小花费
                    }
                }
            }
        }
    } 
    if (res == inf)
        puts("Impossible");
    else 
        printf("%.3f\n", res);
}


int main() {
    while (~scanf("%d%d%d%d%d", &n, &m, &p, &a, &b)) {
        if (n == 0 && m == 0 && p == 0 && a == 0 && b == 0)
            break;
        for (int i=0; i<n; i++)
            scanf("%d", &t[i]);
        memset(d, -1, sizeof(d));
        for (int i=0; i<p; i++)
        {
            int u, v, w;
            scanf("%d%d%d", &u, &v, &w);
            d[u-1][v-1] = w;
            d[v-1][u-1] = w;
        }
        solve();
    }
    return 0;
}
816华为OD机试真题 新系统 - 电池充电时间计算 (Java/Py/C/C++/Js/Go) 阅读详情

相关推荐

数字IC后端设计实战:从floorplan到powerplan的优化策略

本文深入探讨了数字IC后端设计中从Floorplan到Powerplan的核心优化策略。文章强调了Floorplan作为布局基础的重要性,并详细解析了Powerplan规划,特别是针对低功耗设计中MTCMOS的应用与IR Drop分析。通过实战经验分享,提供了从模块摆放到电源网络构建、再到IR Drop问题层层递进的解决方案,旨在帮助后端工程师构建稳健高效的芯片物理设计。

weixin_28338005的博客 349

POJ 2686 状态压缩DP

题目链接:http://poj.org/problem?id=2686 题目大意:有一个旅行家计划旅行,他所在城市有m个城市,在城市之间有p条路径。从某个城市沿着某条道路到相邻的城市需要坐马车,而乘坐马车需要车票。一张车票只能通过一条路,每张车票都记有马的数量。从一个城市到另一个城市所需要的时间是城市之间道路的长度除以马的数量的结果。这位旅行家一共有n张车票,第i张车票上的马的数量是ti

新熊君的博客 564

TexGen模型导出到ABAQUS的inp文件:如何快速修改纱线材料属性(避坑指南)

本文针对TexGen模型导出到ABAQUS的inp文件后纱线材料属性缺失或错误的问题,提供了高效的解决方案。文章详细解析了inp文件结构,指导用户如何手动定位并修改纱线材料属性,并提供了可批量处理的Python脚本,帮助用户快速、精准地完成材料参数赋值,有效提升复合材料仿真工作效率。

ice55的博客 602

POJ 2686 Traveling by Stagecoach (状态压缩DP)

Traveling by Stagecoach Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2776   Accepted: 996   Special Judge Description Once upon a time, there was a trav

MADE_Y 2261

解锁微信小程序Camera组件的隐藏技能:连续扫码背后的逻辑与陷阱

本文深入解析微信小程序Camera组件在连续扫码功能中的技术原理与优化方案,涵盖识别率提升、内存管理策略及典型问题排查。通过实战参数对比和代码示例,帮助开发者解决扫码枪应用中的性能损耗与界面卡顿问题,提升零售、物流等场景的工作效率。

weixin_29019241的博客 492

2026年医疗AI的可信革命全栈实现(上)

医疗AI可信化:贝叶斯网络与向量数据库的协同架构 本文探讨2026年医疗AI发展的关键转折点——从追求生成能力转向构建可信决策系统。核心观点认为,医疗AI下一阶段的竞争焦点在于不确定性量化和错误传播约束,而非单纯的问答流畅度。研究提出以Python为开发语言、结合向量数据库与贝叶斯网络的混合架构: 技术分层 语言层(LLM):处理自然语言交互与工具调用 证据层(Milvus/Qdrant):实现可审计的临床知识检索 概率层(pgmpy/PyMC):执行贝叶斯推理与风险路由 治理层:确保权限控制与人工复核机制

AllenLV的博客 1151

POJ2686 状态压缩

n张车票; m个城市; p条边, 从u->v 花费len[u][v]/ 所选车票 求从start到end 最小花费 dp[s][u] ,剩余车票集合,当前所在城市u ; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.P

老李2009 386

halcon与QT联合:(5.1)瓶盖检测以及QT界面搭建

halcon实现瓶盖检测与定位代码: dev_close_window() dev_open_window(0, 0, 1024, 1024, 'black', WindowHandle) list_image_files('C:/Users/TIMI/Desktop/picture/circle', 'default', [], ImageFiles) for J := 1 to 4 by 1 *read image read_image(halconImage, ImageFil

m0_37957160的博客 1044

poj2686(TSP)

/* translation: 一张图由n个点构成,并且若干个点之间由双向边连成。有若干票,每次走完一个边花费一张票 花费时间是边的长度 / 票的权重。求从指定起点到终点所用的最短时间。 solution: 状压dp 首先观察数据,发现n比较小,很容易看出对n进行状态压缩。设置dp[s][v]表示状态s下,到达节点v所用的最短时间。 其中s表示已经使用的车票

林伏案的博客 589

Unity 之jenkins自动打包

1.Jenkins 是什么? jenkins官方网址:https://www.jenkins.io/zh/ Jenkins是一款开源 CI&CD(持续集成&持续部署) 软件,用于自动化各种任务,包括构建、测试和部署软件。 简易安装:Jenkins 是一个基于 Java 的独立程序,可以立即运行,包含 Windows、Mac OS X 和其他类 Unix 操作系统。Jenkins 支持各种运行方式,可通过系统包、Docker 或者通过一个独立的 Java 程序。 插件扩展:通过更新中心中的

TxNet 2132

Open3D——RANSAC拟合球面【2026最新版】

RANSAC三维点云球面拟合,博客长期更新,本文最新更新时间为:2026年3月5日。

点云侠的博客 4084

结合poj 2686,入门状态压缩dp

poj 2686 题意:有m个城市,p条道路将他们连接起来,每经过一条道路需要缴纳车票,车票上印有马的匹数,因此通过一条道路等于道路距离/马的匹数,一共有n张票。问从a点到b点最少需要多少时间 假如不要车票的话,这道题无疑是最短路的算法。但是每条路均可以使用车票,假如将所有可行的方法保存下来,用贪心的思想将方法中经过的道路从长到短排序,票最多的匹配最长的道路,试试就逝世,再往下想会发现这程序写...

forOnward的博客 274

抓包工具:Fiddler下载、安装、使用 教程

文章目录抓包工具:Fiddler下载、安装、使用 教程一、Fiddler 下载二、Fiddler 安装三、Fiddler 使用3、Statistics 请求的性能数据分析4、Inspectors 查看数据内容5、AutoResponder 允许拦截指定规则的请求6、Composer 自定义请求发送服务器7、Filters 请求过滤规则8、Timeline 请求响应时间9、Fiddler 设置解密HTTPS的网络数据10、Fiddler 内置命令与断点 抓包工具:Fiddler下载、安装、使用 教程 Fidd

Sumarua的博客 17万+

POJ2686 Traveling by Stagecoach 状态压缩DP

POJ2686 比较简单的 状态压缩DP 注意DP方程转移时,新的状态必然数值上小于当前状态,故最外层循环为状态从大到小即可。 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn = 1 &l...

weixin_30361753的博客 156

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

poj 2686 Traveling by Stagecoach【状态压缩】

题目链接:http://poj.org/problem?id=2686题意: 一个人从某个城市要到另一个城市。然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票。花费的时间是马车票上有个速率值,用边/速率就是花的时间。 问最后这个人花费的最短时间是多少dp[i][j] 表示集合 i 车票 到 j 的最优解代码:#include <stdio.h> #include <ctime>

mfcheer 683

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

poj 2686 Traveling by Stagecoach 状态压缩

http://poj.org/problem?id=2686题意:有m个城市,其中有些城市之间 有道路连通,且有一定的距离w,现在你有n张卡,每张卡有个权值t能让经过通过道路的费用变为w/t,求从城市a到城市b的 最小的花费。#include #include #include #include #include #include #include #include #include #inclu

smilesundream的博客 428

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

状态压缩DP 之 poj 2686

之前敲了一个 “旅行商问题”

JmingS 778

POJ2686 状态压缩dp

题目大意: n张票,m个城市,p条路,a是出发地,b是目的地,然后n张票上面都会有马的数目。从a走到b中,经过每条路都需要时间,并且一定要借助马。时间 = 路程/马的数量。问,如何走才能让时间最短(每张票只能使用一次)。 挑战程序竞赛上的例题P193. 代码我就弱弱的贴一下吧 #include #include #include #include using namespace

黑猫5027的博客 402

poj2686(Traveling by Stagecoach)状态压缩dp+DAG

Traveling by Stagecoach Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2894   Accepted: 1053   Special Judge Description Once upon a time, there was a tra

相思作坊半世离殇的博客 551

poj 2686 状态压缩DP

如题;http://poj.org/problem?id=2686 Traveling by Stagecoach Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2206   Accepted: 755   Special Judge Description O

zht的专栏 462

poj2686(状态压缩dp)

链接:点击打开链接 题意:n张车票,m座城市,k条路,问从城市a到城市b的最短时间,从一个城市移动到另一个城市所需时间等于城市之间的长度除以每张票上数字,一张票只能使用一次 代码:#include #include #include #include #include #define INF 0x3f3f3f3f using namespace std; int t[50],d[30

stay_accept的专栏 599

POJ 2686_Traveling by Stagecoach【状态压缩DP】

题意: 一共有m个城市,城市之间有双向路连接,一个人有n张马车票,一张马车票只能走一条路,走一条路的时间为这条路的长度除以使用的马车票上规定的马车数,问这个人从a出发到b最少使用时间。 分析: 状态压缩dp,用dp[i][j]表示到达j城市的最小时间,其中i为剩余车票的集合。集合i使用状态压缩的表示方法。由于剩余车票的集合不断变小,实际上为求DAG最短路问题...

An55511的博客 151

状态压缩or 最短路 poj2686

自己居然做出来了。用状态压缩的。。 但是用djk却是MLE 啊,然后改了之后是TLE。。。 这用dp是因为这个DAG 状态dp #include #include #include #include #include #include #include #include #include #include #include #include #define ll long long #de

now_ing的博客 312

poj 2686 Traveling by Stagecoach ---状态压缩DP

题意:给出一个简单带权无向图和起止点,以及若干张马车车票,每张车票可以雇到相应数量的马。 点 u, v 间有边时,从 u 到 v 或从 v 到 u 必须用且仅用一张车票,花费的时间为 w(u, v) / ticket[i], 其中 w(u, v) 表示边的权值,ticket[i] 表示第 i 张车票可以雇到的马匹数。求从起点到终点花费的最小时间。 如果不能到达终点,输...

weixin_33825683的博客 87

Traveling by Stagecoach(POJ-2686)(状态压缩DP)

状态压缩DP和普通DP唯一的区别就是它所枚举的对象不再是一个整数,而是一个集合,解决的策略就是利用二进制将这个集合压缩成一个整数。 对于该题,dp[s][v]表示:s表示在该城市剩下的车票集合,v表示在城市v,dp表示在该状态的最小话费。 影响决策的因素有一下几个: 1.一共使用哪几个车票(包括数量和种类) 2.当前从哪个城市到哪个城市 3.使用哪个车票完成两个城市的转移 所以一共需要

AC_Arthur的专栏 1141
上一篇: hdu 2888 二维线段树和二维ST模版
下一篇: poj 2728 最优比例生成树
nielaishan
博客等级 码龄11年 7粉丝 94原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值