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

题目链接

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

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 

题目描述:

n个景点间有m条路,要求在时间t内从景点1走到景点n,并且使途中经过尽可能多的景点,输出景点数和路径。

 

Solution:

dp[i][j]:表示经过了i个景点后到达景点j时最小的花费。

如果由u到v是可到达的,那么dp[i][v] = min(dp[i][v], dp[i-1][u] + w(u, v));

最后就是转移方程的第一维度表示经过的景点数目,所以从1开始枚举到n,第二维可以通过遍历整个边集合,来找到每一个u到v来更新当前经历过i个点下的dp[i][v]。

f[i][j]:表示经过了i个景点之后,当前j节点的父节点(用于输出路径)。

 

代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define fi first
#define se second
#define rush() int T; scanf("%d", &T); while(T--)
#define _rush() int T; cin >> T; while(T--)
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef pair PII;
const int MaxN = 5e3 + 5;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

struct node{
	int u, v, w;
}G[MaxN];

int f[MaxN][MaxN], res[MaxN];
int dp[MaxN][MaxN];

int main() 
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	
	int n, m, t;
	cin >> n >> m >> t;
	for(int i = 1; i <= m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		G[i].u = u, G[i].v = v, G[i].w = w;
	}
	mst(dp, INF); //注意初始化为INF
	mst(f, 0); 
	dp[1][1] = 0; //dp[1][1]需要初始化为0
	int cnt;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) {
			int u = G[j].u, v = G[j].v, w = G[j].w;
			if(dp[i][v] > dp[i-1][u] + w) {
				dp[i][v] = dp[i-1][u] + w;
				f[i][v] = u; //用于输出路径
			}
		}
		if(dp[i][n] <= t) cnt = i; //cnt用来记录最多可经过的城市数
	}
	cout << cnt << endl;
	int cur = n, num = 0;
	while(cur) {
		res[++num] = cur;;
		cur = f[cnt--][cur];
	}
	for(int i = num; i > 0; i--) cout << res[i] << " ";
	cout << endl;
	return 0;
}

 

你可能感兴趣的:(动态规划)