codeforces 721C C. Journey(dp)
题目链接:
3 seconds
256 megabytes
standard input
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 from1 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 nocyclic 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.
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.
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.
4 3 13
1 2 5
2 3 7
2 4 8
3
1 2 4
6 6 7
1 2 2
1 3 3
3 6 3
2 4 2
4 6 2
6 5 1
4
1 2 4 6
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
3
1 3 5 题意:
给一个无环的无向图,问用不超过T的时间从1到n最多可以经过多少个点.要求输出一条路径;
思路: dp[i][j]表示经过了j个点到达i点,转移的时候直接dfs转移,记录下路径就好了;写的挫一点就T了, AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
#define lson o<<1
#define rson o<<1|1
typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9+200;
const int N=1e6+10;
const int maxn=5e3+5;
const double eps=1e-12; int n,m,t,head[maxn],cnt=0,path[maxn][maxn];//ti[maxn][maxn];
int dp[maxn][maxn];
//map<pair<int,int>,int>path,ti;
pair<int,int>p;
struct Edge
{
int to,next,val;
}edge[maxn]; inline void add_edge(int s,int e,int va)
{
edge[cnt].to=e;
edge[cnt].val=va;
edge[cnt].next=head[s];
head[s]=cnt++;
}
void dfs(int cur,int num,int tim,int fa,int gg)
{
if(tim>=dp[cur][num])return ;
dp[cur][num]=tim;
path[cur][num]=fa;
if(cur==n)return ;
for(int i=head[cur];i!=-1;i=edge[i].next)
{
int x=edge[i].to;
dfs(x,num+1,tim+edge[i].val,cur,edge[i].val);
}
}
void dfs1(int cur,int num,int tim)
{
int fa=path[cur][num];
if(fa>0)
{
for(int i=head[fa];i!=-1;i=edge[i].next)
{
int x=edge[i].to;
if(x!=cur)continue;
dfs1(fa,num-1,tim-edge[i].val);
}
}
printf("%d ",cur);
}
int main()
{
mst(head,-1);
read(n);read(m);read(t);
int u,v,w;
for(int i=1;i<=m;i++)
{
read(u);read(v);read(w);
add_edge(u,v,w);
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dp[i][j]=inf;
dfs(1,1,0,0,0);
int ans=0;
for(int i=1;i<=n;i++)
{
if(dp[n][i]<=t)ans=max(ans,i);
}
cout<<ans<<"\n";
dfs1(n,ans,dp[n][ans]);
return 0;
}
codeforces 721C C. Journey(dp)的更多相关文章
- 【10.58%】【codeforces 721C】Journey
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- CodeForces - 721C 拓扑排序+dp
题意: n个点m条边的图,起点为1,终点为n,每一条单向边输入格式为: a,b,c //从a点到b点耗时为c 题目问你最多从起点1到终点n能经过多少个不同的点,且总耗时小于等于t 题解: 这道 ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- Codeforces Round #374 (Div. 2) C. Journey DP
C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...
- Codeforces Round #374 (Div. 2) C. Journey —— DP
题目链接:http://codeforces.com/contest/721/problem/C C. Journey time limit per test 3 seconds memory lim ...
- Codeforces 1063F - String Journey(后缀数组+线段树+dp)
Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- Codeforces 721C [dp][拓扑排序]
/* 题意:给你一个有向无环图.给一个限定t. 问从1点到n点,在不超过t的情况下,最多可以拜访几个点. 保证至少有一条路时限不超过t. 思路: 1.由无后向性我们可以知道(取决于该图是一个DAG), ...
随机推荐
- 关于antlr包删除问题
在建这个网站,用户登录的时候,涉及查询问题,然后就出现了java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I错误,我一脸蒙逼,后来 ...
- Java中的Set集合接口实现插入对象不重复的原理
在java的集合中,判断两个对象是否相等的规则是: 1).判断两个对象的hashCode是否相等 . 如果不相等,认为两个对象也不相等,完毕 如果相等,转入2)(这一点只是为了提 ...
- Ahjesus Nodejs01 环境搭建及运行
访问http://nodejs.org/,根据系统选择下载文件,我用的win7 64 安装一路下一步直到完成 运行cmd输入node -v查看是否安装成功 成功会显示版本号 到此环境搭建完毕 ==== ...
- phpcms--使用添加php原生支持
1,phpcms模板中有时候要添加一些php相关变量这个时候要使用原始php的东西,可以如下加入 {php $no_wq_id=$r[id] ;}其中$r[id]是通过{pc:get sql=&quo ...
- python基础之常用模块以及格式化输出
模块简介 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...
- ABAP modify screen:修改屏幕,实现隐藏、禁止输入字段
Loop at screen会loop处理屏幕上的每一个组件,并对其做相应的处理. SELECTION-SCREEN: BEGIN OF BLOCK B1 WITH FRAME.PARAMETERS ...
- Hadoop技术内幕(YARN)第4章问题部分答案
问题1:改写DistributedShell程序,使得每个container运行在不同节点上(目前是随机的,可能运行在任意节点上). 问题2:改写DistributedShell程序,使得某个用户指定 ...
- Git使用ssh key
生成ssh key步骤 这里以配置github的ssh key为例: 1. 配置git用户名和邮箱 git config user.name "用户名" git config us ...
- Android群英传笔记系列三 view的自定义:实现一个模拟下载
1.实现效果:动态显示进度(分别显示了整个的动态改变的过程,然后完成后,弹出一个对话框) 2.实现过程:可以分为绘制一个圆,圆弧和文本三部分,然后在MainAcitivity中通过线程模拟 ...
- 【读书笔记】iOS-Coco内存管理规则-拥有对象
一,事件循环.一个典型的图形应用程序往往花费很多时间等待用户操作.在控制程序运行的人非常缓慢地作出决定(例如单击鼠标或按下某个键)以前,程序将一直处于空闲状态.当发生这样的事件时,程序被唤醒并开始工作 ...