CodeForces-721C-Journey(DAG, DP)
链接:
https://vjudge.net/problem/CodeForces-721C
题意:
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 T time units passing it.
思路:
Dp[i][j] 为i点到n点经过j个点的时间.可以在图上得到方程Dp[i][j] = min(dp[k][j-1]+dis(i,k)).
代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e3+10;
struct Edge
{
int to, dis;
};
vector<Edge> G[MAXN];
bool Vis[MAXN];
int To[MAXN][MAXN], Dp[MAXN][MAXN];
int n, m, t;
void Dfs(int x)
{
Vis[x] = true;
if (x == n)
return;
for (int i = 0;i < G[x].size();i++)
{
int v = G[x][i].to;
int dis = G[x][i].dis;
if (!Vis[v])
Dfs(v);
for (int j = 2;j <= n;j++)
{
if (Dp[v][j-1]+dis < Dp[x][j])
{
Dp[x][j] = Dp[v][j-1]+dis;
To[x][j] = v;
}
}
}
}
int main()
{
memset(Dp, 0x3f3f3f3f, sizeof(Dp));
scanf("%d %d %d\n", &n, &m, &t);
int u, v, w;
for (int i = 1;i <= m;i++)
{
scanf("%d %d %d", &u, &v, &w);
G[u].push_back(Edge{v, w});
}
Dp[n][1] = 0;
Dfs(1);
int maxp = 1;
for (int i = n;i >= 1;i--)
{
if (Dp[1][i] <= t)
{
maxp = i;
break;
}
}
printf("%d\n", maxp);
int p = 1, x = maxp;
printf("1");
while (x > 1)
{
printf(" %d", To[p][x]);
p = To[p][x--];
}
puts("");
return 0;
}
CodeForces-721C-Journey(DAG, DP)的更多相关文章
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- CodeForces 721C Journey
$dp$,拓扑排序. 记$dp[i][j]$表示走到节点$i$,走过了$j$个点的最小时间,然后就可以递推了.要注意的是节点$1$的入度一开始不一定等于$0$. #pragma comment(lin ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)
起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- codeforces 721C C. Journey(dp)
题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- Codeforces 721C [dp][拓扑排序]
/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...
- Codeforces Round #460 (Div. 2): D. Substring(DAG+DP+判环)
D. Substring time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...
随机推荐
- 跨域请求配置 Amazon AWS S3 腾讯云 阿里云 COS OSS 文件桶解决方案以及推荐 Lebal:Research
跨域请求配置 跨域请求指的就是不同的域名和端口之间的访问.由于 ajax 的同源策略影响.跨域请求默认是不被允许的. 使用@font-face外挂字体时,可能遇到跨域请求CROS问题:F12控制台报错 ...
- Python新手最容易犯的十大错误
1. 忘记写冒号 在 if.elif.else.for.while.class.def 语句后面忘记添加“:” if spam == 42 print('Hello!') 2. 误用 “=” 做等值比 ...
- linu基础命令1
/根目录,第一级目录 1.ls列出当前目录下的文件和目录-a: 列出所有的文件,包括所有以.开头的隐藏文件-d: 列出目录本身,并不包含目录中的文件(-ld)-h: 和-l一起使用,文件大小人类易读 ...
- Tensorflow 安装 和 初识
Windows中 Anaconda,Tensorflow 和 Pycharm的安装和配置 https://blog.csdn.net/zhuiqiuzhuoyue583/article/detai ...
- Doker部署Jmeter(一) 目标服务器部署Jmeter监控容器
用jmeter插件监控服务器性能之前也有提到:https://www.cnblogs.com/betterbb/p/11285022.html 这里主要记录一下docker上的部署,所需的3个插件可以 ...
- 【神经网络与深度学习】Caffe部署中的几个train-test-solver-prototxt-deploy等说明
1:神经网络中,我们通过最小化神经网络来训练网络,所以在训练时最后一层是损失函数层(LOSS), 在测试时我们通过准确率来评价该网络的优劣,因此最后一层是准确率层(ACCURACY). 但是当我们真正 ...
- Websocket --(2)实现
首先声明,本篇博文参考文章 https://blog.csdn.net/jack_eusong/article/details/79064081 主要在于理解和自己动手搭建环境,自己搭建的过程中会发生 ...
- Notepad++ 不打开历史文件
1. 自己的很多虚拟机上面安装了notepad++ 提高编辑文件的速度. 但是发现 有时候总是默认打开 很多 历史文件 会造成很卡顿. 2. 解决办法 如下图 设置->首选项 3. 具体的位置为 ...
- 【基本优化实践】【1.4】tempdb优化
[1]tempdb介绍 tempdb全局存储内部对象,用户对象,临时表,临时对象,以及SQL Server操作创建的存储过程.每个数据库实例只有一个tempdb,所以可能存在性能以及磁盘空间瓶颈. 各 ...
- easyUI关键(常见)组件详解
一.easyUI 相关介绍 1.EasyUI 是前端框架,封装大量 css和封装大量 JS 2.使用前端框架时,给标签定义class 属性,就会有样式和脚本功能了(class属性对应了相关封装过的cs ...