题目链接:

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

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 题意:
给一个无环的无向图,问用不超过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)的更多相关文章

  1. 【10.58%】【codeforces 721C】Journey

    time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

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

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

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

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

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

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

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

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

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

  7. Codeforces 1063F - String Journey(后缀数组+线段树+dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神仙题,做了我整整 2.5h,写篇题解纪念下逝去的中午 后排膜拜 1 年前就独立切掉此题的 ymx,我在 2021 年的第 5270 个小 ...

  8. codeforces 721C (拓排 + DP)

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

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

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

随机推荐

  1. SQL SERVER获取数据库文件信息

        MS SQL SERVER 获取当前数据库文件等信息,适用于多个版本: SELECT dbf.file_id AS FileID , dbf.name AS [FileName] , s.fi ...

  2. 硅谷新闻4--解决页签手指按下从左到右滑动的bug

    有一种方法可以阻止父层的View截获touch事件,就是调用 getParent().requestDisallowInterceptTouchEvent(true);方法.一旦底层View收到tou ...

  3. dbcp2和dbcp 1.4在API层面的差异

    近期处于某种原因,打算把所有系统的数据库连接统一升级到dbcp2.发现有几处与dbcp 1在API层面发生了变化,主要如下所示: dbcp 2:org.apache.commons.dbcp2.Bas ...

  4. 关于JS闭包,作者不详(转)

    说明:本文由两篇文章结合而成,系从他人笔记中转过来的, 具体作者不详.因为觉得不错,遂共享之.如有侵权,立删致歉. 一.变量的作用域 要理解闭包,首先必须理解Javascript特殊的变量作用域. 变 ...

  5. Win10 FaceAPI小demo开发问题汇总

    Win10 FaceAPI小demo开发问题汇总 最近使用微软牛津计划做一个小demo,使用FaceAPI做一个小应用,实现刷脸的功能.开发的过程中用到几个问题,具体如下: Stream 与IRand ...

  6. VS 2013打开.edmx文件时报类型转换异常

      供应商提交了项目代码,但在我的电脑上打开项目编译时一直报Entityframework 的 .edmx文件转换异常,而无法通过编译.   分析后认为可能是entityframework的类库不够新 ...

  7. 参加2013中国大数据技术大会(BDTC2013)

    2013年12月5日-6日参加了为期两天的2013中国大数据技术大会(Big Data Technology Conference, BDTC2013),本期会议主题是:“应用驱动的架构与技术 ”.大 ...

  8. MyBatis入门(六)---mybatis与spring的整合

    一.整合需要 1.1.方法 上一章中的数据 需要spring通过单例方式管理SqlSessionFactory spring和mybatis整合生成代理对象,使用SqlSessionFactory创建 ...

  9. MyBatis入门(二)---一对一,一对多

    一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybati ...

  10. 基础学习day02--标识符、关键字、数据类型与运算符

    一.标识符和关键字   关键字: 就是被java语言赋予了特殊含义的单词. 特点就是所有的关键字都是小写.   标识符: 就是给包.类.接口.方法.变量名起的名字. 规则:1.以数字.字母._以及$符 ...