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 exceedingT. 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.
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.
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.
4 3 13 1 2 5 2 3 7 2 4 8
3 1 2 4
6 6 7 1 2 2 1 3 3 3 6 3 2 4 2 4 6 2 6 5 1
4 1 2 4 6
5 5 6 1 3 3 3 5 3 1 2 2 2 4 3 4 5 2
3 1 3 5
题意:有向无环图,n个点,m条有向边,每条边权值t表示经过这条边用时为t,总时间T。从1出发,到n,问最多能经过多少个点,并输出路径,要求总时间<=T。
我的解法:考虑的是spfa,由一个点更新临近点的条件有两个,因为要优先考虑点数,第一个就是经过这个点的的个数,走到这个点能经过更多的点数就更新点数和时间。第二个条件是走到这个点的点数和当前点被更新的点数一样,使得后面有更多的时间去走更多的点,如果被更新过来时间更少,就对当前的的时间进行更新。
用一个二维数组记录路径,第一维是点编号,第二维这个点由起点经过来的时候经过了几个点,保存的是由哪个点更新过来的。
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pi;
typedef pair<int,pi> pii;
const int N = 5010;
const int INF = 1e9+7;
struct node{
int v,w,next;
node(){}
node(int v,int w,int next):
v(v),w(w),next(next){}
}E[N];
int n,m,T,top;
int a[N]; ///路径
int num[N]; ///经过点数
int dis[N]; ///经过距离(时间)
int head[N]; ///头结点
int pre[N][N]; ///第一维是点编号,第二维是点数。
void Init()
{
top = 0;
for(int i = 0;i < N;i++){
head[i] = -1;
num[i] = 0;
dis[i] = INF;
for(int j = 0;j < N;j++){
pre[i][j] = 0;
}
}
}
void add(int u,int v,int w)
{
E[top] = node(v,w,head[u]);
head[u] = top++;
}
void spfa()
{
num[1] = 1;
dis[1] = 0;
queue<pii> q; ///pii每个点依次是,点编号,经过的点的数量,经过的距离
q.push(pii(1,pi(1,0)));
while(!q.empty()){
pii now = q.front();
q.pop();
int u = now.first;
int cnt = now.second.first+1;
for(int i = head[u];i != -1;i = E[i].next){
int v = E[i].v;
int dist = now.second.second+E[i].w;
if(dist > T) continue;
if(cnt > num[v]){
num[v] = cnt;
dis[v] = dist;
pre[v][cnt] = u;
q.push(pii(v,pi(cnt,dist)));
}
else if(cnt == num[v] && dist < dis[v]){
dis[v] = dist;
pre[v][cnt] = u;
q.push(pii(v,pi(cnt,dist)));
}
}
}
}
int main(void)
{
Init();
scanf("%d%d%d",&n,&m,&T);
for(int i = 1;i <= m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
}
spfa();
int pos; ///记录路径
for(int i = n;i >= 1;i--){
if(pre[n][i]){
pos = i;
break;
}
}
int cnt = 0;
a[cnt++] = n;
while(pos > 1){
a[cnt++] = pre[n][pos];
n = pre[n][pos];
pos--;
}
printf("%d\n",cnt);
for(int i = cnt-1;i >= 0;i--)
printf("%d ",a[i]);
}