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 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 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
题目链接:http://codeforces.com/problemset/problem/721/C
题目大意:有n个点,m条单向边,时间为t,问在时间 t 内,从1到n 最多能经过几个点
思路:按dfs的顺序进行DP,dp [ i ] [ j ] 表示到 点 i 经过 j 个城市的最小时间
代码:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
const int N=5005;
const int inf=0x3f3f3f3f;
int n,m,t,tot,first[N],vis[N];
int dp[N][N],path[N][N],a[N];
struct node
{
int v,w,nex;
}e[N<<1];
void adde(int u,int v,int w)
{
e[tot].v=v,e[tot].w=w;
e[tot].nex=first[u];
first[u]=tot++;
}
void dfs(int u,int fa,int k)
{
if(dp[u][k]>=t) return ; //剪枝,如果时间超过t就不往下更新了
for(int i=first[u];~i;i=e[i].nex)
{
int v=e[i].v;
if(dp[v][k+1]>dp[u][k]+e[i].w) //如果时间小于之前走的路径的时间就更新下去
{
dp[v][k+1]=dp[u][k]+e[i].w;
path[v][k+1]=u; //记录到点v经过k+1时间的路径
dfs(v,u,k+1);
}
}
}
int main()
{
scanf("%d%d%d",&n,&m,&t);
tot=0;
memset(first,-1,sizeof(first));
memset(dp,inf,sizeof(dp));
int u,v,w;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);
}
dp[1][1]=0;
dfs(1,1,1);
int ans=0;
for(int i=1;i<=n;i++)
if(dp[n][i]<=t) ans=i;
printf("%d\n",ans);
int p=n,cnt=0;
while(1)
{
a[cnt++]=p;
if(p==1) break;
p=path[p][ans--];
}
for(int i=cnt-1;i>=0;i--)
printf("%d%c",a[i],i==0?'\n':' ');
return 0;
}