链接:

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)的更多相关文章

  1. CodeForces 721C Journey(拓扑排序+DP)

    <题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...

  2. CodeForces 721C Journey

    $dp$,拓扑排序. 记$dp[i][j]$表示走到节点$i$,走过了$j$个点的最小时间,然后就可以递推了.要注意的是节点$1$的入度一开始不一定等于$0$. #pragma comment(lin ...

  3. CodeForces - 721C 拓扑排序+dp

    题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c     //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...

  4. CodeForces 839C - Journey | Codeforces Round #428 (Div. 2)

    起初误以为到每个叶子的概率一样于是.... /* CodeForces 839C - Journey [ DFS,期望 ] | Codeforces Round #428 (Div. 2) */ #i ...

  5. [Codeforces 1201D]Treasure Hunting(DP)

    [Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...

  6. codeforces 721C C. Journey(dp)

    题目链接: C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...

  7. codeforces 721C (拓排 + DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  8. Codeforces 721C [dp][拓扑排序]

    /* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...

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

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

    C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...

随机推荐

  1. mingw32环境上静态编译 dav1d 0.4.0

    2019-08-05 发布 dav1d 0.4.0 dav1d 'Cheetah', the fast and small AV1 decoder This is the fourth major r ...

  2. RL - 001- 入门

    https://www.freecodecamp.org/news/an-introduction-to-reinforcement-learning-4339519de419/ https://gi ...

  3. Dojo入门:DOM操作

      作为一款功能齐全的js工具包,dojo提供了统一的DOM操作方法. dojo.byId dojo.byId 函数使您可以通过 id 属性选择一个 DOM 节点.该函数是标准 document.ge ...

  4. line-height 与 height 的区别

    line-height是行高的意思,它决定了元素中文本内容的高度,height则是定义元素自身的高度. height:表示  行高 line-height:表示   每行文字所占的高度   举例: 第 ...

  5. [原创]关于类似方程x+y+z=P的解的总解

    1:如果x,y,z>=0,则直接插板法c(P+3,3-1)2:如果x,y,z均有下界a1,a2,a3,则求解方程x+y+z=P-a1-a2-a33:如果x,y,z均有上界的自然数,则使用容斥定理 ...

  6. netcore 实现一个简单的Grpc 服务端和客户端

    参考资料,和详细背景不做赘述. 首先定义prop 文件 syntax ="proto3"; package RouteGrpc; service HelloWorld{ rpc S ...

  7. Akka系列(九):Akka分布式之Akka Remote

    前言.... Akka作为一个天生用于构建分布式应用的工具,当然提供了用于分布式组件即Akka Remote,那么我们就来看看如何用Akka Remote以及Akka Serialization来构建 ...

  8. k8s-kubernettes-sercet存储

    Secret Secret存在意义 Secret解决了密码.token.密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中.Secret可以以Volume或者环境变量的方 ...

  9. C语言作业09

    问题 答案 这个作业属于那个课程 C语言程序设计 这个作业要求在哪里 https://i.cnblogs.com/EditPosts.aspx?opt=1 我在这个课程的目标是 在学好C语言编程的基础 ...

  10. mysql应用之通过存储过程方式批量插入数据

    我们平时的测试过程中有一个环节就是准备测试数据,包括准备基础数据,准备业务数据,使用的场景包括压力测试,后台批量数据传输,前端大数据查询导出,或者分页打印等功能,准备测试数据我们通俗点讲就是造数据,根 ...