博客
关于我
Codeforces Round #374 (Div. 2) C. Journey(拓扑+DP)(DAG上跑DP)
阅读量:389 次
发布时间:2019-03-05

本文共 1036 字,大约阅读时间需要 3 分钟。

在这里插入图片描述
在这里插入图片描述
思路:dp【i】【j】代表1到i中经过j个点的最小花费。

#include
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];vector
>g[maxn];void tuopu(int n){ queue
q; for(int i=1;i<=n;++i) if(ru[i]==0) q.push(i); for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) dp[i][j]=inf; dp[1][1]=0; while(!q.empty()) { int top=q.front(); q.pop(); for(auto to:g[top]) { if(ru[to.first]) { ru[to.first]--; for(int j=2;j<=n;++j) if(dp[to.first][j]>dp[top][j-1]+to.second) dp[to.first][j]=dp[top][j-1]+to.second,pre[to.first][j]=top; if(ru[to.first]==0) q.push(to.first); } } }}void dfs(int now,int ans){ if(!ans) return ; dfs(pre[now][ans],ans-1); printf("%d ",now);}int main(){ int n,m,x,y; ll T,z; scanf("%d%d%lld",&n,&m,&T); for(int i=1;i<=m;++i) { scanf("%d %d %lld",&x,&y,&z); g[x].push_back({ y,z}); ru[y]++; } tuopu(n); int ans=0; for(int i=1;i<=n;++i) if(dp[n][i]<=T) ans=max(ans,i); printf("%d\n",ans); dfs(n,ans); }

转载地址:http://jeewz.baihongyu.com/

你可能感兴趣的文章
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
NIS认证管理域中的用户
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>
NLog类库使用探索——详解配置
查看>>
NLP 基于kashgari和BERT实现中文命名实体识别(NER)
查看>>
NLP 时事和见解【2023】
查看>>
NLP 模型中的偏差和公平性检测
查看>>
Vue3.0 性能提升主要是通过哪几方面体现的?
查看>>
NLP 项目:维基百科文章爬虫和分类【01】 - 语料库阅读器
查看>>
NLP_什么是统计语言模型_条件概率的链式法则_n元统计语言模型_马尔科夫链_数据稀疏(出现了词库中没有的词)_统计语言模型的平滑策略---人工智能工作笔记0035
查看>>
NLP、CV 很难入门?IBM 数据科学家带你梳理
查看>>