Codeforces712 C. Journey (DP / 记忆化搜索)

来源:互联网 发布:php图片管理系统 编辑:程序博客网 时间:2024/06/08 00:09

题目连接:http://codeforces.com/contest/721/problem/C


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 131 2 52 3 72 4 8
output
31 2 4 
input
6 6 71 2 21 3 33 6 32 4 24 6 26 5 1
output
41 2 4 6 
input
5 5 61 3 33 5 31 2 22 4 34 5 2
output
31 3 5 




题目大意:给出一个有向无环图,每条边都有时间,求在给定时间内从1走到n最多经过多少个点,输出路径。


解题思路:DP。


dp[i][j]表示走到第i个点时经过了j个点的花费。

因为要输出路径,用pre记录,并且反向建图(递归输出路径,所以要反向)。

注意必须用vis记录经过的点,不然会TLE test11,因为即使无环,但一个点也可能经过多次,如:


1  2  1

1  3  1

1  4  1

2  5  1

3  5  1

4  5  1


如这组数据5就经过了3次。


其他细节看代码吧……



/* ***********************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆  ┃ 勒 ┃  ┆      ┆  ┃ 戈 ┗━━━┓ ┆┆  ┃ 壁     ┣┓┆┆  ┃ 的草泥马  ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <bitset>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()#define   mem(x,v)      memset(x,v,sizeof(x))typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")const int N = 5005;int n,m,t;struct Edge{    int to,nx,w;}edge[N*2];int cnt,head[N];void addedge(int u,int v,int w){    edge[cnt] = Edge{v,head[u],w};    head[u] = cnt++;}bool vis[N];int dp[N][N],pre[N][N];void solve(int u){    if(vis[u])return;    vis[u] = 1;    for(int i=head[u];~i;i=edge[i].nx)    {        int v = edge[i].to;        int w = edge[i].w;        solve(v);        for(int j=1;j<=n;j++)        {            if(dp[v][j-1]+w<dp[u][j])            {                dp[u][j] = dp[v][j-1] + w;                pre[u][j] = v;            }        }    }}void path(int x,int y){    if(y>1) path(pre[x][y],y-1);    printf("%d ",x);}int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);n = read(), m = read(), t = read();memset(dp,inf_add,sizeof dp);dp[1][1] = 0;memset(head,-1,sizeof head);for(int i=1;i<=m;i++)    {        int u,v,w;        u = read(), v = read(), w = read();        addedge(v,u,w);    }    solve(n);    int mx;    for(int i=n;i;i--)    {        if(dp[n][i]<=t)        {            mx = i;            break;        }    }    printf("%d\n",mx);    path(n,mx);return 0;}



0 0
原创粉丝点击