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

题意:单向,没有回路,没有重边自环,限制时间,求1到n最多经过几个点,并输出这些点任意方案

因为没有环,又保证1和n连通,一开始想树形DP,并不好做,然后发现这是有向边
突然发现,这不就是有向无环图,有向无环图DAG的最短路最长路可以用DP来做,扩展一下应该也可以
f[i][j]表示从i到n经过j个点的时间 PS:因为忘判vis TLE一次
//
// main.cpp
// c
//
// Created by Candy on 9/30/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
using namespace std;
const int N=,M=,INF=1e9+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x;
}
int n,m,T,u,v,w;
struct edge{
int v,w,ne;
}e[M<<];
int h[N],cnt=;
void ins(int u,int v,int w){
cnt++;
e[cnt].v=v;e[cnt].w=w;e[cnt].ne=h[u];h[u]=cnt;
}
int f[N][N],vis[N];
void dp(int u){
if(u==n) return;
int child=;
if(vis[u]) return;
vis[u]=;
for(int i=h[u];i;i=e[i].ne){
child++;
int v=e[i].v,w=e[i].w;
dp(v);
for(int j=;j<=n;j++) if(f[v][j-]<INF)
f[u][j]=min(f[u][j],f[v][j-]+w);
}
}
void print(int u,int d){
printf("%d ",u);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v,w=e[i].w;
if(f[v][d-]<INF&&f[u][d]==f[v][d-]+w) {print(v,d-);break;}
}
}
int main(int argc, const char * argv[]) {
n=read();m=read();T=read();
for(int i=;i<=m;i++){
u=read();v=read();w=read();
ins(u,v,w);
}
memset(f,,sizeof(f));
f[n][]=;
dp();
int num=;
for(int i=n;i>=;i--)
if(f[][i]<=T) {num=i;break;}
printf("%d\n",num);
print(,num);
return ;
}

CF721C. Journey[DP DAG]的更多相关文章

  1. 拓扑排序+DP CF721C Journey

    CF721C Journey 给出一个\(n\)个点\(m\)条边的有向无环图. 问从\(1\)到\(n\),在距离不超过\(k\)的情况下最多经过多少点,并输出一个方案. \(topo\)+\(DP ...

  2. NYOJ16|嵌套矩形|DP|DAG模型|记忆化搜索

    矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...

  3. 「BZOJ1924」「SDOI2010」 所驼门王的宝藏 tarjan + dp(DAG 最长路)

    「BZOJ1924」[SDOI2010] 所驼门王的宝藏 tarjan + dp(DAG 最长路) -------------------------------------------------- ...

  4. CF #374 (Div. 2) C. Journey dp

    1.CF #374 (Div. 2)    C.  Journey 2.总结:好题,这一道题,WA,MLE,TLE,RE,各种姿势都来了一遍.. 3.题意:有向无环图,找出第1个点到第n个点的一条路径 ...

  5. codeforces 721C C. Journey(dp)

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

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

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

  7. Codeforce 721C DP+DAG拓扑序

    题意 在一个DAG上,从顶点1走到顶点n,路径上需要消费时间,求在限定时间内从1到n经过城市最多的一条路径 我的做法和题解差不多,不过最近可能看primer看多了,写得比较复杂和结构化 自己做了一些小 ...

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

  9. VK Cup 2015 - Qualification Round 1 A. Reposts [ dp DAG上最长路 ]

    传送门 A. Reposts time limit per test 1 second memory limit per test 256 megabytes input standard input ...

随机推荐

  1. [deviceone开发]-do_GridView的简单示例

    一.简介 do_GridView的高度支持-1,根据gridview里item的个数来决定gridview的高度,这样gridview自身就无法滚动了,需要放到固定高度的scrollview里才能滚动 ...

  2. 《javascript权威指南》读书笔记(连载)

    这是一篇很长的博客 终于把权威指南给买回来了,之前一直犹豫,第一:书太厚,怕买了不能坚持看完.第二:觉得太贵,最少100¥.现在实习也能发点工资了,给自己定了一个志愿:把一个月的工资用于买书.这么一想 ...

  3. SAP学习日志--RFC REMOTE FUNCTION CALL

    RFC  Remote function Call 远程功能调用, 是SAP系统之间以及非SAP系统之间程序通信的基本接口技术. 例如BAPI , ALE都是基于RFC实现的 SAP系统提供了三种外部 ...

  4. AE选中要素

    private void 选中要素ToolStripMenuItem_Click(object sender, EventArgs e) { if(axMapControl2.LayerCount&l ...

  5. Mac电脑清理硬盘"其他"

    作为一个MacBook的使用者,无不感受到苹果对于系统和硬件的完美匹配. 苹果电脑不适合玩游戏,所以我只用它开发iOS使用.电脑里除了Xcode和常用办公软件与通讯软件以外,我没有装其他的任何大应用. ...

  6. 如何优化TableView

    关于UITable的优化: 1.最常用的就是不重复生成单元格,很常见,很实用: 2.使用不透明的视图可以提高渲染速度,xCode中默认TableCell的背景就是不透明的: 3.如果有必要减少视图中的 ...

  7. 通过dubbo暴露接口调用方法,及基于zookeeper的dubbo涉及配置文件

    现在很流行的Dubbo很多朋友都听说过吧,最近我也在看这方面的东西,分享先我的心得笔记. 先说说我们团队要做的项目框架,很简单重在实现基于zookeeper的dubbo注册. 框架:springmvc ...

  8. jQuery加载一个html页面到指定的div里

    一.jQuery加载一个html页面到指定的div里 把a.html里面的某一部份的内容加载到b.html的一个div里.比如:加载a.html里面的<div id=“row"> ...

  9. MapReduce二次排序

    默认情况下,Map 输出的结果会对 Key 进行默认的排序,但是有时候需要对 Key 排序的同时再对 Value 进行排序,这时候就要用到二次排序了.下面让我们来介绍一下什么是二次排序. 二次排序原理 ...

  10. 开启Tomcat 源码调试

    开启Tomcat 源码调试 因为工作的原因,需要了解Tomcat整个架构是如何设计的,正如要使用Spring MVC进行Web开发,需要了解Spring是如何设计的一样,有哪些主要的类,分别是用于干什 ...