【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 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 
#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)
*/

猜你喜欢

转载自blog.csdn.net/c_czl/article/details/82948648