Codeforces Round #374 (Div. 2) C(DAG上的DP)

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 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
Copy
4 3 13
1 2 5
2 3 7
2 4 8
output
3
1 2 4
input
Copy
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
Copy
5 5 6
1 3 3
3 5 3
1 2 2
2 4 3
4 5 2
output
3
1 3 5

思路:,
之前直接暴搜搜超时了,感觉时间复杂度减不下去,看了下题解,用的是dp,dp[i][j]代表从i点到n点需要经过j个点,
但我感觉和记忆化搜索的思维有点像像,都是储存各个点的最优状态,这样遍历到这个点的时候只要看下是否访问过了,
访问过了的话就更新一遍最优状态就可以了,这样就可以节省很多时间复杂度,

实现代码:
#include<bits/stdc++.h>
using namespace std;
const int M = 5e3+10;
#define ll long long
int n,m;
int T;
int dp[M][M],to[M][M],vis[M];
vector<int>g[M];
vector<int>q[M];
void dfs(int u){
    vis[u] = 1;
    if(u==n) return ;
    for(int i = 0;i < g[u].size();i ++){
        int v = g[u][i];
        int d = q[u][i];
        if(!vis[v])
            dfs(v);
        for(int j = 2;j <= n;j ++){
            if(dp[v][j-1] + d < dp[u][j]){
                dp[u][j] = dp[v][j-1] + d;
                to[u][j] = v;
            }
        }
    }
}

int main()
{
    memset(dp,0x3f,sizeof(dp));
    int u,v,x;
    cin>>n>>m>>T;
    for(int i = 0;i < m;i ++){
        cin>>u>>v>>x;
        g[u].push_back(v);
        q[u].push_back(x);
    }
    dp[n][1] = 0;
    dfs(1);
    for(int i = 1;i <= n;i ++){
        cout<<i<<" :";
        for(int j = 1;j <= n;j ++){
            cout<<j<<" "<<dp[i][j]<<" ";
        }
        cout<<endl;
    }
    int ans = 0;
    for(int i = n;i >= 1;i --){
        //cout<<dp[1][i]<<" ";
        if(dp[1][i] <= T){
            ans = i;
            cout<<ans<<endl;
            cout<<"1 ";
            break;
        }
    }
    int cur = 1;
    while(ans>1){
        cout<<to[cur][ans]<<" ";
        cur = to[cur][ans--];
    }
}
View Code

 

posted @ 2018-03-12 20:58  冥想选手  阅读(234)  评论(0编辑  收藏  举报