Recently Irina arrived to one of the most famous cities of Berland — the Berlatov city. There aren 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 thereare no cyclic routes between showplaces.
Initially Irina stands at the showplace 1, and the endpoint of her journey is the showplacen. 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 thanT time units.
Help Irina determine how many showplaces she may visit during her journey from showplace1 to showplace n within a time not exceedingT. It is guaranteed that there is at least one route from showplace1 to showplace n such that Irina will spend no more thanT time units passing it.
The first line of the input contains three integers n, m andT (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 showplacevi, and Irina spendsti 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 showplace1 to showplace n within time not exceedingT, 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条边,每条边都有一个权值; 要求出一条从1到n的路径,满足权值的累加最大且不大于k;
思路:
首先会想到求出从1到n的最短路径,求出来之后对这条路径进行松弛操作;
松弛操作的时候需要保存每个点在每种情况的时候的权值和;
那就是明显的dp了;
dp[i][j]表示在访问i点的时候,之前经过了j-1个点的时候的权值和;
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 5005;
int n,m,i;
int u[maxn],v[maxn];
int w[maxn];
int first[maxn],nxt[maxn];
int dp[maxn][maxn];
int pre[maxn][maxn];
__int64 t;
void dfs(int st, __int64 remain,int num){
int k= first[st];
while(k!=-1){
int to=v[k];
if(remain+w[k]<=t&&dp[to][num+1]>remain+w[k]){
dp[to][num+1]=remain+w[k];
pre[to][num+1]=st;
dfs(to,remain+w[k],num+1);
}
k=nxt[k];
}
return ;
}
int main(){
scanf("%d%d%I64d",&n,&m,&t);
int j;
for(i=1;i<=n;i++)
first[i]=-1;
for(i=1;i<=m;i++)
{
scanf("%d %d %d",&u[i],&v[i],&w[i]);//读入每一条边
nxt[i]=first[u[i]];
first[u[i]]=i;
}
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
dp[i][j]=0x3fffffff;
//cout<<0x3fffffff<<endl;
dp[1][0]=0;
dfs(1,0,1);
int ans=0;
for(i=n;i>=0;i--){
if(dp[n][i]<=t){
printf("%d\n",i);
ans=i;
break;
}
}
int fs=n;
int lu[maxn];
int fn=0;
lu[++fn]=n;
while(fs!=1){
lu[++fn]=pre[fs][ans];
fs=pre[fs][ans];
ans--;
}
for(i=fn;i>=1;i--)
printf("%d ",lu[i]);
printf("\n");
return 0;
}