CF721C Journey 题解

Journey -每天一把CF - 20201101 2020-11-01 dp 721C 1800 题目 原题链接:https://codeforces.com/problemset/problem/721/C 思路 题目大意:一个DAG(有向无环图),要在时间限度以内从点1到达点n并经过尽量多的点,给出点数以及路径。 思路:我裂了 ,1800 的题 都做不出来了。dp状态想了好久都没想懂。 真正的方案:拓扑加dp。 dp[x][y]表示到达城市x经过y个城市的最少时间,同时用pre数组存储每一个状态对应的前一个坐标。 代码实现 #include&lt. 阅读详情

首先可以发现,本题实际上类似一个 DAG 上的背包问题。

那么我们有一个很显然的做法:

设 f[v][t]f[v][t]f[v][t] 表示从点 111 到达点 vvv,路径长度为 ttt 的所有方案中的最大点数,转移方程平凡,这里略去。

但是 T≤109T \leq 10^9T≤109 ,这样做空间会炸飞。

可喜的是,答案一定是小于等于 nnn 的,同时 n≤5000n \leq 5000n≤5000,这启发我们可以考虑 O(n2)O(n^2)O(n2) 空间的算法。

我们换个角度思考:既然枚举距离求点的最大值不行,那么枚举点数求距离的最小值是否可行?

观察数据范围,在复杂度上这显然是可行的。

所以我们试着考虑转移方程:

设 f[v][t]f[v][t]f[v][t] 表示从点 111 到达点 vvv,经过点数为 ttt 的所有方案中的最短路径。

那么我们有转移方程:(其中 prefix(v)prefix(v)prefix(v) 表示点 vvv 的前驱,即所有能够到达点 vvv 的点,www 表示边 u→vu\to vu→v 的长度)

f[v][t]=min⁡u∈prefix(v)(f[u][t−1]+w)[f[u][t−1]+w<=T] f[v][t] = \min_{u \in prefix(v)} (f[u][t - 1] + w)[f[u][t - 1] + w <= T] f[v][t]=u∈prefix(v)min​(f[u][t−1]+w)[f[u][t−1]+w<=T]

为什么是最短路径呢?因为我们并不关心路径的长度到底是多少,只关心它是否不超过 TTT。从贪心的角度考虑,这里选择最短路径显然最优。

转移的同时,我们记录一个 g[v][t]g[v][t]g[v][t],表示 f[v][t]f[v][t]f[v][t] 是由哪个 uuu 转移过来的,方便最后输出答案。

代码:(别急着抄)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<cstring>
#include<vector>
#include<queue>

#define MAXN 5005

using namespace std;

int n, m, k, in[MAXN], f[MAXN][MAXN], g[MAXN][MAXN];
vector<pair<int, int> > vec[MAXN];

inline void Toposort()
{
    queue<int> q;
    q.push(1), f[1][1] = 0, g[1][1] = 1;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        for(register vector<pair<int, int> > :: iterator it = vec[u].begin(); it != vec[u].end(); it++)
        {
            int v = it -> first, w = it -> second;
            for(register int i = 1; i <= n; i++)
            {
                if(g[u][i - 1] && f[u][i - 1] + w <= k && f[u][i - 1] + w < f[v][i])
                {
                    f[v][i] = f[u][i - 1] + w;
                    g[v][i] = u;
                }
            }
            --in[v]; if(!in[v]) q.push(v);
        }
    }
    return;
}

inline void Output(int u, int k)
{
    if(u > 1) Output(g[u][k], k - 1);
    printf("%d ", u);
    return;
}

int main()
{
    #ifdef FILE
        freopen("Input.in", "r", stdin);
        freopen("Output.out", "w", stdout);
    #endif
    scanf("%d%d%d", &n, &m, &k);
    for(register int i = 1; i <= m; i++)
    {
        int u, v, w; scanf("%d%d%d", &u, &v, &w);
        vec[u].push_back(make_pair(v, w));
        ++in[v];
    }
    memset(f, 0x3F, sizeof f);
    Toposort();
    int t;
    for(t = n; t > 1; t--) if(g[n][t]) break;
    printf("%d\n", t);
    Output(n, t);
    return 0;
}

但是上面的代码是过不了的,Hack 数据:

4 4 10
2 1 1
2 3 1
1 3 1
3 4 1

这组数据的特点是什么?DAG 的源点不是点 111 !

虽然点 111 的所有直接前驱和间接前驱都不会影响到答案,但是它们会影响拓扑排序的过程。

比如上面的边 2→32 \to 32→3,虽然点 222 在拓扑排序中没有访问到,但是这条边对点 333 的入度仍然存在 111 的影响。

所以我们需要先 DFS 一遍,把点 111 的所有直接前驱和间接前驱对于拓扑图中点的入度影响去掉。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cassert>
#include<cstring>
#include<vector>
#include<queue>

#define MAXN 5005

using namespace std;

int n, m, k, in[MAXN], f[MAXN][MAXN], g[MAXN][MAXN];
bool vis[MAXN];
vector<pair<int, int> > vec[MAXN];

inline void DFS(int u)
{
    vis[u] = true;
    for(register vector<pair<int, int> > :: iterator it = vec[u].begin(); it != vec[u].end(); it++) if(!vis[it -> first]) DFS(it -> first);
    return;
}

inline void Toposort()
{
    queue<int> q;
    q.push(1), f[1][1] = 0, g[1][1] = 1;
    while(!q.empty())
    {
        int u = q.front(); q.pop();
        for(register vector<pair<int, int> > :: iterator it = vec[u].begin(); it != vec[u].end(); it++)
        {
            int v = it -> first, w = it -> second;
            for(register int i = 1; i <= n; i++)
            {
                if(g[u][i - 1] && f[u][i - 1] + w <= k && f[u][i - 1] + w < f[v][i])
                {
                    f[v][i] = f[u][i - 1] + w;
                    g[v][i] = u;
                }
            }
            --in[v]; if(!in[v]) q.push(v);
        }
    }
    return;
}

inline void Output(int u, int k)
{
    if(u > 1) Output(g[u][k], k - 1);
    printf("%d ", u);
    return;
}

int main()
{
    #ifdef FILE
        freopen("Input.in", "r", stdin);
        freopen("Output.out", "w", stdout);
    #endif
    scanf("%d%d%d", &n, &m, &k);
    for(register int i = 1; i <= m; i++)
    {
        int u, v, w; scanf("%d%d%d", &u, &v, &w);
        vec[u].push_back(make_pair(v, w));
        ++in[v];
    }
    memset(f, 0x3F, sizeof f);
    DFS(1);
    for(register int i = 1; i <= n; i++) if(!vis[i]) for(register vector<pair<int, int> > :: iterator it = vec[i].begin(); it != vec[i].end(); it++) --in[it -> first];
    Toposort();
    int t;
    for(t = n; t > 1; t--) if(g[n][t]) break;
    printf("%d\n", t);
    Output(n, t);
    return 0;
}

END\mathit{END}END

Codeforces 721 C. Journey(拓扑排序+DP) codeforces每日一练。 题意: 给定n个点,m条有向边,以及k时间。求不超过k时间1-n最多能经过多少个点。 思路: 数据<=5000,说明是个暴力dp。 那么可以用dp[i][j]维护从1到i点经过了j个点,然后初始化为inf,再设dp[1][1]=0,保证每个dp都是由1出发的。 因为是有向无环图,所以我们可以在拓扑排序的时候进行dp,循环n个点,复杂度On^2,然后多开一个pre[i][j]记录经过i点时的前一个点。 由于开了两个5000*5000的数组,所以得用int,如果用long long会MLE。 代码如下: #include #define ll long long # 立即下载

相关推荐

从零构建锂电池主动均衡系统:Simulink仿真中的Buck-Boost拓扑实战指南

本文详细介绍了如何在Simulink中从零构建基于Buck-Boost拓扑的锂电池主动均衡系统。通过仿真环境搭建、参数优化、控制策略设计和性能评估,为工程师提供了完整的实战指南,帮助快速验证和优化均衡算法,提升电池组一致性与寿命。

gitlab7runner的博客 896

CF 721C - C. Journey(拓扑排序+DP)

C. Journey(拓扑排序+DP) 一点不相关的话:好久没有更新博客了,当我再次看到这些,甚至都想象不到这些都是我写的。嗳,可能这就是命运多舛吧。去年打完最后一场ICPC后,就开始抉择是考研还是继续打下去,但是好像没有人陪我打下去了,ACM一个人真的很难坚持(其实有很多话想说,但是,我现在不想再回忆,这样只会消沉自己的情绪。)。最终,在询问过已经毕业的学长之后,我选择了去考研。今天考研视频网课...

Eternally831143的博客 720

零信任架构实战:“持续验证+动态授权”全流程解析(附策略引擎联动图)

《零信任实战指南:7图拆解核心流程与行业案例》摘要:本文通过7张架构图系统解析零信任五大核心流程:强身份认证、设备检查、动态授权、持续监控和自动降权。对比金融(重数据审计)与制造业(重物理绑定)的落地差异,指出三大常见误区及解决方案。数据显示,零信任可使安全响应提速60%,泄露成本降低45%。文章提供6个月实施路线图,强调零信任是"智能信任"体系,建议企业分阶段改造,优先保护核心系统。附赠金融/制造行业策略模板及甘特图示例。(149字)

sara_han的博客 773

CF 721C

题目:找到一条加权和少于T的路线,使其经过的点数尽可能多,输出点的数量,并打印路线。题解:脑抽卡题了。。。这是题图上dp。注意要点:为了满足dp条件,必须拓扑排序。在dp过程中利用prev记录路径。dp[i][j],为了到达i需要经过j个结点的最小花费。prev[i][j],最小花费的路线的前任结点。用longlong可能会炸空间。#include&lt;iostream&gt; #include...

mengwuyaaa的博客 1158

STM32 Nucleo-144开发板开箱bring-up

最近做项目,用到STM32F446ZET6这款MCU,为了赶进度,前期软件需要提前开发,于是在某宝买了对应主控的官方开发板,为了避免遗忘,后面能翻出来回忆一些细节,就形成了本篇的主要内容,同时也希望能够帮助刚上手的小伙伴少走弯路,如果对你有帮助请点赞、收藏、转发,谢谢!

汽车电子软件领域知识分享 2676

基于STM32控制直流电机加减速正反转仿真设计

*单片机设计介绍,基于STM32控制直流电机加减速正反转仿真设计。

2201_75940997的博客 1603

CF 721C DAG上的dp

C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Recently Irina arrived to one of the most famous cities of Berland — the Berlat

ACM, deep love 712

基于SAGE算法的宽带信道参数估计算法的MATLAB仿真

本文将介绍如何使用MATLAB进行SAGE算法的仿真,以便更好地理解和应用该算法。SAGE算法是一种基于最大期望(Expectation-Maximization)方法的迭代估计算法,用于估计未知的参数。在宽带信道参数估计中,SAGE算法通过迭代优化来估计信道的传输函数。总结起来,本文介绍了如何使用MATLAB进行基于SAGE算法的宽带信道参数提取算法的仿真。通过以上步骤,我们可以得到估计的信道响应,并与真实信道响应进行比较。此外,SAGE算法还可以与其他信道估计算法相结合,以提高估计的准确性和性能。

CodeRoarX的博客 1075

CF721C. Journey[DP DAG]

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

weixin_34049032的博客 176

模型预测控制(MPC)解析(二):单输入单输出系统

在上一篇中,为了便于说明,预测控制系统采用单输入单输出系统为例进行设计。由于系统模型使用的是状态空间方程,于是可以很容易的将这种设计方法可以扩展到多输入、多输出系统。

行者无疆的博客 8223

/home 挂载点无法访问 怎么解决 指令可以进入Home目录 du: 无法访问“*“: 输入/输出错误

本文介绍了Linux系统中XFS文件系统损坏的故障排查与修复方法。当/home挂载点出现I/O错误时,通过dmesg发现XFS日志错误和磁盘I/O报错,判断可能是文件系统逻辑损坏或硬件故障。解决方案包括:1)尝试卸载分区;2)使用xfs_repair工具进行预检查和正式修复;3)在极端情况下使用-L参数强制清空日志;4)若存在硬件故障,建议优先进行数据抢救并更换硬盘。最后强调重要数据应先备份,再考虑系统重建。

专注于计算机研发知识和资讯分享 153

Codeforces Round #374 (Div. 2) C. Journey DP

&#13; C. Journey&#13; 链接:&#13; http://codeforces.com/contest/721/problem/C&#13; 题意:&#13; 给你一个n点m边的图,让你从1走到n,找到一条经过尽量多点的路径,且路径边权和小于等于T&#13; 然后输出路径。&#13; 题解:&#13; 直接DP,DP[i][j]表示在i点,当前经过了j个点的...

花海 201

【Java基础知识 1】Java入门级概述

优快云最强Java专栏,包含全部Java基础知识点、Java8新特性、Java集合、Java多线程、Java代码实例,理论结合实战,实现Java的轻松学习。

学Java,找哪吒 16万+

csust-8.6早训CF之700-2000分

目录 A - Anatoly and Cockroaches B - Efim and Strange Grade C - One-dimensional Japanese Crossword D - Passwords E - Journey F - Maxim and Array A - Anatoly and Cockroaches CodeForces - 7...

孤风的博客 544

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

SAP B1中文培訓教材_SDK

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

【Codeforces】 Round #374 (Div. 2)

Position:http://codeforces.com/contest/721 我的情况 开始还是rank1,秒出C。(11:00机房都走光了,我ma到11:05才走,只打了一个小时) 结果。。。 FST!!尤其是第一题还WA了一发。gi烂。B题还ma得慢。 最后滚到青名。。。。。 反思 首先每次都WA了A题,很不应该,虽然快,样例都没测就交,结果丢了50分。 B...

weixin_34342992的博客 203

IEM模型,地表粗糙度模拟

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

[CF721C]Journey

Codeforces DP

YveH 817

深度学习工具包 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")

CF103D Time to Raid Cowavans 题解

这道题看似可以线段树乱搞,但是真正用线段树就会发现根本没有用。 因此我们考虑根号算法。 难道是分块?错!还有一种根号算法——根号分治。 根号分治的思想就是设定阈值 SSS ,大于阈值的暴力计算,小于阈值的快速计算,而取 S=nS = \sqrt nS=n​ 时最优(不要问我怎么证明)。 那么这道题,我们针对 kik_iki​ 手动设定阈值为 500,大于 500 直接计算,小于等于 500 前缀和计算,但是因为这道题卡空间,因此我们需要对询问离线,同时处理相同的 kik_iki​ 即可。 代码: #incl

Plozia 的小窝 248

Educational Codeforces Round 103 (Rated for Div. 2)---D. Journey

D. Journey原题链接 time limit per test2 seconds memory limit per test512 megabytes inputstandard input outputstandard output There are n+1 cities, numbered from 0 to n. n roads connect these cities, the i-th road connects cities i−1 and i (i∈[1,n]). Each road

SCUEC__zhazhahui的博客 361

C. Journey(DAG上dp详解)

https://codeforces.com/problemset/problem/721/C 题目描述 Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There arennshowplaces in the city, numbered from11tonn, and some of them are connected by one-directional r...

代码笔记本 397

cf721 C DP

题目大意:给你个有向无环图,询问从1到n的路径里面,时间小于T的,但是路过点数是最多的路径。求点数 n,m只有5000,所以大胆一点,n^2的算法就冲上去。题目要什么我们的dp就设计什么。 设f[i][j]有两个权值v1,fa; 意义是从1走到i走过了j个点的最小权值。走到i点的前一个点是fa。 那么只要做一次拓扑排序,拓扑排序后就可以做一个线性的dp。不做拓扑会可能发现某个点还没被更新但是你就在...

塞满箱子的过程 265

NEERC17 J Journey from Petersburg to Moscow

CF上可以提交。 链接 依然是很妙的解法。 我们可以枚举每一个出现过的边权$L$,然后把所有边的边权减掉这个$L$,如果小于$L$就变为$0$,然后跑一遍最短路然后加上$k * L$更新答案即可。 注意$L$也可以取到$0$。 这样做就是强制让出了前$k$大的边的边权,所以能计算到所有答案。 时间复杂度$O(n^2logn)$。 Code: #include &...

dashu497731727的博客 207

cf839C Journey题解

又一次愉快的题意杀…… 参考:http://margatroid.xyz/2017-08-12-Codeforces-Round-428-Div-2-C/题意如果用一句话说就是:求一颗树深度的期望。 数据的输入表示两个节点的连通……我一直以为是有向线段orz 先来思考期望怎么求,期望=概率*权重,这里的权重就是叶节点的深度。而概率P[a]=P[father[a]]*(son of father

LiRewriter的博客 1396

Educational Codeforces Round 103 (Rated for Div. 2)题解

写在前面: 这次打出4道,其中C题是赛后补题,我把它想的太难了,一直想用dfs或者图论解决,结果最后看别人的AC代码发现只要观察规律即可,还有B题最后因为没有lld输出导致错误4发,很不应该。 A. K-divisible Sum 思路: 简单的签到题,n个数相加能够整除k,求n个数中最大的数最小时的值,其实就是求一下n个数相加能整除k时的和,然后平均给n个数,求出每个数的值,如果还有余数就+1。 #include <bits/stdc++.h> using namespace std; .

Sqssq的博客 543

软考架构师·教材版|05 图论:Dijkstra 最短路径与 AOE 关键路径

拿着一张“路口和过路费”的图,一轮一轮算出起点到每个路口的最短时间;再拿“项目工序图”正推倒推两遍,说出最快几天完工、哪些工序一天都拖不得。Dijkstra 与关键路径两道常考计算题,方法对了五分钟拿分。

weixin_46316564的博客 205
上一篇: AT4438 [AGC028D] Chords 题解
下一篇: P3736 [HAOI2016]字符合并 题解
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值