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

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

Copy
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input

Copy
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

Copy
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5 思路:,
之前直接暴搜搜超时了,感觉时间复杂度减不下去,看了下题解,用的是dp,dp[i][j]代表从i点到n点需要经过j个点,
但我感觉和记忆化搜索的思维有点像像,都是储存各个点的最优状态,这样遍历到这个点的时候只要看下是否访问过了,
访问过了的话就更新一遍最优状态就可以了,这样就可以节省很多时间复杂度, 实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 5e3+;
#define ll long long
int n,m;
int T;
int dp[M][M],to[M][M],vis[M];
vector<int>g[M];
vector<int>q[M];
void dfs(int u){
vis[u] = ;
if(u==n) return ;
for(int i = ;i < g[u].size();i ++){
int v = g[u][i];
int d = q[u][i];
if(!vis[v])
dfs(v);
for(int j = ;j <= n;j ++){
if(dp[v][j-] + d < dp[u][j]){
dp[u][j] = dp[v][j-] + d;
to[u][j] = v;
}
}
}
} int main()
{
memset(dp,0x3f,sizeof(dp));
int u,v,x;
cin>>n>>m>>T;
for(int i = ;i < m;i ++){
cin>>u>>v>>x;
g[u].push_back(v);
q[u].push_back(x);
}
dp[n][] = ;
dfs();
for(int i = ;i <= n;i ++){
cout<<i<<" :";
for(int j = ;j <= n;j ++){
cout<<j<<" "<<dp[i][j]<<" ";
}
cout<<endl;
}
int ans = ;
for(int i = n;i >= ;i --){
//cout<<dp[1][i]<<" ";
if(dp[][i] <= T){
ans = i;
cout<<ans<<endl;
cout<<"1 ";
break;
}
}
int cur = ;
while(ans>){
cout<<to[cur][ans]<<" ";
cur = to[cur][ans--];
}
}

Codeforces Round #374 (Div. 2) C(DAG上的DP)的更多相关文章

  1. Codeforces Round #374 (Div. 2) C DAG上dp

    C. Journey time limit per test 3 seconds memory limit per test 256 megabytes input standard input ou ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  3. Codeforces Round #374 (Div. 2)

    A题和B题是一如既往的签到题. C题是一道拓扑序dp题,题意是给定一个DAG,问你从1号点走到n号点,在长度不超过T的情况下,要求经过的点数最多,换个思维,设dp[i][j]表示到i号点时经过j个点的 ...

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

  5. Codeforces Round #530 (Div. 2) F 线段树 + 树形dp(自下往上)

    https://codeforces.com/contest/1099/problem/F 题意 一颗n个节点的树上,每个点都有\(x[i]\)个饼干,然后在i节点上吃一个饼干的时间是\(t[i]\) ...

  6. Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  7. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  8. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

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

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

随机推荐

  1. FFmpeg编程学习笔记二:音频重採样

    ffmpeg实现音频重採样的核心函数swr_convert功能很强大,但是ffmpeg文档对它的凝视太过简单.在应用中往往会出这样那样的问题,事实上在读取数据->重採样->编码数据的循环中 ...

  2. 有关C++的数据类型(int,long,short,float,double等等)

    再看C++ prime plus 第六版的时候 对数据类型又一次有些乱了,在看了这篇博客后,重新清晰起来了. 有关C++的数据类型(int,long,short,float,double等等)

  3. NetWork——描述一次完整的网络请求过程

    台根DNS,根DNS服务器收到请求后会返回负责这个域名(.net)的服务器的一个IP,本地DNS服务器使用该IP信息联系负责.net域的这台服务器.这台负责.net域的服务器收到请求后,如果自己无法解 ...

  4. [清华集训2015 Day2]矩阵变换-[稳定婚姻模型]

    Description 给出一个N行M列的矩阵,保证满足以下性质: M>N. 矩阵中每个数都是 [0,N]中的自然数. 每行中, [1,N]中每个自然数刚好出现一次,其余的都是0. 每列中,[1 ...

  5. mfc 进程的诞生和死亡

     进程概念  进程的诞生  进程的死亡 一. 进程: .简单的说 双击一个EXE图标时,系统就会产生一个相应的进程,分配相应的资源,并执行相应的代码. .标准一些的说法: 进程是一个具有独立功能 ...

  6. P3426 [POI2005]SZA-Template

    P3426 [POI2005]SZA-Template 链接 分析: 首先T一定是S的一个前缀,也是一个后缀. 判断一个前缀s[1...i]是不是满足条件,那么求出s[1...i]在s中出现的所有位置 ...

  7. ASP.NET多行文本框限制字符个数

    asp.net中TextBox当设置TextMode = Multiline时,其MaxLength属性无效.可使用JS进行辅助限制输入的字符个数.中文算两个字符,西文算1个字符. TextBox属性 ...

  8. shell变量常用方法

    变量之数组操作: 参考网址:http://www.jb51.net/article/55253.htm #直接赋值 [root@local-]=chengd [root@local-]=xrd [ro ...

  9. LintCode——交叉字符串

    描述:给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例:s1 = "aabcc" s2 = "dbbca" - 当 s3 = &quo ...

  10. PAT甲题题解-1065. A+B and C (64bit) (20)-大数溢出

    第一眼以为是大数据,想套个大数据模板,后来发现不需要.因为A.B.C的大小为[-2^63, 2^63],用long long 存储他们的值和sum. 接下来就是分类讨论:如果A > 0, B & ...