1. 程式人生 > >【Codeforces Round 374 (Div 2)C】【DAG上的DP】

【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

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

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 
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 5050, M = 0, Z = 1e9 + 7, inf = 0x3f3f3f3f;
template <class T1, class T2>inline void gadd(T1 &a, T2 b) { a = (a + b) % Z; }
int casenum, casei;
int n, m, T;
int f[N][N];	//f[i][j]表示當前在點i,接下來經過j個點(包括自己)到n點的最小距離
int nxt[N][N];	//nxt[i][j]表示當前在點i,接下來經過j個點(包括自己)到n點的後繼
vector< pair<int,int> >a[N];
bool vis[N];
void dfs(int x)
{
	if (vis[x])return; vis[x] = 1;
	for (auto it : a[x])
	{
		dfs(it.first);
		for (int i = 2; i <= n; ++i)
		{
			int dis = f[it.first][i - 1] + it.second;
			if (dis < f[x][i])
			{
				f[x][i] = dis;
				nxt[x][i] = it.first;
			}
		}
	}
}
void print()
{
	for (int i = n; ; --i)if (f[1][i] <= T)
	{
		printf("%d\n", i);
		int x = 1; printf("%d ", x);
		while (x != n)
		{
			x = nxt[x][i--];
			printf("%d ", x);
		}puts("");
		break;
	}
}
int main()
{
	while(~scanf("%d%d%d",&n, &m, &T))
	{
		for (int i = 1; i <= n; ++i)a[i].clear(), vis[i] = 0;
		for (int i = 1; i <= m; ++i)
		{
			int x, y, z;
			scanf("%d%d%d", &x, &y, &z);
			if (y == 1 || x == n)continue;
			a[x].push_back({ y,z });
		}
		MS(f, 63);
		f[n][1] = 0; vis[n] = 1;
		dfs(1);
		print();
	}
	return 0;
}
/*
【題意】
DAG圖,5000個點,5000條邊
讓你求從1到n的路徑長度不超過T中經過點數最多的一條
【型別】
DP
【分析】
比賽的時候傻傻沒讀清題目要求"DAG圖的方向不一定是下標的升序",然後wa了
之後用拓撲排序重標號再AC。
不過這樣就顯得有些麻煩了。
事實上,我們可以用記憶化的形式實現
定義:
int f[N][N];	//f[i][j]表示當前在點i,接下來經過j個點(包括自己)到n點的最小距離
int nxt[N][N];	//nxt[i][j]表示當前在點i,接下來經過j個點(包括自己)到n點的後繼
然後我們對每個點都求出其f[]和nxt[]
注意,這個dfs採取記憶化的方式,起點是n,終點為1
如果我們有x的後繼為y
那麼我們肯定可以對y做dfs,在對y做dfs的時候,顯然不會走到x(DAG圖)
換而言之,y的所有可能的前驅(包括前驅的前驅)都不會影響到dfs(y)的值
於是,這裡就保證了該dp的正確拓撲序,也就可以在O(n^2)的複雜度內出解。
【時間複雜度&&優化】
O(n^2)
*/