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. git和svn

    git 分布式管理工具 svn 集中式管理工具 1. Git是分布式的,SVN是集中式的,好处是跟其他同事不会有太多的冲突,自己写的代码放在自己电脑上,一段时间后再提交.合并,也可以不用联网在本地提交 ...

  2. 利用JSONP实现跨域请求

    前言:有时候一忙起来就没了时间观念,原来我已经有十多天没写博客了.一直想做跨域方面的尝试,无奈最近准备校招没时间动动手.今天就先讲讲JSONP吧,昨晚还在研究QQ空间日志里面网络图片的问题呢,我发现日 ...

  3. ABAP 锁机制

  4. 给SHP文件定义投影

    #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib.request import os def get_epsg_code(epsg ...

  5. linux命令学习使用记录

    1.文件批量重命名:把所有.xml文件重命名.txt,第一个参数为文件名中字符串,第二个参数为替换后文件名,第三个为当前目录文件列表 rename .xml .txt *.xml 2.解压不显示过程: ...

  6. 深入.net(多态一)

    代码优化技术: 当您在 编写一个类时,如果您发现你需要编写的“属性”和“方法”曾经在已有的类中实现,则,您可以将其共用的“属性”和“方法”剪切到一个新的“类”中,然后,让两个类共同继承这个“新类”.( ...

  7. 【代码笔记】iOS-点击一个button,出6个button

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> //加入头文件 #import "DCPathB ...

  8. macbook安装win7

    通常大家都喜欢购买苹果电脑,因为配置高,速度快,但是却不喜欢使用ios系统,这时候需要在macbook上安装windows系统 全新的macbook进行windows的安装,基本大家都会,使用boot ...

  9. WPF学习之路(十一)布局

    布局 Canvas 基本面板,传统布局方式,支持与设备无关的坐标定位元素 <Border BorderThickness="> <Canvas> <Button ...

  10. jstack简单使用,定位死循环、线程阻塞、死锁等问题

    当我们运行java程序时,发现程序不动,但又不知道是哪里出问题时,可以使用JDK自带的jstack工具去定位: 废话不说,直接上例子吧,在window平台上的: 死循环 写个死循环的程序如下: pac ...