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

JavaScript性能优化:全链路性能优化策略 JavaScript性能优化是提升Web应用质量的核心要素,直接影响用户体验与业务成果。研究表明,页面加载延迟1秒可能导致转化率下降7%。高效的JavaScript执行能减少主线程阻塞,避免动画卡顿或操作延迟,尤其在移动端低性能设备上更为关键。优化内存管理与减少冗余计算可降低浏览器资源消耗,防止因内存泄漏导致的崩溃,提升应用稳定性。在竞争激烈的互联网环境中,性能优势已成为产品差异化的关键因素,直接影响用户留存与商业价值。因此,性能优化应作为开发全周期的核心实践,而非事后补救措施。 阅读详情

题目链接:http://codeforces.com/contest/721/problem/C

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 of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1 to n, and some of them are connected by one-directional roads. The roads in Berlatov are designed in a way such that there are no cyclic routes between showplaces.

Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplace n. Naturally, Irina wants to visit as much showplaces as she can during her journey. However, Irina's stay in Berlatov is limited and she can't be there for more than T time units.

Help Irina determine how many showplaces she may visit during her journey from showplace 1 to showplace n within a time not exceeding T. It is guaranteed that there is at least one route from showplace 1 to showplace n such that Irina will spend no more than Ttime units passing it.

Input

The first line of the input contains three integers n, m and T (2 ≤ n ≤ 5000,  1 ≤ m ≤ 5000,  1 ≤ T ≤ 109) — the number of showplaces, the number of roads between them and the time of Irina's stay in Berlatov respectively.

The next m lines describes roads in Berlatov. i-th of them contains 3 integers ui, vi, ti (1 ≤ ui, vi ≤ n, ui ≠ vi, 1 ≤ ti ≤ 109), meaning that there is a road starting from showplace ui and leading to showplace vi, and Irina spends ti time units to pass it. It is guaranteed that the roads do not form cyclic routes.

It is guaranteed, that there is at most one road between each pair of showplaces.

Output

Print the single integer k (2 ≤ k ≤ n) — the maximum number of showplaces that Irina can visit during her journey from showplace 1 to showplace n within time not exceeding T, in the first line.

Print k distinct integers in the second line — indices of showplaces that Irina will visit on her route, in the order of encountering them.

If there are multiple answers, print any of them.

Examples
input
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4 
input
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
output
4
1 2 4 6 
input
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5 

题意:

有n个点, 给出m条边,每条边都有权值(可以理解为距离)。问在T距离之内,从顶点1走到顶点n,最多可以经过多少个顶点? 

其中,构成的图中不会出现环,且题目至少有一个答案。


题解:

一开始以为是最短路径,很显然不是,因为题目求的不是最短路,而是在限定的起点、终点和距离的情况下,最多能经过多少个点。


然后想到应该可以用DP:

dp[len][v]:从顶点1开始,途经len个顶点(包括起点终点),终点为v所花费的最短距离。

枚举len*枚举边:在已有的dp[len-1][u]的基础上,再得出dp[len][v], 很有DP的味道。

(注:用vector存顶点之间的关系时,枚举边 = 枚举起点*枚举终点)


保存路径:

一开始是用1维数组fa[]来保存。后来发现,假设当前顶点为v,用一维存的话,存的fa[v]仅仅是路径所能达到最大时的fa[v],而之前长度较小的路径的fa[v]会被新的fa[v]给覆盖掉。因此需要开二维数组fa[len][v]:记录在路径长度为len时,v的前一个顶点。


注意点:

if(dp[len-1][u]==INF) continue;   因为当dp[len-1][u]不合法时(其值设为2e9), 如果直接把他与w(最大为1e9)相加, 结果为3e9,超出了int的范围了。所以以后还是先判断其值是否合法或存在,然后再进项操作。



类似的DP:http://blog.youkuaiyun.com/dolfamingo/article/details/71024194



1.枚举长度*枚举起点*枚举终点:

#include <bits/stdc++.h>
using namespace std;
#define ms(a, b)  memset((a), (b), sizeof(a))
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 5000+10;

struct node
{
    int v, w;
};

int n,m,T;
vector<node>G[maxn];
int dp[maxn][maxn], fa[maxn][maxn];

void init()
{
    scanf("%d%d%d",&n,&m,&T);

    for(int i = 1; i<=n; i++)
        G[i].clear();

    for(int i = 1; i<=m; i++)
    {
        int u;
        node e;
        scanf("%d%d%d",&u,&e.v,&e.w);
        G[u].push_back(e);
    }

    ms(fa,0);
    for(int i = 1; i<=n; i++)
    for(int j = 1; j<=n; j++)
        dp[i][j] = INF;
    dp[1][1] = 0;
}

void prt(int len, int u)
{
    if(len>1)
        prt(len-1, fa[len][u]);

    printf("%d ",u);
}

void solve()
{
    int k;
    for(int len = 2; len<=n; len++)
    {
        for(int u = 1; u<n; u++)
        {
            for(int i = 0; i<G[u].size(); i++)
            {
                int v = G[u][i].v;
                int w = G[u][i].w;

                if(dp[len-1][u]==INF) continue; //少了这步,如果继续用int,会溢出,因为INF+1e9

                int tmp = dp[len-1][u] + w;
                if(tmp<=T && dp[len][v]>tmp)
                {
                    dp[len][v] = tmp;
                    fa[len][v] = u;
                }
            }
        }
        if(dp[len][n]!=INF)
            k = len;
    }

    printf("%d\n",k);
    prt(k,n); putchar('\n');

}

int main()
{
    init();
    solve();
    return 0;
}


2.枚举长度*枚举边:

#include <bits/stdc++.h>
using namespace std;
#define ms(a, b)  memset((a), (b), sizeof(a))
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 5000+10;

struct node
{
    int u, v, w;
    void read()
    {
        scanf("%d %d %d",&u, &v, &w);
    }
}edge[maxn];

int n,m,T;
int dp[maxn][maxn], fa[maxn][maxn];

void init()
{
    scanf("%d%d%d",&n,&m,&T);
    for(int i = 1; i<=m; i++)
        edge[i].read();

    ms(fa,0);
    for(int i = 1; i<=n; i++)
    for(int j = 1; j<=n; j++)
        dp[i][j] = INF;
    dp[1][1] = 0;
}

void prt(int len, int u)
{
    if(len>1)
        prt(len-1, fa[len][u]);

    printf("%d ",u);
}

void solve()
{
    int k;
    for(int len = 2; len<=n; len++)
    {
        for(int i = 1; i<=m; i++)
        {
            int u = edge[i].u;
            int v = edge[i].v;
            int w = edge[i].w;

            if(dp[len-1][u]==INF) continue;  //少了这步,如果继续用int,会溢出,因为INF+1e9

            int cost = dp[len-1][u] + w;
            if(cost<=T && cost<dp[len][v])
            {
                dp[len][v] = cost;
                fa[len][v] = u;
            }
        }

        if(dp[len][n]!=INF)
            k = len;
    }

    printf("%d\n",k);
    prt(k,n); putchar('\n');
}

int main()
{
    init();
    solve();
    return 0;
}


【Python百日进阶-Web开发-Feffery】Day385 -“一起Dash”训练营Lesson-07_利用fac构建表单(上) 文章目录前言:fac是什么?“人生苦短,我用Python;Web开发,首选Feffery!”↓↓↓ 今日笔记 ↓↓↓一、费老师课堂1.1 什么是表单1.2 页面效果1.3 课堂源码二、课后作业2.1 页面效果2.2 项目源码 前言:fac是什么? feffery-antd-components(简称fac),是国内大佬费弗里(Feffery)老师基于著名的React UI组件库ant design进行二次开发,将ant design中的诸多实用组件及特性引入Dash,帮助开发者使用极低的纯Python代 阅读详情

相关推荐

SurfaceControlViewHost使用介绍

可以看到,主要用途:是把一个进程的View显示到另外一个进程里面。

chadm.xiang 1289

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

题意: 给你一个n点m边的图,让你从1走到n,找到一条经过尽量多点的路径,且路径边权和小于等于T 然后输出路径。 题解: 直接DP,DP[i][j]表示在j点,当前经过了i个点的最小代价是多少 #include #include #include #include #include #include #include #include #include

Ezereal的博客 347

保姆级教程:用TIA Portal V17给S7-1200 PLC和HMI做个联调仿真(附抢答器完整项目包)

本文提供了一份详细的TIA Portal V17教程,指导如何实现S7-1200 PLC与HMI的联合仿真开发。通过抢答器项目实例,涵盖从环境搭建、PLC编程到HMI设计的全流程,帮助工程师高效完成工业自动化系统的虚拟调试,显著提升开发效率。

weixin_30417487的博客 631

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

题目 n(n<=5e3)个点,m(m<=5e3)条边的有向图,保证无重边, 第i条边有一个权值ti(1<=ti<=1e9),代表经过这条边的时间, 给定一个T(1<=T<=1e9),要求在不超过T的时间下,从点1走到点n 输出满足条件的路径中,路径中经过的节点个数最多的路径, 输出长度,及路径途径哪些节点,题目保证最短路一定有解 思路来源 http...

小衣同学的博客 307

halcon 查找圆形

先找到大概位置进行拟合,然后再以拟合出的位置作为测量的大概位置进行找圆;

qq_59062726的博客 734

SDRAM控制器——添加读写FIFO

用FIFO实现数据缓存

H19981118的博客 2627

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

分析:dp[i][j]表示在i点已经经过j个点最小花费时间是多少。 这里wa了整场,因为j应该是外层循环而不是i#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <queue> #include <string> #include <cmath> #include <vect

JIBANCANYANG 384

用户行为数据分析 User Behavior Data from Taobao for Recommendation

一、提出问题 二、理解数据 数据来源 阿里云天池数据集:(https://tianchi.aliyun.com/dataset/dataDetail?dataId=649) 本文选取的数据集为UserBehavior.csv,共有5242880条数据。数据的具体类别如下表所示: 用户行为种类有四种,如下表所示 对于数据维度的说明如下: 在本文选取的2017年11月25日到2017年12月3日的时间范围内,不重复的用户数量为10202,不重复的商品ID数为412369,不重复的商品类目ID数为5859,

cccxxxqq的博客 5206

Codeforces Round #374 (Div. 2) C. Journey(拓扑+DP)(DAG上跑DP)

题目链接 思路:dp【i】【j】代表1到i中经过j个点的最小花费。 #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn=5e3+5; const int inf=1e9+1; int pre[maxn][maxn]; int dp[maxn][maxn],ru[maxn]...

qq_42479630的博客 271

UltraScale系列底层结构(3)——URAM概述

在UltraScale+™ 器件中,除了我们在常规熟悉的BRAM资源以外,还额外提供了URAM(UltraRAM)资源。 UltraRAM 是一种单时钟同步操作的高密度存储器,每个 UltraRAM 块可存储 288 Kb 数据,配置为 4K x 72 位的存储单元,其容量是 Block RAM 的八倍。虽然 UltraRAM 内部使用单端口存储单元,但通过在一个时钟周期内顺序执行端口 A 和端口 B 的操作,实现了双端口功能。

apple_53311083的博客 2837

Linux | Ubuntu18.04安装RTX 4060显卡驱动完整教程

本节详细介绍了如何在ubuntu18系统安装4060显卡的驱动。

九七的博客 5160

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

DAG 上动态规划 跑一遍dfs 转移状态,记录路径 #include using namespace std; const int maxn = 5007, INF = 0x7f7f7f7f; int n, m, len; int edge[maxn][3]; int f[maxn][maxn]; int g[maxn][maxn]; int route[maxn]; in

床前明月光 370

RK3588部署YOLO模型 包含模型量化+npu轻量化+ubuntu+RGA图像加速和多线程模型加速 包源码

本专栏为深度学习部署全栈学习体系,包含68篇由浅入深的教程,专为零基础学习者设计。内容涵盖TensorRT与RK3588平台部署技术,包含Jetson系列和RK系列硬件部署实战,采用"理论+源码+实战"三位一体的教学模式。学习路径从基础环境搭建到模型优化、容器化部署、多硬件平台适配,最终掌握工业级AI部署能力。特色包括C++基础教学、Docker容器化、INT8量化、多线程优化等核心技术,并附带YOLOv5、人脸检测、车牌识别等多个实战项目源码。适合算法工程师转部署方向、嵌入式开发者及对

FJN110的博客 3732

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

大大体题意: 给你一个有向图,要求从1点出发,到n 点结束,要求在T时间内 经过尽可能多的城市,输出最大数量,和路径! 思路: dp思想:比赛时自己想的太简单了,果然wa了! 令dp[i][j]表示用i个点  到目的地 j 的最小时间! 那么 直接枚举每个i 点了! 枚举目的地不好转移,那么 就枚举每个边,反正每个边也经过所有的点,也可以完成目的! 那么如果u 能到v! 那么dp[

aozil_yang的博客 751

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Codeforces Round #374 (Div. 2) C. Journey(dfs+dp)

dp[i][j]表示在i点出走过j个点所需的最短时间。 状态转移方程: dp[i][j] = min(dp[i][j], dp[k][j-1]+dis[k][i])

CillyB的博客 469

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Codeforces Round #374 (Div. 2) C. Journey 拓扑排序+dp

C. Journeytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRecently Irina arrived to one of the most famous cities of Berland — the Berlatov city.

liang23333的博客 382

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (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 Ber...

weixin_30260399的博客 93

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

题目链接 Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There are n showplaces in the city, numbered from 1to n, and some of them are connected by one-directional...

FFOYeah的博客 276

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

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

窦小雨的小点子 316

Codeforces Round #374 (Div. 2) C. Journey DPor最短路

题意给你一个n点m边的图,让你从1走到n,找到一条经过尽量多点的路径,且路径边权和小于等于T然后输出路径。分析:自己原本打算5000*5000×vector 果断爆了。。 然后用最短路的方式,dp【Node】里面记录cost和num,又在case35TLE。 看了别人,还是要好好学学记录路径的方法。。。 2. 别人的代码。。400ms 最短路spfa(一敲)220600 KB (所以其实

now_ing的博客 307

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

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 of

无常是生活的常态 586

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

原题链接 C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Recently Irina arrived to on

L. 302

Codeforces Round #178 (Div. 2) B. Shaass and Bookshelf —— DP

题解:http://codeforces.com/contest/294/problem/B B. Shaass and Bookshelf time limit per test 1 second memory limit per test 256 megabytes input standard input output

A man can be destroyed, but not defeated. 657

VK Cup 2012 Qualification Round 1 E. Phone Talks —— DP

题目链接:http://codeforces.com/contest/158/problem/E E. Phone Talks time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard

A man can be destroyed, but not defeated. 588
上一篇: Codeforces Round #363 (Div. 2) D. Fix a Tree —— 并查集
下一篇: Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
DOLFAMINGO
博客等级 码龄10年 8粉丝 216原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值